mirror of
https://gitlab.com/ceda_ei/sky
synced 2025-10-25 16:40:05 +02:00
Compare commits
7 Commits
63213d43df
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 723fb81d28 | |||
| 6ac1457f3a | |||
| 2613906e2c | |||
| 9e86eee17d | |||
| fae99965c6 | |||
| 5d67e85b78 | |||
| d3f21f7ec0 |
@@ -13,3 +13,5 @@ To check weather for your location simply run the following command in a termina
|
|||||||
### In Fahrenheit
|
### In Fahrenheit
|
||||||
+ Weather for one week - `curl sky.webionite.com/f/location`
|
+ Weather for one week - `curl sky.webionite.com/f/location`
|
||||||
+ Weather for today - `curl sky.webionite.com/f/location/t`
|
+ Weather for today - `curl sky.webionite.com/f/location/t`
|
||||||
|
|
||||||
|
Replace sky.webionite.com/ with sky.webionite.com/p/ on Windows
|
||||||
|
|||||||
4
requirements.txt
Normal file
4
requirements.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
flask
|
||||||
|
mapbox
|
||||||
|
pytz
|
||||||
|
darkskylib
|
||||||
78
server.py
78
server.py
@@ -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"
|
||||||
@@ -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 "
|
||||||
@@ -150,32 +159,53 @@ def index():
|
|||||||
"In Fahrenheit\n"
|
"In Fahrenheit\n"
|
||||||
"+ Weather for one week - curl sky.webionite.com/f/location \n"
|
"+ Weather for one week - curl sky.webionite.com/f/location \n"
|
||||||
"+ Weather for today - curl sky.webionite.com/f/location/t\n\n"
|
"+ Weather for today - curl sky.webionite.com/f/location/t\n\n"
|
||||||
|
"Replace sky.webionite.com/ with sky.webionite.com/p/ on "
|
||||||
|
"Windows \n\n"
|
||||||
+ config.source + "\n")
|
+ config.source + "\n")
|
||||||
return text
|
return text
|
||||||
|
|
||||||
|
|
||||||
def main(location, geocoder, cache_db, kind, unit):
|
def main(location, geocoder, cache_db, kind, unit):
|
||||||
|
"Main function that returns the weather based on parameters"
|
||||||
|
try:
|
||||||
coordinates = get_coordinates(location, geocoder, cache_db)
|
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 - "
|
||||||
"https://darksky.net/poweredby/\n")
|
"https://darksky.net/poweredby/\n")
|
||||||
|
|
||||||
|
|
||||||
app.add_url_rule('/<location>', 'location', lambda location:
|
def strip_colors(text):
|
||||||
main(location, geocoder, cache_db, "daily", "c"))
|
"Strips ANSI colors from a string"
|
||||||
|
return re.sub('\033\\[[0-9;]+?m', '', text)
|
||||||
|
|
||||||
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.route('/<path:path>')
|
||||||
main(location, geocoder, cache_db, "hourly", "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('/f/<location>', 'location_f', lambda location:
|
|
||||||
main(location, geocoder, cache_db, "daily", "f"))
|
|
||||||
|
|
||||||
app.add_url_rule('/f/<location>/', 'location_f/', lambda location:
|
@APP.after_request
|
||||||
main(location, geocoder, cache_db, "daily", "f"))
|
def after(response):
|
||||||
|
"Sets content type to text/plain"
|
||||||
app.add_url_rule('/f/<location>/t', 'today_location_f', lambda location:
|
response.headers['Content-Type'] = "text/plain; charset=utf-8"
|
||||||
main(location, geocoder, cache_db, "hourly", "f"))
|
return response
|
||||||
|
|||||||
Reference in New Issue
Block a user