-
Notifications
You must be signed in to change notification settings - Fork 1
/
weather.py
99 lines (79 loc) · 3.29 KB
/
weather.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import datetime
import io
import json
import os
import time
from base64 import b64encode
import configparser
import pytemperature
import requests
from dotenv import load_dotenv, find_dotenv
from flask import Flask, Response, render_template
GITHUB_USERNAME = "saumiko"
GITHUB_REPO = "weather-readme-widget"
GIT_BRANCH = "master"
CONFIG_FILE_URL = "https://raw.githubusercontent.com/{}/{}/{}/api/config.ini".format(GITHUB_USERNAME,
GITHUB_REPO,
GIT_BRANCH)
CONFIGURATION = requests.get(CONFIG_FILE_URL).text
config_buffer = io.StringIO(CONFIGURATION)
config = configparser.ConfigParser()
config.read_file(config_buffer)
load_dotenv(find_dotenv())
app = Flask(__name__)
OPENWEATHERMAP_KEY = os.getenv("OPENWEATHERMAP")
OPENWEATHERMAP_API_URL = config["api"]["openweathermap"].format(config["location"]["lat"],
config["location"]["lon"],
OPENWEATHERMAP_KEY)
ICON_URL = config["icon"]["openweathermap"]
UTC_PLUS = int(config["timezone"]["utc_plus"])
UTC_MINUS = int(config["timezone"]["utc_minus"])
UTC_BALANCE = UTC_PLUS + UTC_MINUS
"""
This program will give wrong output in local.
This is configured for Vercel deployment.
Since it runs on UTC..
"""
def set_sys_time():
os.environ["TZ"] = config["timezone"]["sys"]
time.tzset()
def load_image_b64(url):
response = requests.get(url)
return b64encode(response.content).decode("ascii")
def get_weather_widget():
global config
weather_data = json.loads(requests.get(OPENWEATHERMAP_API_URL).text)
current_time = datetime.datetime.fromtimestamp(weather_data["dt"] + UTC_BALANCE).strftime("%d %B, %Y - %I:%M:%S %p")
temperature = pytemperature.k2c(weather_data["main"]["temp"])
feels_like = pytemperature.k2c(weather_data["main"]["feels_like"])
max_temperature = pytemperature.k2c(weather_data["main"]["temp_max"])
min_temperature = pytemperature.k2c(weather_data["main"]["temp_min"])
weather_type = weather_data["weather"][0]["main"]
sunrise = datetime.datetime.fromtimestamp(weather_data["sys"]["sunrise"] + UTC_BALANCE).strftime("%I:%M:%S %p")
sunset = datetime.datetime.fromtimestamp(weather_data["sys"]["sunset"] + UTC_BALANCE).strftime("%I:%M:%S %p")
city = config["location"]["city"]
country = weather_data["sys"]["country"]
icon = weather_data["weather"][0]["icon"]
w_data = {
"city": city,
"country": country,
"current_time": current_time,
"weather_type": weather_type,
"temperature": int(temperature),
"feels_like": int(feels_like),
"max_temperature": int(max_temperature),
"min_temperature": int(min_temperature),
"sunrise": sunrise,
"sunset": sunset,
"image": load_image_b64(ICON_URL.format(icon))
}
return render_template("widget.html", **w_data)
@app.route("/", defaults={"path": ""})
@app.route("/<path:path>")
def catch_all(path):
svg = get_weather_widget()
resp = Response(svg, mimetype="image/svg+xml")
resp.headers["Cache-Control"] = "s-maxage=1"
return resp
if __name__ == "__main__":
app.run(debug=True)