1 |
# Facebook |
2 |
# |
3 |
# Douglas Thrift |
4 |
# |
5 |
# $Id$ |
6 |
|
7 |
import atom |
8 |
from datetime import datetime |
9 |
import gdata.calendar |
10 |
import icalendar |
11 |
import pytz |
12 |
import re |
13 |
import urllib, urllib2 |
14 |
import warnings |
15 |
|
16 |
def main(calendar, config, debug): |
17 |
uid = re.compile(r'^e(.*)@facebook\.com$') |
18 |
pacific = pytz.timezone('US/Pacific') |
19 |
reminder = gdata.calendar.Reminder(hours = 2) |
20 |
|
21 |
with warnings.catch_warnings(): |
22 |
warnings.filterwarnings('ignore', r'object\.__init__\(\) takes no parameters', DeprecationWarning) |
23 |
|
24 |
for event in icalendar.Calendar.from_string(urllib2.urlopen('http://www.facebook.com/ical/u.php?' + urllib.urlencode({'uid': config.get('uid'), 'key': config.get('key')})).read()).walk('vevent'): |
25 |
id = uid.match(event.decoded('uid')).group(1) |
26 |
query = gdata.calendar.service.CalendarEventQuery('default', 'private', 'full', id) |
27 |
entries = calendar.CalendarQuery(query).entry |
28 |
title = event.decoded('summary') |
29 |
content = event.decoded('description') |
30 |
status = event.decoded('partstat') |
31 |
transparency = 'OPAQUE' if status == 'ACCEPTED' else 'TRANSPARENT' |
32 |
start_time = pacific.localize(event.decoded('dtstart')) |
33 |
end_time = pacific.localize(event.decoded('dtend')) |
34 |
where = event.decoded('location') |
35 |
|
36 |
eid = re.compile(r'http://www\.facebook\.com/event\.php\?eid=%s$' % re.escape(id)) |
37 |
|
38 |
if not eid.search(content): |
39 |
content += '\n\nhttp://www.facebook.com/event.php?eid=%s' % id |
40 |
|
41 |
if not entries: |
42 |
entry = gdata.calendar.CalendarEventEntry() |
43 |
entry.title = atom.Title(text = title) |
44 |
entry.content = atom.Content(text = content) |
45 |
entry.transparency = gdata.calendar.Transparency() |
46 |
entry.transparency.value = transparency |
47 |
entry.visibility = gdata.calendar.Visibility() |
48 |
entry.visibility.value = 'CONFIDENTIAL' |
49 |
|
50 |
entry.when.append(gdata.calendar.When(start_time = start_time.isoformat(), end_time = end_time.isoformat())) |
51 |
|
52 |
if status != 'NEEDS-ACTION': |
53 |
entry.when[0].reminder.append(reminder) |
54 |
|
55 |
entry.where.append(gdata.calendar.Where(value_string = where)) |
56 |
calendar.InsertEvent(entry, '/calendar/feeds/default/private/full') |
57 |
else: |
58 |
for index, entry in enumerate(filter(lambda entry: eid.search(entry.content.text), entries)): |
59 |
if index == 0: |
60 |
entry.title.text = title |
61 |
entry.content.text = content |
62 |
entry.transparency.value = transparency |
63 |
entry.visibility.value = 'CONFIDENTIAL' |
64 |
entry.when[0].start_time = start_time.isoformat() |
65 |
entry.when[0].end_time = end_time.isoformat() |
66 |
|
67 |
if status != 'NEEDS-ACTION': |
68 |
if entry.when[0].reminder: |
69 |
entry.when[0].reminder[0] = reminder |
70 |
else: |
71 |
entry.when[0].reminder.append(reminder) |
72 |
|
73 |
calendar.UpdateEvent(entry.GetEditLink().href, entry) |
74 |
else: |
75 |
calendar.DeleteEvent(entry.GetEditLink().href) |