Advertisement
Bugs_Larnia

LSL: RL Weather Checker

Jan 15th, 2021
929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* WEATHER CHECKER
  2. Created 2018-10-19 by Bugs Larnia
  3. *************
  4. WARNING: This script requires that you register at OpenWeatherMap.org and append the API key to this script
  5. WARNING: DO NOT SHARE YOUR API KEY OR AN EDITABLE SCRIPT WITH YOUR API KEY IN IT!!!
  6. *************
  7. Please keep annotations
  8. */
  9.  
  10. //USER SETTINGS
  11. string gsCity = "San Francisco"; //The city you want the weather for
  12. string gsCountry = "us"; //The country code (optional)
  13. string gsApiKey = "YOUR API_KEY HERE"; //The API key you receive from OpenWeatherMap.org
  14. //END USER SETTINGS
  15.  
  16. string gsUrl = "https://api.openweathermap.org/data/2.5/weather?q="; //The base URL
  17.  
  18. //Function to create the url + endpoint and send the request
  19. key SendCall()
  20. {
  21.     string sUrl = gsUrl + llEscapeURL(gsCity) + "," + gsCountry + "&APPID=" + gsApiKey;
  22.     key kRequest = llHTTPRequest(sUrl, [HTTP_METHOD, "GET", HTTP_BODY_MAXLENGTH, 16384], "");
  23.     return kRequest;
  24. }
  25.  
  26. //Function to set/clear the floating text
  27. SetText(string psText)
  28. {
  29.     if (psText != "")
  30.     {
  31.         psText += "\n \n \n \n";
  32.     }
  33.     llSetText(psText, <1.0, 1.0, 0.0>, 1.0);
  34. }
  35.  
  36. default
  37. {
  38.     state_entry()
  39.     {
  40.         SetText(""); //Clear the text
  41.     }
  42.  
  43.     touch_start(integer total_number)
  44.     {
  45.         SendCall(); //Send the request
  46.     }
  47.    
  48.     http_response(key pkHttpReqId, integer piStatus, list plMetaData, string psBody)
  49.     {      
  50.         if (piStatus == 200) //Check if the request is successful (200 == OK)
  51.         {
  52.             list lResponse = llJson2List(psBody); //Put the response in a list
  53.            
  54.             integer iIndex = llListFindList(lResponse, ["weather"]); //Get the weather item
  55.             list lWeather = llJson2List(llList2String(lResponse, iIndex + 1)); //Parse the weather item data
  56.                  
  57.             list lWeatherDetails = llJson2List(llList2String(lWeather, 0)); //Get the details
  58.             integer iWeatherId = llList2Integer(lWeatherDetails, 1); //We need the weather ID
  59.            
  60.             //Morph the ID into a text
  61.             //NOTE: A full list of weather ids can be found at: https://openweathermap.org/weather-conditions
  62.             string sWeatherType = "having unknown weather";
  63.             if (iWeatherId < 300)
  64.             {
  65.                 sWeatherType = "having thunderstorms";
  66.             }
  67.             else if (iWeatherId < 400)
  68.             {
  69.                 sWeatherType = "having some drizzle";
  70.             }
  71.             else if (iWeatherId < 600)
  72.             {
  73.                 sWeatherType = "having rain";
  74.             }
  75.             else if (iWeatherId < 700)
  76.             {
  77.                 sWeatherType = "having snow";
  78.             }
  79.             else if (iWeatherId < 800)
  80.             {
  81.                 sWeatherType = "under a bit of a haze";
  82.             }
  83.             else if (iWeatherId == 800)
  84.             {
  85.                 sWeatherType = "having a clear sky";
  86.             }
  87.             else
  88.             {
  89.                 sWeatherType = "having some clouds";
  90.             }
  91.            
  92.             //Set the floating text
  93.             SetText(gsCity + " is " + sWeatherType + " at the moment.");            
  94.         }
  95.         else //No 200, so error
  96.         {
  97.             SetText("Error " + (string)piStatus + "\n" + psBody); //Show the error
  98.         }
  99.         llSetTimerEvent(10.0); //Timer to clear the text
  100.     }
  101.    
  102.     //Timer to clear the text
  103.     timer()
  104.     {
  105.         llSetTimerEvent(0.0);
  106.         SetText("");
  107.     }
  108.    
  109.     //Housekeeping
  110.     on_rez(integer piParam)
  111.     {
  112.         llResetScript();
  113.     }
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement