-
Notifications
You must be signed in to change notification settings - Fork 1
/
freezer.py
96 lines (81 loc) · 2.9 KB
/
freezer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/python
# -*- coding: utf8 -*-
import os
import httplib2
import urllib2
import pprint
import json
import codecs
from apiclient.discovery import build
from oauth2client.client import OAuth2WebServerFlow
import pystache
import views
# Copy your credentials from the APIs Console
CLIENT_ID = '110358685167-hcaeade6erniobnfmm1t90sahcdoqa2c.apps.googleusercontent.com'
CLIENT_SECRET = '7XwCrSOXPmKmPZrRM2HNYe60'
# Check https://developers.google.com/drive/scopes for all available scopes
OAUTH_SCOPE = 'https://www.googleapis.com/auth/drive'
try:
res=json.load(open("list.json"))
except IOError:
# Run through the OAuth flow and retrieve credentials
flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE)
authorize_url = flow.step1_get_authorize_url('urn:ietf:wg:oauth:2.0:oob')
print 'Go to the following link in your browser: ' + authorize_url
code = raw_input('Enter verification code: ').strip()
credentials = flow.step2_exchange(code)
# Create an httplib2.Http object and authorize it with our credentials
http = httplib2.Http()
http = credentials.authorize(http)
drive_service = build('drive', 'v2', http=http)
list = drive_service.files().list(maxResults=1000)
res = list.execute()
f=open("list.json","w")
json.dump(res,f)
f.close()
docs = {}
folders = {}
for i in res.get('items',[]):
print "looping on: %s" % i['title']
mime_type = i['mimeType']
if mime_type == "application/vnd.google-apps.folder":
folders[i['id']] = i
else:
el = i.get('exportLinks', {})
if i['labels']['starred'] and el.has_key('text/html'):
# if el.has_key('text/html'):
docs[i['id']] = i
out = []
for id,d in docs.items():
url = d['exportLinks']['text/html']
file_name = "%s.html" % id
if not os.path.isfile("build/"+file_name):
html = urllib2.urlopen(url).read()
out_f = open("build/"+file_name,"w")
out_f.write(html)
out_f.close()
d_out = dict (title = d['title'],
url = file_name,
source_url = d['alternateLink'],
)
parents = d.get('parents', [])
for p in parents:
p_id = p['id']
if not folders.has_key(p_id):
continue
try:
out_index = folders[p_id].get('out_index', False)
except KeyError:
print "couldn't find %s in `folders`" % p_id
if not out_index:
folders[p_id]['out_index'] = out_index = len(out)
out.append(dict(title=folders[p_id]['title'],
children=[],
)
)
out[out_index]['children'].append(d_out)
doc_view = views.Doc(out)
renderer = pystache.Renderer()
# index = codecs.open('build/index.html', encoding='utf-8', mode='w')
index = open('build/index.html', mode='w')
index.write(renderer.render(doc_view).encode('utf8'))