-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsbahnmuc05_weather.py
94 lines (70 loc) · 3.37 KB
/
sbahnmuc05_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
import datetime
import configparser
from pytictoc import TicToc
import pyowm
import boto3
import multiprocessing as mp
import logging
logpath = "/home/ubuntu/sbmd/logs/"
normlogfilename = "sb05log_" \
+ datetime.datetime.now().strftime("%Y-%m-%d_%H-%M") + ".log"
logging.basicConfig(filename=logpath+normlogfilename, level=logging.DEBUG)
def load_weather(c, s3key, s3skey,
owmfile = "/home/ubuntu/sbmd/owm.txt"):
try:
with open(owmfile, "r") as f:
owmapi = f.readline()
owmapi = owmapi.replace("\n", "")
owm = pyowm.OWM(owmapi)
obs = owm.weather_at_place(c + ", DE")
jf = obs.to_JSON()
now = str(datetime.datetime.now()).replace("-", "_")\
.replace(":", "_").replace(" ", "_")
filename = c + "_" + now + "_" + ".json"
with open (filename, "w") as f:
f.write(jf)
s3 = boto3.resource('s3',
aws_access_key_id=s3key,
aws_secret_access_key= s3skey)
bucket = s3.Bucket("sbmd3weather2")
bucket.upload_file(filename, filename)
except Exception as e:
logging.error(c)
logging.error(e)
def main():
pool = mp.Pool(mp.cpu_count())
t = TicToc()
t.tic()
cities = ["München", "Puchheim", "Germering", "Fürstenfeldbruck",
"Olching", "Gröbenzell", "Murnau", "Miesbach", "Gersthofen",
"Wolfratshausen", "Starnberg", "Gernlinden", "Eichstätt",
"Maisach", "Mammendorf", "Schöngeising", "Geltendorf",
"Buchenau", "Eichenau", "Holzkirchen", "Ebersberg", "Grafing",
"Zorneding", "Freising", "Haar", "Wasserburg am Inn",
"Rosenheim", "Augsburg", "Geretsried", "Waldkraiburg",
"Ingolstadt", "Donauwörth", "Unterhaching", "Taufkirchen",
"Erding", "Dachau", "Tutzing", "Feldafing", "Schrobenhausen",
"Mühldorf am Inn", "Deggendorf", "Landsberg", "Landshut",
"Nürnberg", "Grafrath", "Gräfelfing", "Garmisch-Partenkirchen",
"Markt Schwaben", "Icking", "Kempten", "Schliersee",
"Planegg", "Stockdorf", "Gauting",
"Gilching", "Türkenfeld", "Petershausen",
"Röhrmoos", "Hallbergmoos", "Ismaning", "Bayrischzell",
"Unterföhring", "Daglfing", "Unterschleißheim",
"Heimstetten", "Tegernsee", "Lenggries", "Mittenwald",
"Aying", "Vaterstetten", "Baldham", "Steinebach",
"Weßling", "Deisenhofen", "Sauerlach", "Otterfing",
"Kreuzstraße", "Ottobrunn", "Hohenbrunn",
"Oberschleißheim", "Eching", "Neufahrn", "Altomünster",
"Schwabhausen", "Karlsfeld", "Kolbermoor", "Bad Aibling"]
credfile = "/home/ubuntu/sbmd/dwh.cfg"
config = configparser.ConfigParser()
config.read(credfile)
s3k = config['AWS']['KEY']
s3ks = config['AWS']['SECRET']
[pool.apply(load_weather, args=(c, s3k, s3ks)) for c in cities]
pool.close()
t.toc()
if __name__ == "__main__":
main()
logging.info("Weather Data loaded succesfull!")