-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetcher.py
143 lines (129 loc) · 4.89 KB
/
fetcher.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from database import Document, create_db, User
from apiclient.discovery import build
from arrow.arrow import parser
from apiclient import errors
from oauth2client.contrib.multistore_file import (
get_all_credential_keys, get_credential_storage)
import funcy
import httplib2
import logging
import time
import log as log_module
log_module.configure_logging()
log = logging.getLogger("fetcher")
datetime_parser = parser.DateTimeParser()
def get_documents(credentials, google_id):
"""Get documents from google drive for user.
Arguments:
credentials -- to access the Drive API
google_id -- google id for authorized user
"""
log.info('get documents')
auth_code = credentials.authorize(httplib2.Http())
drive_service = build('drive', 'v2', http=auth_code)
files_resource = drive_service.files()
page_token = None
while True:
try:
param = {}
if page_token:
param['pageToken'] = page_token
files = files_resource.list(**param).execute()
for file_ in files['items']:
yield Document(
owner=file_['owners'][0]['displayName'],
creation_time=datetime_parser.parse_iso(
file_['createdDate']),
modification_time=datetime_parser.parse_iso(
file_['modifiedDate']),
header=file_['title'],
link=file_.get('downloadUrl', '...'),
last_modification_author=file_['lastModifyingUserName'],
is_public_access=file_['shared'],
type_access=u'1',
permission_access=file_['userPermission']['role'],
google_code_id=google_id
)
page_token = files.get('nextPageToken')
if not page_token:
break
except errors.HttpError as error:
print 'An error occurred: %s' % error
break
db = create_db()
@funcy.once
def set_trace():
import pdb
pdb.set_trace()
def main():
count = 0
count_files = 0
while True:
log.info("fetcher iteration")
time.sleep(5)
credential_keys = get_all_credential_keys('test_storage.txt')
for key in credential_keys:
user_info = db.query(User).filter(
User.google_id == key['clientId']).first()
if not user_info:
user_info = User(
google_id=key['clientId'],
is_download_data=False)
log.info(
'add new user %s'
' in User table', key['clientId'])
db.add(user_info)
db.commit()
log.info(
'getting documents for user %s',
key['clientId'])
user_info = db.query(User).filter(
User.google_id == key['clientId']).first()
log.info(
'is_download_data = %s',
user_info.is_download_data)
if not user_info.is_download_data:
log.info(
'is_download_data = %s',
user_info.is_download_data)
log.info(
'download files from google id %s',
key['clientId'])
storage = get_credential_storage(
'test_storage.txt',
key['clientId'],
key['userAgent'],
key['scope'])
credentials = storage.get()
log.info(
'get credential for google id %s',
key['clientId'])
while credentials and count == 0:
for document in get_documents(credentials,
key['clientId']):
db.add(document)
count_files = count_files + 1
db.commit()
count = count + 1
break
log.info('download %d files from google id %s',
count_files, key['clientId'])
user_info = db.query(User).filter(
User.google_id == key['clientId']).first()
if user_info:
if not user_info.is_download_data:
user_info.is_download_data = True
log.info(
'download files for user %s'
' is_download_data = %d',
str(key['clientId']),
user_info.is_download_data)
db.commit()
count_files = 0
count = 0
if __name__ == "__main__":
while True:
try:
main()
except Exception:
log.exception("uncaught error")