-
Notifications
You must be signed in to change notification settings - Fork 0
/
mos_caladd.py
executable file
·91 lines (73 loc) · 2.88 KB
/
mos_caladd.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
#!/usr/bin/env python
# script for adding recurring events to the MOS calendar
# Metalab Operating System (https://github.com/Metalab/mos)
import dateutil.rrule as dr
import dateutil.parser as dparse
import dateutil.relativedelta as drel
import sys
import urllib, urllib2, getpass, time
from cookielib import CookieJar
from BeautifulSoup import BeautifulSoup
event = {
"category": "",
"location": "1", #1: Hauptraum, 2: Bibliothek, 7: Lounge (Raucherbetrieb),
# 5: *.*, 6: Woanders, 8: Whateverlab, 9: Kueche, 11: Fotolab,
# 12: any room, 13: heavy machinery, 14: Lounge (Nichtraucherbetrieb)
"name": "FunkFeuer",
"teaser": "Montagstreffen",
"wikiPage": "FunkFeuer",
"time": ['19:00', "21:00"], # ['start', 'end']
"date": {
"freq": dr.WEEKLY,
"byweekday": drel.MO(0),
"dtstart": dparse.parse("2013-9-1"),
"until": dparse.parse("2013-10-1")
}
}
BASEURL = 'https://metalab.at'
dates = dr.rrule(**event['date'])
print """Adding new events to calendar:
Name:\t\t%s
Teaser:\t%s
wikiPage:\t%s
time:\t\t%s to %s
dates:""" %( event['name'], event['teaser'],
event['wikiPage'], event['time'][0], event['time'][1])
print '\t'+'\n\t'.join([d.strftime('%a, %d.%m.%Y') for d in dates])
posthandler = urllib2.build_opener(urllib2.HTTPCookieProcessor(CookieJar()))
content = posthandler.open(BASEURL+'/member/login/').read()
soup = BeautifulSoup(content)
csrftoken= soup.findAll('input', attrs={'name': "csrfmiddlewaretoken"})[0].get("value")
print '\nusername: ',
username = sys.stdin.readline().rstrip('\n')
password = getpass.getpass('password: ')
data = { 'next' : '',
'csrfmiddlewaretoken' : csrftoken,
'password': password,
'username': username}
posthandler.addheaders = [('Referer', BASEURL+'/member/login/')]
content = posthandler.open(BASEURL+'/member/login/', data=urllib.urlencode(data)).read()
if ('Please try again' in content):
print 'Your username and password didn\'t match. Please try again.'
sys.exit(1)
print 'login successful, adding events:'
soup = BeautifulSoup(content)
csrftoken= soup.findAll('input', attrs={'name': "csrfmiddlewaretoken"})[0].get("value")
eventdata = {
"category": event['category'],
"endDate_1": event['time'][1],
"location": event['location'],
"name": event['name'],
"startDate_1": event['time'][0],
"teaser": event['teaser'],
"wikiPage": event['wikiPage'],
'csrfmiddlewaretoken' : csrftoken
}
for d in dates:
eventdata["startDate_0"] = d.strftime('%Y-%m-%d')
eventdata["endDate_0"] = d.strftime('%Y-%m-%d')
print ' adding %s...' % d.strftime('%Y-%m-%d'),
posthandler.open(BASEURL+'/calendar/new/', data=urllib.urlencode(eventdata)).read()
print 'done.'
time.sleep(0.1)
print 'added all events.'