refactor: weather API request changes

This commit is contained in:
Дмитрий Абдрахманов 2024-09-23 12:17:02 +03:00
parent 37ee688096
commit 0f49adebe1
7 changed files with 49 additions and 9 deletions

3
.gitignore vendored
View File

@ -1 +1,2 @@
__pycache__ __pycache__
__data__

File diff suppressed because one or more lines are too long

18
main.sh
View File

@ -1,3 +1,19 @@
#!/bin/bash #!/bin/bash
/usr/bin/pwd
curl localhost:8000 > data.out $str=`/usr/bin/diff --exclude=multi-user.target.wants -r ./services ~/.config/systemd/user`
echo $str
# Update daemon files
#/usr/bin/cp -ur ./services/* ~/.config/systemd/user
# Daemon launching
#/usr/bin/systemctl --user daemon-reload
#/usr/bin/systemctl --user is-active wthrc.service
#/usr/bin/systemctl --user restart wthrc.service
# Wait for restart
#/usr/bin/sleep 5
/usr/bin/curl localhost:8000 > ./__data__/data.out

11
services/wthrc.service Normal file
View File

@ -0,0 +1,11 @@
[Unit]
Description=Weather collection service
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/python3 /home/citrullux/ModernOSLabs/src/main.py
Restart=always
[Install]
WantedBy=multi-user.target

View File

@ -1,3 +1,3 @@
from server import run from server import weather_run
run() weather_run()

View File

@ -4,7 +4,7 @@ import os
from weather import weather_handler from weather import weather_handler
def run(server_class=HTTPServer, handler_class=BaseHTTPRequestHandler): def weather_run(server_class=HTTPServer, handler_class=BaseHTTPRequestHandler):
server_address = ("", int(os.getenv("PORT", "8000"))) server_address = ("", int(os.getenv("PORT", "8000")))
httpd = server_class(server_address, WeatherHandler) httpd = server_class(server_address, WeatherHandler)
httpd.serve_forever() httpd.serve_forever()

View File

@ -4,14 +4,27 @@ import urllib.request
@cache @cache
def weather_handler(date: str): def weather_handler(date: str):
print(date) # print(date)
# Request variables
api_link = "https://api.open-meteo.com/v1/forecast?"
coordinates = [48.712, 44.514] coordinates = [48.712, 44.514]
weather_values = "temperature_2m,relative_humidity_2m,rain,snowfall,weather_code,wind_speed_10m,wind_direction_10m"
timezone = "Europe/Moscow"
days = "3"
url = ( url = (
"https://api.open-meteo.com/v1/forecast?latitude=" api_link
+ "latitude="
+ str(coordinates[0]) + str(coordinates[0])
+ "&longitude=" + "&longitude="
+ str(coordinates[1]) + str(coordinates[1])
+ "&current=temperature_2m,relative_humidity_2m,rain,snowfall,weather_code,wind_speed_10m,wind_direction_10m&hourly=temperature_2m,relative_humidity_2m,rain,snowfall,weather_code,wind_speed_10m,wind_direction_10m&timezone=Europe/Moscow&forecast_days=3" + "&current="
+ weather_values
+ "&hourly="
+ weather_values
+ "&timezone="
+ timezone
+ "&forecast_days="
+ days
) )
raw_data = urllib.request.urlopen(url).read() raw_data = urllib.request.urlopen(url).read()
return raw_data return raw_data