Initial commit
[kiosk.git] / gdocs_renderer.py
1 import file_writer
2 import renderer
3 import re
4 import sets
5 import gdata_oauth
6 import secrets
7
8 class gdocs_renderer(renderer.debuggable_abstaining_renderer):
9     """A renderer to fetches and munge docs from drive.google.com"""
10
11     query = 'title="Grocery (go/grocery)" OR title="Costco List (go/costco)"'
12
13     def __init__(self, name_to_timeout_dict, oauth):
14         super(gdocs_renderer, self).__init__(name_to_timeout_dict, False)
15         self.oauth = oauth
16         self.client = self.oauth.docs_service()
17
18     def debug_prefix(self):
19         return "gdocs"
20
21     def periodic_render(self, key):
22         result = []
23         page_token = None
24         while True:
25             try:
26                 param = {}
27                 if page_token:
28                     param['pageToken'] = page_token
29                 param['q'] = self.query
30                 print "QUERY: %s" % param['q']
31
32                 files = self.client.files().list(**param).execute()
33                 result.extend(files['items'])
34                 page_token = files.get('nextPageToken')
35                 if not page_token:
36                     break
37             except:
38                 print("********* TRYING TO REFRESH GDOCS CLIENT *********")
39                 self.oauth.refresh_token()
40                 self.client = self.oauth.docs_service()
41                 return False
42
43         def boost_font_size(matchobj):
44             x = int(matchobj.group(1))
45             x *= 2.33
46             x = int(x)
47             return "font-size:%dpt" % (x)
48
49         for f in result:
50             print f['title']
51             print f['id']
52             self.debug_print("%s (%s)\n" % (f['title'], f['id']))
53             title = f['title']
54             url = f['exportLinks']['text/html']
55             print f
56             print "Fetching %s..." % url
57             resp, contents = self.client._http.request(url)
58             print resp.status
59             print contents
60             if resp.status == 200:
61                 print "Got contents."
62                 contents = re.sub('<body class="..">', '', contents)
63                 contents = contents.replace('</body>', '')
64                 contents = re.sub('font-size:([0-9]+)pt', boost_font_size, contents)
65                 f = file_writer.file_writer('%s_2_3600.html' % title)
66                 now = datetime.datetime.now()
67                 f.write("""
68 <H1>%s</H1>
69 <!-- Last updated at %s -->
70 <HR>
71 <DIV STYLE="-webkit-column-count: 2; -moz-column-count: 2; column-count: 2;">
72 %s
73 </DIV>""" % (title, now, contents))
74                 f.close()
75             else:
76                 self.debug_print("error: %s" % resp)
77                 return False
78         return True
79
80
81 #oauth = gdata_oauth.OAuth(secrets.google_client_id,
82 #                          secrets.google_client_secret)
83 #x = gdocs_renderer({"Testing", 12345},
84 #                   oauth)
85 #x.periodic_render("Test")