Code Restructure, Merging CGI
[frenchie/icalparse.git] / fbcal.py
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2011 James French <[email protected]>
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 # THE SOFTWARE.
22
23 import sys
24 import cgi
25 import vobject
26 import icalparse
27 import re
28 #import cgitb; cgitb.enable()
29
30 def exitQuiet(exitstate=0):
31         print('Content-Type: text/html\n')
32         sys.exit(exitstate)
33
34 if __name__ == '__main__':
35         form = cgi.FieldStorage()
36         if "uid" not in form or "key" not in form:
37                 print('Content-Type: text/html\n')
38                 sys.exit(0)
39         try:
40                 # UID should be numeric, if it's not we have someone playing games
41                 uid = int(form['uid'].value)
42         except:
43                 exitQuiet()
44
45         # The user's key will be a 16 character string
46         key = form['key'].value
47         re.search('[&?]+', key) and exitQuiet()
48         len(key) == 16 or exitQuiet()
49         
50         # Historically facebook has been notoriously bad at setting timzeones
51         # in their stuff so this should be a user setting. If it is set in
52         # their calendar it'll  be used otherwise if the user feeds crap or
53         # nothing just assume they want Australia/Perth
54         tz = ""
55         if "tz" in form:
56                 from pytz import timezone
57                 try:
58                         timezone(form['tz'].value)
59                         tz = form['tz'].value
60                 except: pass
61         
62         ruleConfig = {}
63         ruleConfig["defaultTZ"] = tz or "Australia/Perth"
64                                 
65         # Okay, we're happy that the input is sane, lets serve up some data
66         url = 'http://www.facebook.com/ical/u.php?uid=%d&key=%s'%(uid,key)
67         (content, encoding) = icalparse.getHTTPContent(url)
68
69         cal = vobject.readOne(unicode(content, encoding))
70         cal = icalparse.applyRules(cal, icalparse.generateRules(ruleConfig), False)
71
72         print('Content-Type: text/calendar; charset=%s\n'%encoding)
73         icalparse.writeOutput(cal)

UCC git Repository :: git.ucc.asn.au