-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheins.py
More file actions
127 lines (102 loc) · 4.05 KB
/
eins.py
File metadata and controls
127 lines (102 loc) · 4.05 KB
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
import urllib.request
import json
import sqlite3
import argparse
from datetime import datetime, timedelta
import logging
from ics import Calendar, Event
muellsorten = {
"gelber": "Gelber Sack",
"rest": "Restmüll",
"bio": "Biomüll",
"papier": "Papier",
"christ": "Christbaum",
}
def main(offset=None, duration=None):
(date, hasChanged) = isModified()
if (hasChanged):
print("Es gibt Neuigkeiten")
updateCalendar(offset, duration)
updateModifcationDate(date)
def updateCalendar(offset=None, duration=None):
calendarfile = "pickups.ics"
pickups = getPickups()
for location in getPickupLocations():
c = Calendar()
for pickup in pickups:
for muellkurz, muelllang in muellsorten.items():
print(pickup['year'] + ' ' + pickup['month'] + ' ' + pickup['day'] + " in " + location['shorthand'])
if muellkurz in pickup.keys():
print(pickup[muellkurz].split(', '))
if location['shorthand'] in pickup[muellkurz].split(', '):
print("habe " + muellkurz + "gefunden")
e = Event()
e.name = muelllang + " in " + location['name']
print(type(e))
e.begin = calcDateTimeBegin(pickup) if (offset==None) else calcDateTimeBegin(pickup, offset)
if(duration == None):
e.make_all_day()
else:
e.duration = timedelta(hours=duration)
c.events.add(e)
calfile = open("pickups" + location['shorthand'] + ".ics", 'w')
calfile.writelines(c.serialize_iter())
calfile.close()
def calcDateTimeBegin(pickup, offset=0):
print(offset)
return datetime(int(pickup['year']), int(pickup['month']), int(pickup['day']))+timedelta(hours=offset)
def isModified():
lastOnlineDate = getLastModified()
if (lastOnlineDate > getLocalModificationDate()):
return (lastOnlineDate, True)
else:
return (lastOnlineDate, False)
def updateModifcationDate(date):
filename = "lastmodified.txt"
import os
f = open(filename, mode='w')
f.write(str(date))
f.close()
def getLocalModificationDate():
try:
return datetime.fromisoformat((open("lastmodified.txt")).read())
except:
return datetime(1970,1,1)
def getLastModified():
url = "https://www.mzvhegau.de/wp-json/flexia/v1/pickups/modified"
rawdate = getFreshData(url)['last_modified']
date = datetime.fromisoformat(rawdate)
return date
def getPickupLocations():
url = "https://www.mzvhegau.de/wp-json/flexia/v2/pickups"
return getFreshData(url)
def getPickups():
url = "https://www.mzvhegau.de/wp-json/flexia/v1/pickups"
return getFreshData(url)
def getFreshData(url):
urlfile = urllib.request.urlopen(
urllib.request.Request(url, headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
}))
jsondata = json.loads(urlfile.read())
return jsondata
def parseUserArguments():
parser = argparse.ArgumentParser(description="Create garbage collection plan ;-)")
parser.add_argument('--offset', type=int, default=None, help='The offset for the event beginning based on 00:00 a.m. local time')
parser.add_argument('--duration', type=int, default=None, help='The event duration')
return parser.parse_args()
def checkDurationValidValue(duration):
if (duration is not None):
if(duration <= 0):
print("Event duration must not be less or equal to 0, ... exit")
exit(1)
def noArugmentIsGivenByUser(args):
return (args.offset is None) and (args.duration is None)
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
args = parseUserArguments()
if noArugmentIsGivenByUser(args):
main()
else:
checkDurationValidValue(args.duration)
main(args.offset, args.duration)