Bugs Larnia Posted January 4 Share Posted January 4 This script will allow you to retrieve real world weather data for a location. Please note that this will require you to register at OpenWeatherMap.org (which is free and will allow a high number of free service calls per day) and obtain an API key, which will need to be pasted into gsApiKey for this to work. For readability, I have placed the city and country in global variables that are hard-coded, but you can, of course, expand this into something like a dialog or a menu. /* WEATHER CHECKER Created 2018-10-19 by Bugs Larnia ************* WARNING: This script requires that you register at OpenWeatherMap.org and append the API key to this script WARNING: DO NOT SHARE YOUR API KEY OR AN EDITABLE SCRIPT WITH YOUR API KEY IN IT!!! ************* Please keep annotations */ //USER SETTINGS string gsCity = "San Francisco"; //The city you want the weather for string gsCountry = "us"; //The country code (optional) string gsApiKey = "YOUR API_KEY HERE"; //The API key you receive from OpenWeatherMap.org //END USER SETTINGS string gsUrl = "https://api.openweathermap.org/data/2.5/weather?q="; //The base URL //Function to create the url + endpoint and send the request key SendCall() { string sUrl = gsUrl + llEscapeURL(gsCity) + "," + gsCountry + "&APPID=" + gsApiKey; key kRequest = llHTTPRequest(sUrl, [HTTP_METHOD, "GET", HTTP_BODY_MAXLENGTH, 16384], ""); return kRequest; } //Function to set/clear the floating text SetText(string psText) { if (psText != "") { psText += "\n \n \n \n"; } llSetText(psText, <1.0, 1.0, 0.0>, 1.0); } default { state_entry() { SetText(""); //Clear the text } touch_start(integer total_number) { SendCall(); //Send the request } http_response(key pkHttpReqId, integer piStatus, list plMetaData, string psBody) { if (piStatus == 200) //Check if the request is successful (200 == OK) { list lResponse = llJson2List(psBody); //Put the response in a list integer iIndex = llListFindList(lResponse, ["weather"]); //Get the weather item list lWeather = llJson2List(llList2String(lResponse, iIndex + 1)); //Parse the weather item data list lWeatherDetails = llJson2List(llList2String(lWeather, 0)); //Get the details integer iWeatherId = llList2Integer(lWeatherDetails, 1); //We need the weather ID //Morph the ID into a text //NOTE: A full list of weather ids can be found at: https://openweathermap.org/weather-conditions string sWeatherType = "having unknown weather"; if (iWeatherId < 300) { sWeatherType = "having thunderstorms"; } else if (iWeatherId < 400) { sWeatherType = "having some drizzle"; } else if (iWeatherId < 600) { sWeatherType = "having rain"; } else if (iWeatherId < 700) { sWeatherType = "having snow"; } else if (iWeatherId < 800) { sWeatherType = "under a bit of a haze"; } else if (iWeatherId == 800) { sWeatherType = "having a clear sky"; } else { sWeatherType = "having some clouds"; } //Set the floating text SetText(gsCity + " is " + sWeatherType + " at the moment."); } else //No 200, so error { SetText("Error " + (string)piStatus + "\n" + psBody); //Show the error } llSetTimerEvent(10.0); //Timer to clear the text } //Timer to clear the text timer() { llSetTimerEvent(0.0); SetText(""); } //Housekeeping on_rez(integer piParam) { llResetScript(); } } 2 Link to comment Share on other sites More sharing options...
Recommended Posts
Please take a moment to consider if this thread is worth bumping.
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now