mirror of
				https://gitlab.com/ceda_ei/sky
				synced 2025-10-31 18:40:04 +01:00 
			
		
		
		
	Add get_weather. Add daily to weather_to_text. Add enpoints.
Change main to accept kind, unit to allow for single function to deal with all cases. Added location/t for today's weather. Added /f for Fahrenheit.
This commit is contained in:
		
							
								
								
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							| @@ -1,3 +1,4 @@ | |||||||
| config.py | config.py | ||||||
| __pycache__/ | __pycache__/ | ||||||
| cache.sqlite | cache.sqlite | ||||||
|  | cache.sqlite-journal | ||||||
|   | |||||||
							
								
								
									
										74
									
								
								server.py
									
									
									
									
									
								
							
							
						
						
									
										74
									
								
								server.py
									
									
									
									
									
								
							| @@ -2,12 +2,17 @@ | |||||||
|  |  | ||||||
| from flask import Flask | from flask import Flask | ||||||
| from mapbox import Geocoder | from mapbox import Geocoder | ||||||
|  | import darksky | ||||||
|  | from columnizer import Columnizer | ||||||
|  | from ascii import arts | ||||||
| import sqlite3 | import sqlite3 | ||||||
| import config | import config | ||||||
|  | from datetime import datetime | ||||||
|  | from pytz import timezone | ||||||
|  |  | ||||||
| geocoder = Geocoder(access_token=config.mapbox_key) | geocoder = Geocoder(access_token=config.mapbox_key) | ||||||
| app = Flask(__name__) | app = Flask(__name__) | ||||||
| cache_db = sqlite3.connect("cache.sqlite") | cache_db = sqlite3.connect("cache.sqlite", check_same_thread=False) | ||||||
|  |  | ||||||
|  |  | ||||||
| def get_coordinates(location, geocoder, cache_db): | def get_coordinates(location, geocoder, cache_db): | ||||||
| @@ -25,11 +30,54 @@ def get_coordinates(location, geocoder, cache_db): | |||||||
|  |  | ||||||
|  |  | ||||||
| def get_weather(coordinates): | def get_weather(coordinates): | ||||||
|     return coordinates |     return darksky.forecast(config.dark_sky_key, coordinates[0], | ||||||
|  |                             coordinates[1]) | ||||||
|  |  | ||||||
|  |  | ||||||
| def weather_to_text(text): | def weather_to_text(forecast, kind, unit): | ||||||
|     return str(text) |     if kind == "daily": | ||||||
|  |         text = forecast["daily"]["summary"] + "\n\n" | ||||||
|  |         row_0 = Columnizer() | ||||||
|  |         row_1 = Columnizer() | ||||||
|  |         # Add today | ||||||
|  |         today = [ | ||||||
|  |                 "Current", | ||||||
|  |                 "", | ||||||
|  |                 *arts[forecast["currently"]["icon"]], | ||||||
|  |                 "" | ||||||
|  |             ] | ||||||
|  |         if unit == "c": | ||||||
|  |             today += ["{0:.1f}°C".format( | ||||||
|  |                 5*(forecast["currently"]["temperature"]-32)/9)] | ||||||
|  |         else: | ||||||
|  |             today += [str(forecast["currently"]["temperature"]) + "°F"] | ||||||
|  |         # today += ["", forecast["currently"]["summary"]] | ||||||
|  |         row_0.add_column(today) | ||||||
|  |         count = 1 | ||||||
|  |  | ||||||
|  |         for i in forecast["daily"]["data"]: | ||||||
|  |             x = [] | ||||||
|  |             date = datetime.fromtimestamp(i["time"], | ||||||
|  |                                           timezone(forecast["timezone"])) | ||||||
|  |             x += date.strftime("%d %B\n%A").split("\n") | ||||||
|  |             x += [*arts[i["icon"]]] | ||||||
|  |             x += [""] | ||||||
|  |             if unit == "c": | ||||||
|  |                 x += ["Max: {0:.1f}°C".format(5*(i["temperatureMax"]-32)/9)] | ||||||
|  |                 x += ["Min: {0:.1f}°C".format(5*(i["temperatureMin"]-32)/9)] | ||||||
|  |             else: | ||||||
|  |                 x += ["Max: {0:.1f}°C".format(i["temperatureMax"])] | ||||||
|  |                 x += ["Min: {0:.1f}°C".format(i["temperatureMin"])] | ||||||
|  |             # x += [i["summary"]] | ||||||
|  |             if count < 4: | ||||||
|  |                 row_0.add_column(x) | ||||||
|  |             elif count < 8: | ||||||
|  |                 row_1.add_column(x) | ||||||
|  |             else: | ||||||
|  |                 break | ||||||
|  |             count += 1 | ||||||
|  |     text += str(row_0) + "\n" + str(row_1) | ||||||
|  |     return text | ||||||
|  |  | ||||||
|  |  | ||||||
| @app.route('/') | @app.route('/') | ||||||
| @@ -39,12 +87,22 @@ def index(): | |||||||
|     return text |     return text | ||||||
|  |  | ||||||
|  |  | ||||||
| def main(location, geocoder, cache_db): | def main(location, geocoder, cache_db, kind, unit): | ||||||
|     coordinates = get_coordinates(location, geocoder, cache_db) |     coordinates = get_coordinates(location, geocoder, cache_db) | ||||||
|     weather = get_weather(coordinates) |     weather = get_weather(coordinates) | ||||||
|     text = weather_to_text(weather) |     text = weather_to_text(weather, kind, unit) | ||||||
|     return text + "\n" + config.source |     return ("\n" + text + "\n\n" + config.source + "\nPowered by Dark Sky - " | ||||||
|  |             "https://darksky.net/poweredby/\n") | ||||||
|  |  | ||||||
|  |  | ||||||
| app.add_url_rule('/<location>', 'location', lambda location: | app.add_url_rule('/<location>', 'location', lambda location: | ||||||
|                  main(location, geocoder, cache_db)) |                  main(location, geocoder, cache_db, "daily", "c")) | ||||||
|  |  | ||||||
|  | app.add_url_rule('/<location>/t', 'today_location', lambda location: | ||||||
|  |                  main(location, geocoder, cache_db, "hourly", "c")) | ||||||
|  |  | ||||||
|  | app.add_url_rule('/f/<location>', 'location_f', lambda location: | ||||||
|  |                  main(location, geocoder, cache_db, "daily", "f")) | ||||||
|  |  | ||||||
|  | app.add_url_rule('/f/<location>/t', 'today_location_f', lambda location: | ||||||
|  |                  main(location, geocoder, cache_db, "hourly", "f")) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user