You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.0 KiB

  1. #!/usr/bin/env python
  2. import json
  3. import requests
  4. from datetime import datetime
  5. WEATHER_CODES = {
  6. '113': '☀️',
  7. '116': '⛅️',
  8. '119': '☁️',
  9. '122': '☁️',
  10. '143': '🌫',
  11. '176': '🌦',
  12. '179': '🌧',
  13. '182': '🌧',
  14. '185': '🌧',
  15. '200': '',
  16. '227': '🌨',
  17. '230': '❄️',
  18. '248': '🌫',
  19. '260': '🌫',
  20. '263': '🌦',
  21. '266': '🌦',
  22. '281': '🌧',
  23. '284': '🌧',
  24. '293': '🌦',
  25. '296': '🌦',
  26. '299': '🌧',
  27. '302': '🌧',
  28. '305': '🌧',
  29. '308': '🌧',
  30. '311': '🌧',
  31. '314': '🌧',
  32. '317': '🌧',
  33. '320': '🌨',
  34. '323': '🌨',
  35. '326': '🌨',
  36. '329': '❄️',
  37. '332': '❄️',
  38. '335': '❄️',
  39. '338': '❄️',
  40. '350': '🌧',
  41. '353': '🌦',
  42. '356': '🌧',
  43. '359': '🌧',
  44. '362': '🌧',
  45. '365': '🌧',
  46. '368': '🌨',
  47. '371': '❄️',
  48. '374': '🌧',
  49. '377': '🌧',
  50. '386': '',
  51. '389': '🌩',
  52. '392': '',
  53. '395': '❄️'
  54. }
  55. data = {}
  56. weather = requests.get("https://wttr.in/palm-coast?format=j1").json()
  57. def format_time(time):
  58. return time.replace("00", "").zfill(2)
  59. def format_temp(temp):
  60. return (hour['FeelsLikeF']+"°").ljust(3)
  61. def format_chances(hour):
  62. chances = {
  63. "chanceoffog": "Fog",
  64. "chanceoffrost": "Frost",
  65. "chanceofovercast": "Overcast",
  66. "chanceofrain": "Rain",
  67. "chanceofsnow": "Snow",
  68. "chanceofsunshine": "Sunshine",
  69. "chanceofthunder": "Thunder",
  70. "chanceofwindy": "Wind"
  71. }
  72. conditions = []
  73. for event in chances.keys():
  74. if int(hour[event]) > 0:
  75. conditions.append(chances[event]+" "+hour[event]+"%")
  76. return ", ".join(conditions)
  77. data['text'] = WEATHER_CODES[weather['current_condition'][0]['weatherCode']] + \
  78. " "+weather['current_condition'][0]['FeelsLikeF']+"°"
  79. data['tooltip'] = f"<b>{weather['current_condition'][0]['weatherDesc'][0]['value']} {weather['current_condition'][0]['temp_F']}°</b>\n"
  80. data['tooltip'] += f"Feels like: {weather['current_condition'][0]['FeelsLikeF']}°\n"
  81. data['tooltip'] += f"Wind: {weather['current_condition'][0]['windspeedMiles']}mph\n"
  82. data['tooltip'] += f"Humidity: {weather['current_condition'][0]['humidity']}%\n"
  83. for i, day in enumerate(weather['weather']):
  84. data['tooltip'] += f"\n<b>"
  85. if i == 0:
  86. data['tooltip'] += "Today, "
  87. if i == 1:
  88. data['tooltip'] += "Tomorrow, "
  89. data['tooltip'] += f"{day['date']}</b>\n"
  90. data['tooltip'] += f"⬆️ {day['maxtempF']}° ⬇️ {day['mintempF']}° "
  91. data['tooltip'] += f"🌅 {day['astronomy'][0]['sunrise']} 🌇 {day['astronomy'][0]['sunset']}\n"
  92. for hour in day['hourly']:
  93. if i == 0:
  94. if int(format_time(hour['time'])) < datetime.now().hour:
  95. continue
  96. data['tooltip'] += f"{format_time(hour['time'])} {WEATHER_CODES[hour['weatherCode']]} {format_temp(hour['FeelsLikeF'])} {hour['weatherDesc'][0]['value']}, {format_chances(hour)}\n"
  97. print(json.dumps(data))