This repository has been archived by the owner on Mar 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.py
148 lines (115 loc) · 5.22 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from PIL import Image, ImageDraw, ImageFont
import json
import requests
from io import BytesIO
import datetime
import os
def current(rc, bg):
#Иконка посередине
icon = requests.get(f"https://openweathermap.org/img/wn/{rc['weather'][0]['icon']}@4x.png")
icon = Image.open(BytesIO(icon.content))
bg.paste(icon, (500,200), icon)
#Текст
draw_text = ImageDraw.Draw(bg)
font = ImageFont.truetype("./resource/Comfortaa-VariableFont_wght.ttf", size= 75)
#Температура
temp = f"{round(rc['main']['temp'])}c°"
draw_text.text((540,420), temp, font=font)
#город
font = ImageFont.truetype("./resource/Comfortaa-VariableFont_wght.ttf", size= 40)
city = rc['name']
country = rc['sys']['country']
w,h = draw_text.textsize(f'{city} / {country}')
draw_text.text((600-(w*2)+w/2,120), f'{city} / {country}', font=font)
def one_call(ro, bg):
i = 1
x = 165
x1 = 185
while True:
if i != 6:
icon = ro['daily'][i]['weather'][0]['icon']
tempDay = round(ro['daily'][i]['temp']['day'])
tempNight = round(ro['daily'][i]['temp']['night'])
date = datetime.datetime.utcfromtimestamp(ro['daily'][i]['dt']).strftime('%d.%m')
#Шрифт
font = ImageFont.truetype("./resource/Comfortaa-VariableFont_wght.ttf", size=20)
draw_text = ImageDraw.Draw(bg)
# Температура
draw_text.text((x1, 715), f'{tempDay}/{tempNight}c°',font=font)
# Дата
draw_text.text((x1, 620), f'{date}', font = font)
# Рисование иконки
icon = requests.get(f"https://openweathermap.org/img/wn/{icon}@2x.png")
icon = Image.open(BytesIO(icon.content))
bg.paste(icon, (x, 630), icon)
# Смешение координат
x1 = x1 + 200
x = x + 200
i = i + 1
else:
break
def doc_bar_left(rc, ro, bg, tran):
responce_current = rc
#Текст док бара всех значений
text_wind = tran['wind']
text_humidity = tran['humidity']
text_visibility = tran['visibility']
text_pressure = tran['pressure']
text_temp = tran['temp']
text_suns = tran['suns']
#Рисование на БГ
draw_text = ImageDraw.Draw(bg)
#Шрифт
font = ImageFont.truetype("./resource/Comfortaa-VariableFont_wght.ttf", size=30)
#TimeZone
timezone = responce_current['timezone']
#Дата на сегодня
date = datetime.datetime.utcfromtimestamp(responce_current['dt']+timezone).strftime('%d.%m')
draw_text.text((80, 70), date, font=font)
#Другой размер шрифта
font_small = ImageFont.truetype("./resource/Comfortaa-VariableFont_wght.ttf", size=25)
#ветер
wild = round(responce_current['wind']['speed'])
text= f'{text_wind}\n {wild}m/s'
draw_text.text((80, 120),text,font=font_small)
#Влажность
humidity = responce_current['main']['humidity']
text = f'{text_humidity}\n {humidity}%'
draw_text.text((80,190), text, font= font_small)
#Видимость
visibility = responce_current['visibility']
visibility = visibility / 1000
text= f'{text_visibility}\n {visibility}km'
draw_text.text((80, 260),text ,font = font_small)
#Давление
pressure = responce_current['main']['pressure']
text = f'{text_pressure}\n {pressure}hPa'
draw_text.text((80,330),text , font = font_small)
#температура
temp_min = round(ro['daily'][0]['temp']['min'])
temp_max = round(ro['daily'][0]['temp']['max'])
text = f'{text_temp}\n {temp_min}c°/{temp_max}c°'
draw_text.text((80, 400), text, font = font_small)
#Рассвет/закат
sunrise = datetime.datetime.utcfromtimestamp(responce_current['sys']['sunrise'] + timezone).strftime('%H:%M')
sunset = datetime.datetime.utcfromtimestamp(responce_current['sys']['sunset'] + timezone).strftime('%H:%M')
text = f'{text_suns}\n {sunrise}, {sunset}'
draw_text.text((80, 470),text ,font = font_small)
def main(city, responce_current, member_id, text):
url_current= f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid=bc6aa7041a41d2b23178b2828adba1ba&units=metric"
responce_current = requests.get(url_current).json()
cheker = list(responce_current['weather'][0]['icon'])
lat, lon = responce_current['coord']['lat'], responce_current['coord']['lon']
url_onecall = f"https://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={lon}&units=metric&exclude=minutely,hourly&appid=bc6aa7041a41d2b23178b2828adba1ba"
responce_onecall = requests.get(url_onecall).json()
#создание бк и рамок
bg = Image.open(f'./resource/{cheker[2]}.png')
frame = Image.open(f'./resource/frame.png')
bg.paste(frame, (0,0), frame)
current(responce_current, bg)
one_call(responce_onecall,bg)
doc_bar_left(responce_current, responce_onecall, bg, text)
save_point = f'./resource/{member_id}.png'
path = os.path.abspath(save_point)
bg.save(save_point)
return path