ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/FreeBSDAdmin/Reminder/facebook.py
Revision: 1196
Committed: 2009-06-25T14:27:22-07:00 (16 years ago) by douglas
Content type: text/x-python
File size: 2771 byte(s)
Log Message:
Stupid facebook! Why must you truncate the description!?

File Contents

# User Rev Content
1 douglas 1179 # Facebook
2     #
3     # Douglas Thrift
4     #
5     # $Id$
6    
7 douglas 1181 import atom
8     from datetime import datetime
9     import gdata.calendar
10 douglas 1179 import icalendar
11 douglas 1181 import pytz
12     import re
13 douglas 1179 import urllib, urllib2
14     import warnings
15    
16     def main(calendar, config, debug):
17 douglas 1181 uid = re.compile(r'^e(.*)@facebook\.com$')
18     pacific = pytz.timezone('US/Pacific')
19     reminder = gdata.calendar.Reminder(hours = 2)
20    
21 douglas 1179 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 douglas 1181 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 douglas 1196 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 douglas 1181 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)

Properties

Name Value
svn:keywords Id