1
0
mirror of https://gitlab.com/ceda_ei/sky synced 2025-10-25 00:20:05 +02:00

Compare commits

..

4 Commits

Author SHA1 Message Date
723fb81d28 Fix 500 on unknown location 2020-08-08 15:39:04 +05:30
6ac1457f3a [Refactor] Better path parsing. Clean up codebase. 2020-01-07 11:20:54 +05:30
2613906e2c Change content type 2019-07-22 21:07:59 +05:30
9e86eee17d Add requirements.txt 2019-04-26 20:57:13 +05:30
2 changed files with 60 additions and 56 deletions

4
requirements.txt Normal file
View File

@@ -0,0 +1,4 @@
flask
mapbox
pytz
darkskylib

112
server.py
View File

@@ -1,23 +1,27 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
"Sky - A simple weather monitor"
import re
import sqlite3
from datetime import datetime
from textwrap import wrap
import darksky
from flask import Flask from flask import Flask
from mapbox import Geocoder from mapbox import Geocoder
import darksky from pytz import timezone
import config
from columnizer import Columnizer from columnizer import Columnizer
from ascii import arts from ascii import arts
import sqlite3
import config
from datetime import datetime
from pytz import timezone
from textwrap import wrap
import re
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", check_same_thread=False) CACHE_DB = sqlite3.connect("cache.sqlite", check_same_thread=False)
def get_coordinates(location, geocoder, cache_db): def get_coordinates(location, geocoder, cache_db):
"Returns the coordinates for a given location"
row = cache_db.execute("select lat,lon,full_name from location where " row = cache_db.execute("select lat,lon,full_name from location where "
"name = ?", (location,)).fetchone() "name = ?", (location,)).fetchone()
@@ -33,11 +37,14 @@ def get_coordinates(location, geocoder, cache_db):
def get_weather(coordinates): def get_weather(coordinates):
"Returns the forecast from the coordinates"
return darksky.forecast(config.dark_sky_key, coordinates[0], return darksky.forecast(config.dark_sky_key, coordinates[0],
coordinates[1]) coordinates[1])
def summary_to_c(summary): def summary_to_c(summary):
"Converts Fahrenheits to Celsius in a string"
# pylint: disable=invalid-name
for i in re.findall(r"[\d.]+°F", summary): for i in re.findall(r"[\d.]+°F", summary):
f = float(i[:-2]) f = float(i[:-2])
c = 5 * (f - 32)/9 c = 5 * (f - 32)/9
@@ -46,6 +53,7 @@ def summary_to_c(summary):
def weather_to_text(forecast, kind, unit, name): def weather_to_text(forecast, kind, unit, name):
"Converts the forecast to text"
text = name + "\n" text = name + "\n"
if unit == "c": if unit == "c":
text += summary_to_c(forecast[kind]["summary"]) + "\n\n" text += summary_to_c(forecast[kind]["summary"]) + "\n\n"
@@ -55,11 +63,11 @@ def weather_to_text(forecast, kind, unit, name):
row_1 = Columnizer() row_1 = Columnizer()
# Add today # Add today
today = [ today = [
"Current", "Current",
"", "",
*arts[forecast["currently"]["icon"]], *arts[forecast["currently"]["icon"]],
"" ""
] ]
if unit == "c": if unit == "c":
today += ["{0:.1f}°C".format( today += ["{0:.1f}°C".format(
5*(forecast["currently"]["temperature"]-32)/9)] 5*(forecast["currently"]["temperature"]-32)/9)]
@@ -138,8 +146,9 @@ def weather_to_text(forecast, kind, unit, name):
return text return text
@app.route('/') @APP.route('/')
def index(): def index():
"/ - Gives the default index page"
text = ("\nSky - A simple weather monitor\n\n" text = ("\nSky - A simple weather monitor\n\n"
"Usage\n\n" "Usage\n\n"
"To check weather for your location simply run the following " "To check weather for your location simply run the following "
@@ -157,7 +166,11 @@ def index():
def main(location, geocoder, cache_db, kind, unit): def main(location, geocoder, cache_db, kind, unit):
coordinates = get_coordinates(location, geocoder, cache_db) "Main function that returns the weather based on parameters"
try:
coordinates = get_coordinates(location, geocoder, cache_db)
except (IndexError, KeyError):
return "Location Not found"
weather = get_weather(coordinates) weather = get_weather(coordinates)
text = weather_to_text(weather, kind, unit, coordinates[2]) text = weather_to_text(weather, kind, unit, coordinates[2])
return ("\n" + text + "\n\n" + config.source + "\nPowered by Dark Sky - " return ("\n" + text + "\n\n" + config.source + "\nPowered by Dark Sky - "
@@ -165,47 +178,34 @@ def main(location, geocoder, cache_db, kind, unit):
def strip_colors(text): def strip_colors(text):
"Strips ANSI colors from a string"
return re.sub('\033\\[[0-9;]+?m', '', text) return re.sub('\033\\[[0-9;]+?m', '', text)
app.add_url_rule('/<location>', 'location', lambda location: @APP.route('/<path:path>')
main(location, geocoder, cache_db, "daily", "c")) def url_parser(path):
"Parses the URL"
location = ''
unit = 'c'
plain = False
frequency = 'daily'
for part in path.strip('/').split('/'):
if part == 'f':
unit = 'f'
elif part == 'p':
plain = True
elif part == 't':
frequency = 'hourly'
else:
location = part
if plain:
return strip_colors(main(location, GEOCODER, CACHE_DB, frequency,
unit))
return main(location, GEOCODER, CACHE_DB, frequency, unit)
app.add_url_rule('/<location>/', 'location/', lambda location:
main(location, geocoder, cache_db, "daily", "c"))
app.add_url_rule('/<location>/t', 'today_location', lambda location: @APP.after_request
main(location, geocoder, cache_db, "hourly", "c")) def after(response):
"Sets content type to text/plain"
app.add_url_rule('/f/<location>', 'location_f', lambda location: response.headers['Content-Type'] = "text/plain; charset=utf-8"
main(location, geocoder, cache_db, "daily", "f")) return response
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"))
app.add_url_rule('/p/<location>', 'plain_location', lambda location:
strip_colors(main(location, geocoder, cache_db, "daily",
"c")))
app.add_url_rule('/p/<location>/', 'plain_location/', lambda location:
strip_colors(main(location, geocoder, cache_db, "daily",
"c")))
app.add_url_rule('/p/<location>/t', 'plain_today_location', lambda
location: strip_colors(main(location, geocoder, cache_db,
"hourly", "c")))
app.add_url_rule('/p/f/<location>', 'plain_location_f', lambda location:
strip_colors(main(location, geocoder, cache_db, "daily",
"f")))
app.add_url_rule('/p/f/<location>/', 'plain_location_f/', lambda location:
strip_colors(main(location, geocoder, cache_db, "daily",
"f")))
app.add_url_rule('/p/f/<location>/t', 'plain_today_location_f', lambda
location: strip_colors(main(location, geocoder, cache_db,
"hourly", "f")))