-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweatherapi.py
76 lines (64 loc) · 2.19 KB
/
weatherapi.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
from pprint import pprint
import requests
from dotenv import load_dotenv, find_dotenv
from pathlib import Path
import os
load_dotenv(Path(".env"))
api_key = os.getenv("WEATHER_API")
base_url = 'https://api.weatherapi.com/v1'
def astronomy(location, date):
endpoint = f'/astronomy.json?key={api_key}&q={location}&dt={date}'
url = base_url + endpoint
response = requests.get(url)
data = response.json()
pprint(data)
def forecast_weather(location, days, date, unixdt, hour, lang):
endpoint = f'/forecast.json?key={api_key}&q={location}&days={days}&dt={date}&unixdt={unixdt}&hour={hour}&lang={lang}'
url = base_url + endpoint
response = requests.get(url)
data = response.json()
pprint(data)
x = data['location']
y = data['current']
pprint(x)
try:
with open('data/location.txt', 'wt') as file:
file.write(str(x))
except:
print("Unable to write to file")
try:
with open('data/attributes.txt', 'wt') as file:
file.write(str(y))
except:
print("Unable to write to file")
lat = x['lat']
lng = x['lon']
response = requests.get('https://api.stormglass.io/v2/bio/point', params={'lat': lat, 'lng': lng, 'params': 'soilMoisture'},
headers={'Authorization': 'f6088ca4-1674-11ee-86b2-0242ac130002-f6088d12-1674-11ee-86b2-0242ac130002'})
json_data = response.json()
# print(json_data)
try:
with open('data/full_info.txt', 'wt') as file:
file.write(str(data) + str(json_data))
except:
print("Unable to write to file")
def future_weather(location, date, lang):
endpoint = f'/forecast.json?key={api_key}&q={location}&dt={date}&lang={lang}'
url = base_url + endpoint
response = requests.get(url)
data = response.json()
pprint(data)
def realtime_weather(location, lang):
endpoint = f'/current.json?key={api_key}&q={location}&lang={lang}'
url = base_url + endpoint
response = requests.get(url)
data = response.json()
pprint(data)
# Usage
q = 'vit chennai'
dt = '2023-07-06'
lang = 'lang_example'
astronomy(q, dt)
forecast_weather(q, 56, dt, 56, 56, lang)
future_weather(q, dt, lang)
realtime_weather(q, lang)