Snippets
  • Uploaded By: entropy
  • Added: 3 years ago
  • Updated: 11 months ago
  • mIRC Version: 7.52+
  • Hits: 1,069
  • Size: 4.56KB
  • Downloads: 41
  • Review By: entropy



Weather v2.0

This snippet scrapes the weather with an API key and returns the results.

If you are using Weather v1.0 - we changed the api site, so you need to get a new api key.
Screenshot...

    
  3    0  Login to Vote.


Source Code:
  1.  
  2. ; Weather v2.0 - by entropy 2020
  3. ; If you are using Weather v1.0 - we changed the api site, so you need to get a new api key.
  4. ; Get your own API key: https://www.weatherapi.com/signup.aspx
  5.  
  6. ;================================================================== GROUPS
  7.  
  8. #weather on
  9. on *:input:#:{ if ($server && /* !iswm $1) { doweather $1- } }
  10. on *:text:*:#:{ doweather $1- }
  11. #weather end
  12.  
  13. ;================================================================== MENUS
  14.  
  15. menu status,channel {
  16. -
  17. Weather v2.0 $chr(9) $replace($group(#weather).status,o,O)
  18. .$iif($group(#weather).status == on,Turn Off,Turn On) { $iif($group(#weather).status == on,.disable,.enable) #weather | echo $color(info) -tag * [Weather] is now: $qt($replace($group(#weather).status,o,O)) }
  19. .-
  20. .API Key $chr(9) $iif(%api_key,$v1,NA) { var %a = $input(API Key?,5) | if (%a) { %api_key = %a } }
  21. .-
  22. .Get an API key { run https://www.weatherapi.com/signup.aspx }
  23. -
  24. }
  25.  
  26. ;================================================================== START/EXIT
  27.  
  28. on *:start:{
  29. if (!$hget(weather)) { hmake weather 100 }
  30. if ($exists($qt($scriptdirweather.dat))) { hload weather $qt($scriptdirweather.dat) }
  31. .timerweather -o 0 1800 saveweather
  32. }
  33. on *:exit:{ saveweather }
  34.  
  35. ;================================================================== ALIASES
  36.  
  37. alias -l saveweather { if ($hget(weather)) { hsave -o weather $qt($scriptdirweather.dat) } }
  38.  
  39. alias -l doweather {
  40. if ((?w iswm $1 && $left($1,1) isin @!.) || (?weather iswm $1 && $left($1,1) isin @!.)) {
  41. if ($2) { hadd -m weather $+($nick,.,$network) $lower($2-) | weather # $lower($2-) }
  42. else {
  43. if ($hget(weather,$+($nick,.,$network))) { weather # $lower($v1) }
  44. else { msg # No location specified! }
  45. }
  46. }
  47. }
  48.  
  49. alias -l weather {
  50. sockclose weather
  51. sockopen -e4 weather api.weatherapi.com 443
  52. set -u5 %_chan $1
  53. sockmark weather $replace($2-,$chr(32),+)
  54. }
  55. on *:sockopen:weather:{
  56. if ($sockerr) { return }
  57. sockwrite -n $sockname GET $+(https://api.weatherapi.com/v1/current.json?key=,%api_key,&q=,$sock($sockname).mark) HTTP/1.0
  58. sockwrite -n $sockname Host: api.weatherapi.com
  59. sockwrite -n $sockname User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0
  60. sockwrite -n $sockname Accept-Language: en-US
  61. sockwrite -n $sockname Content-Type: application/json; charset=utf-8
  62. sockwrite -n $sockname $str($crlf,2)
  63. }
  64.  
  65. on *:sockread:weather:{
  66. if ($sockerr) { return }
  67. var %a
  68. sockread -f %a
  69. tokenize 32 %a
  70. if ($1) {
  71. var %a = 1, %b, %c, %d
  72. while ($gettok($1-,%a,44)) {
  73. %b = $v1
  74. %c = $gettok(%b,2-,58)
  75. %d = $gettok(%b,3-,58)
  76.  
  77. if ("temp_c":* iswm %b) { set -u2 %_c %c }
  78. if ("temp_f":* iswm %b) { set -u2 %_f %c }
  79.  
  80. if ("lon":* iswm %b) { set -u2 %_lon %c }
  81. if ("lat":* iswm %b) { set -u2 %_lat %c }
  82.  
  83. if ("wind_mph":* iswm %b) { set -u2 %_mph %c }
  84. if ("wind_degree":* iswm %b) { set -u2 %_wdeg %c }
  85. if ("wind_dir":* iswm %b) { set -u2 %_wdir $remove(%c,") }
  86.  
  87. if ("condition":{"text":* iswm %b) { set -u2 %_cond $remove(%d,") }
  88.  
  89. if ("pressure_mb":* iswm %b) { set -u2 %_pres %c }
  90. if ("humidity":* iswm %b) { set -u2 %_hum %c }
  91.  
  92. if ("cloud":* iswm %b) { set -u2 %_cloud %c }
  93. if ("vis_miles":* iswm %b) { set -u2 %_vis %c }
  94.  
  95. if ({"location":{"name":* iswm %b) { set -u2 %_city $remove(%d,") }
  96. if ("region":* iswm %b) { set -u2 %_region $remove(%c,") }
  97. if ("country":* iswm %b) { set -u2 %_count $remove(%c,") }
  98.  
  99. if ("message":"API key is invalid."* iswm %b) { msg %_chan [Weather] $c Weather API Key is Invalid! | sockclose $sockname | return }
  100.  
  101. inc %a
  102. }
  103. }
  104. }
  105. on *:sockclose:weather:{
  106. if (%_city) {
  107. if (%_city != %_region) { var %t = %_city $+ $chr(44) %_region $+ $chr(44) %_count }
  108. else { var %t = %_city $+ $chr(44) %_count }
  109. %t = $replace(%t,United States of America,USA)
  110. msg %_chan [Weather] $c %t $c Temp: %_f $+ $chr(176) $+ F ( $+ %_c $+ $chr(176) $+ C $+ ) $c Lat: %_lat $c Lon: %_lon $c Wind: %_mph MPH $c Condition: %_cond $c Wind Degree: %_wdeg $c Wind Direction: %_wdir $c Pressure: %_pres $+ mb $c Humidity: %_hum $+ % $c Clouds: %_cloud $c Visibility: %_vis miles
  111. }
  112. else { msg %_chan [Weather] $c Nothing found! }
  113. }
  114. alias -l c { return $chr(9679) }


Comments
No Comments.

Login to Comment.