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 |
if not entries: |
37 |
entry = gdata.calendar.CalendarEventEntry() |
38 |
entry.title = atom.Title(text = title) |
39 |
entry.content = atom.Content(text = content) |
40 |
entry.transparency = gdata.calendar.Transparency() |
41 |
entry.transparency.value = transparency |
42 |
entry.visibility = gdata.calendar.Visibility() |
43 |
entry.visibility.value = 'CONFIDENTIAL' |
44 |
|
45 |
entry.when.append(gdata.calendar.When(start_time = start_time.isoformat(), end_time = end_time.isoformat())) |
46 |
|
47 |
if status != 'NEEDS-ACTION': |
48 |
entry.when[0].reminder.append(reminder) |
49 |
|
50 |
entry.where.append(gdata.calendar.Where(value_string = where)) |
51 |
calendar.InsertEvent(entry, '/calendar/feeds/default/private/full') |
52 |
else: |
53 |
eid = re.compile(r'http://www\.facebook\.com/event\.php\?eid=%s$' % re.escape(id)) |
54 |
|
55 |
for index, entry in enumerate(filter(lambda entry: eid.search(entry.content.text), entries)): |
56 |
if index == 0: |
57 |
entry.title.text = title |
58 |
entry.content.text = content |
59 |
entry.transparency.value = transparency |
60 |
entry.visibility.value = 'CONFIDENTIAL' |
61 |
entry.when[0].start_time = start_time.isoformat() |
62 |
entry.when[0].end_time = end_time.isoformat() |
63 |
|
64 |
if status != 'NEEDS-ACTION': |
65 |
if entry.when[0].reminder: |
66 |
entry.when[0].reminder[0] = reminder |
67 |
else: |
68 |
entry.when[0].reminder.append(reminder) |
69 |
|
70 |
calendar.UpdateEvent(entry.GetEditLink().href, entry) |
71 |
else: |
72 |
calendar.DeleteEvent(entry.GetEditLink().href) |