-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathicser_sr.py
executable file
·107 lines (82 loc) · 2.8 KB
/
icser_sr.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
import sys
try:
from icalendar import Calendar, Event
except:
sys.path.append("icalendar-1.2-py2.5.egg")
from icalendar import Calendar, Event
from BeautifulSoup import BeautifulSoup
from urllib import urlopen
from htmldecode import decode_htmlentities
LOCATION = "Supercinema Rovereto"
class EventReader:
provider = ""
doc = None
location = ""
file_path = ""
url = ""
def __init__(self, from_file=False):
if from_file:
f = open(self.file_path)
else:
f = urlopen(self.url)
page = ''.join(f.readlines())
self.doc = BeautifulSoup(page)
f.close()
#----------------------------------------------------------------------
def get_events(self):
"""
virtual method
"""
pass
########################################################################
class SupercinemaRoveretoEventReader(EventReader):
"""
Specialized for www.supercinemarovereto.it
>>> scr = SupercinemaRoveretoEventReader(True)
>>> len([e for e in scr.get_events()])
12
"""
location = LOCATION
file_path = "test/data/rassegne.php"
url = "http://www.supercinemarovereto.it/rassegne.php"
def get_events(self):
from hashlib import md5
ap = self.doc.find('td', {"class": "main"}).findAll('p')
year = 0
for p in ap:
p = decode_htmlentities(p.string).strip()
if not p: continue
if not year:
year = p.split(' ')[-1]
continue
event = Event()
try:
date = p.split(' ')[0].split('/')
desc = ' '.join(p.split(' ')[1:])
#event.add('dtstart', dateStart)
#event.add('dtstamp', dateStart) #maybe it's better to use NOW()
#event.add('dtend', dateEnd)
event.add('location', LOCATION)
event.add('dtstart;value=date', "%s%.2d%.2d" % (int(year),
int(date[1]),int(date[0])))
event.add('summary', desc)
#TODO: add other info like the date!!
md5text = desc
event['uid'] = md5(md5text).hexdigest()+'@supercinemarovereto.it'
yield event
except:
continue
def main():
scr = SupercinemaRoveretoEventReader(True)
events = scr.get_events()
cal = Calendar()
#cal.add('prodid', '-//My calendar product//mxm.dk//')
cal.add('version', '2.0')
for event in events:
cal.add_component(event)
f = open('scr.ics', 'wb')
f.write(cal.as_string())
f.close()
if __name__ == "__main__":
main()