1 #!/usr/bin/python3 2 3 # Dependencies: 4 # requests 5 6 import requests 7 import json 8 import pathlib 9 from datetime import datetime 10 11 # TODO: support other units 12 13 icons_day = { 14 "clear_sky": "☀️", 15 "few_clouds": "🌤️", 16 "scattered_clouds": "🌤", 17 "broken_clouds": "☁", 18 "overcast_clouds": "☁", 19 "shower_rain": "🌧", 20 "very_heavy_rain": "🌧", 21 "rain": "🌧", 22 "thunderstorm": "⛈", 23 "snow": "❄", 24 "mist": "🌫" 25 } 26 27 moon_phase = { 28 (0.00, 0.124): "🌑", 29 (0.124, 0.249): "🌒", 30 (0.250, 0.374): "🌓", 31 (0.375, 0.49): "🌔", 32 (0.50, 0.674): "🌕", 33 (0.675, 0.749): "🌖", 34 (0.750, 0.824): "🌗", 35 (0.825, 0.99): "🌘", 36 (0.99, 1): "🌑" 37 } 38 39 lat = -34.71 40 lon = -58.27 41 api_key = "5d30773002dbb7a5aaa846b1656965b2" 42 units = "metric" 43 exclude = "minutely,hourly,alert" 44 cache_file_name = "/home/mk/.cache/wthr.json" 45 46 # update time in minutes 47 update_time = 5 48 49 url = "https://api.openweathermap.org/data/2.5/onecall" 50 params = {'lat': lat, 'lon': lon, 'appid': api_key, 'units': units, 'exclude': exclude} 51 52 curr_time = datetime.now().timestamp() 53 cache_file = pathlib.Path(cache_file_name) 54 55 def cache_update(): 56 pass 57 58 def cache_fetch(): 59 pass 60 61 if cache_file.exists() != True: 62 # file doesn't exist, creates one 63 pathlib.Path(cache_file_name).touch() 64 print("cache file created") 65 66 try: 67 res = requests.get(url, params) 68 except (requests.ConnectionError, res): 69 print("☠️ Service down") 70 quit() 71 72 wthr_dict = json.loads(res.text) 73 with open(cache_file_name,'w') as cache_file: 74 json.dump(wthr_dict, cache_file, indent=4) 75 else: 76 # get file modified time 77 mtime = cache_file.stat().st_mtime 78 if (curr_time - mtime) > (update_time * 60): 79 try: 80 res = requests.get(url, params) 81 except (requests.ConnectionError, res): 82 print("☠️ Service down") 83 quit() 84 85 wthr_dict = json.loads(res.text) 86 with open(cache_file_name,'w') as cache_file: 87 json.dump(wthr_dict, cache_file, indent=4) 88 else: 89 try: 90 with open(cache_file_name,'r') as cache_file: 91 wthr_dict = json.loads(cache_file.read()) 92 except ValueError: 93 # file not JSON 94 try: 95 res = requests.get(url, params) 96 except (requests.ConnectionError, res): 97 print("☠️ Service down") 98 quit() 99 100 wthr_dict = json.loads(res.text) 101 with open(cache_file_name,'w') as cache_file: 102 json.dump(wthr_dict, cache_file, indent=4) 103 104 # print(json.dumps(wthr_dict, indent=4, sort_keys=True)) 105 106 desc = wthr_dict['current']['weather'][0]['description'] 107 main = wthr_dict['current']['weather'][0]['main'] 108 # temp = int(wthr_dict['current']['feels_like']) 109 temp = int(wthr_dict['current']['temp']) 110 icon = icons_day[desc.replace(' ', '_')] 111 moon_curr = wthr_dict['daily'][0]['moon_phase'] 112 curr_time_int = int(datetime.now().strftime('%H%M')) 113 sunrise = int(wthr_dict['current']['sunrise']) 114 sunset = int(wthr_dict['current']['sunset']) 115 116 sunrise = int(datetime.fromtimestamp(sunrise).strftime('%H%M')) 117 sunset = int(datetime.fromtimestamp(sunset).strftime('%H%M')) 118 119 for range, moon_icon in moon_phase.items(): 120 if range[0] <= moon_curr <= range[1]: 121 moon_curr = moon_icon 122 break 123 124 night = (0000, sunrise, sunset + 100, 2359) 125 sunset = (sunset, sunset + 50) 126 dusk = (sunset[1], sunset[1] + 50) 127 day = (sunrise, sunset) 128 129 if (dusk[0] <= curr_time_int <= dusk[1]) and main == 'Clear': 130 icon = "🌆" 131 elif (sunset[0] <= curr_time_int <= sunset[1]) and (main == 'Clear'): 132 icon = "🌇" 133 elif ((night[0] <= curr_time_int <= night[1]) or (night[2] <= curr_time_int <= night[3])): 134 icon = moon_curr 135 136 print("{} {}°C".format(icon, temp))
