-
Notifications
You must be signed in to change notification settings - Fork 0
/
METARTAF_scraper.py
64 lines (50 loc) · 1.13 KB
/
METARTAF_scraper.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
from datetime import date, timedelta
import requests
KEYPATH = "weather_key.txt"
DBPATH = "weather_db.json"
STATION_ID = 'IPARMA61'
URI = "http://api.weather.com/v2/pws/history/daily?stationId={0}&format=json&numericPrecision=decimal&units=m&date={1}&apiKey={2}"
def main():
global DBPATH
global KEYPATH
global URI
global STATION_ID
key = ""
fd = 0
try:
with open(KEYPATH, 'r') as fd:
key = fd.readline()
except Exception as e:
print("[-] COULDN'T OPEN KEY FILE")
print(e)
exit(-1)
yesterday = date.today() - timedelta(1)
URI = URI.format(STATION_ID, date.strftime(yesterday, "%Y%m%d"), key)
print(URI)
#headers
headers = requests.utils.default_headers()
headers.update(
{
'User-Agent': 'Mozilla/5.0'
}
)
req = 0
try:
req = requests.get(URI, headers=headers)
except Exception as e:
print("[-] REQUEST ERROR")
print(e)
exit(-1)
body = str(req.content)
body = body.split("'")[1]
body = body.replace("\\n", "")
print(body)
try:
with open(DBPATH, 'a') as fd:
fd.write(body)
except Exception as e:
print("[-] Coudn't write to db")
print(e)
exit(-1)
print("[+] Success")
main()