Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* 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();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement