|
43 | 43 | from distutils import util |
44 | 44 | import calendar |
45 | 45 |
|
46 | | -from oauth2client import file, client, tools |
47 | | -from googleapiclient.discovery import build |
48 | | -from googleapiclient.http import MediaIoBaseUpload |
49 | | -from googleapiclient.errors import HttpError |
50 | | -from httplib2 import Http |
51 | | -from io import BytesIO |
52 | | - |
53 | 46 | # Classes |
54 | 47 | class Birthday: |
55 | 48 | def __init__(self, uid, name, day, month): |
@@ -98,12 +91,6 @@ def main(): |
98 | 91 |
|
99 | 92 | logger.info(f'Logging level set to: {logging.getLevelName(logger.level)}') |
100 | 93 |
|
101 | | - # Authenticate with Google API early |
102 | | - if util.strtobool(config['DRIVE']['UPLOAD_TO_DRIVE']): |
103 | | - logger.info('Authenticating with Google Drive API...') |
104 | | - service = google_drive_api_authenticate() |
105 | | - logger.info('Successfully authenticated with Google Drive API.') |
106 | | - |
107 | 94 | # Init browser |
108 | 95 | browser = mechanicalsoup.StatefulBrowser() |
109 | 96 | init_browser(browser) |
@@ -143,37 +130,6 @@ def main(): |
143 | 130 | ics_file.write(ics_str) |
144 | 131 | logger.info(f'Successfully saved ICS file to {os.path.abspath(config["FILESYSTEM"]["ICS_FILE_PATH"])}') |
145 | 132 |
|
146 | | - # Upload to drive |
147 | | - if util.strtobool(config['DRIVE']['UPLOAD_TO_DRIVE']): |
148 | | - logger.info('Uploading ICS file to Google Drive...') |
149 | | - metadata = {'name': config['DRIVE']['ICS_FILE_NAME']} |
150 | | - UPLOAD_RETRY_ATTEMPTS = 3 |
151 | | - uploaded_successfully = False |
152 | | - |
153 | | - for attempt in range(UPLOAD_RETRY_ATTEMPTS): |
154 | | - try: |
155 | | - updated_file = upload_and_replace_file(service, config['DRIVE']['DRIVE_FILE_ID'], metadata, bytearray(ics_str, 'utf-8')) # Pass payload as bytes |
156 | | - config.set('DRIVE', 'DRIVE_FILE_ID', updated_file['id']) |
157 | | - uploaded_successfully = True |
158 | | - except HttpError as e: |
159 | | - if e.resp.status == 404: # file not found |
160 | | - if config['DRIVE']['DRIVE_FILE_ID']: |
161 | | - logger.warning(f'{e}. Resetting stored file id in config and trying again. Attempt: {attempt+1}') |
162 | | - config.set('DRIVE', 'DRIVE_FILE_ID', '') # reset stored file_id |
163 | | - continue |
164 | | - else: |
165 | | - logger.error(e) |
166 | | - raise SystemError |
167 | | - else: |
168 | | - logger.error(e) |
169 | | - raise SystemError |
170 | | - |
171 | | - if uploaded_successfully: |
172 | | - logger.info(f'Successfully uploaded {config["DRIVE"]["ICS_FILE_NAME"]} to Google Drive with file id: {config["DRIVE"]["DRIVE_FILE_ID"]}\nDirect download link: http://drive.google.com/uc?export=download&id={config["DRIVE"]["DRIVE_FILE_ID"]}') |
173 | | - else: |
174 | | - logger.error(f'Failed to upload {config["DRIVE"]["ICS_FILE_NAME"]} to Google Drive after {UPLOAD_RETRY_ATTEMPTS} attempts.') |
175 | | - raise SystemError |
176 | | - |
177 | 133 | # Update config file with updated file id for subsequent runs |
178 | 134 | logger.info('Saving changes to config file...') |
179 | 135 | with open(CONFIG_FILE_PATH, 'w') as configfile: |
@@ -262,41 +218,6 @@ def facebook_authenticate(browser, email, password): |
262 | 218 | logger.error(f'Hit Facebook security checkpoint. Please login to Facebook manually and follow prompts to authorize this device.') |
263 | 219 | raise SystemError |
264 | 220 |
|
265 | | -def google_drive_api_authenticate(): |
266 | | - """ Authenticate with Google Drive Api """ |
267 | | - |
268 | | - # Confirm credentials.json exists |
269 | | - if not os.path.isfile('credentials.json'): |
270 | | - logger.error(f'credentials.json file does not exist') |
271 | | - raise SystemExit |
272 | | - |
273 | | - SCOPES = 'https://www.googleapis.com/auth/drive.file' |
274 | | - store = file.Storage('token.json') |
275 | | - creds = store.get() |
276 | | - if not creds or creds.invalid: |
277 | | - flow = client.flow_from_clientsecrets('credentials.json', SCOPES) |
278 | | - creds = tools.run_flow(flow, store) |
279 | | - service = build('drive', 'v3', http=creds.authorize(Http()), cache_discovery=False) |
280 | | - return service |
281 | | - |
282 | | -def upload_and_replace_file(service, file_id, metadata, payload): |
283 | | - mine_type = 'text/calendar' |
284 | | - text_stream = BytesIO(payload) |
285 | | - media_body = MediaIoBaseUpload(text_stream, mimetype=mine_type, chunksize=1024*1024, resumable=True) |
286 | | - |
287 | | - # If file id is provided, update the file, otherwise we'll create a new file |
288 | | - if file_id: |
289 | | - updated_file = service.files().update(fileId=file_id, body=metadata, media_body=media_body).execute() |
290 | | - else: |
291 | | - updated_file = service.files().create(body=metadata, media_body=media_body).execute() |
292 | | - |
293 | | - # Need publically accessible ics file so third party tools can read from it publically |
294 | | - permission = { "role": 'reader', |
295 | | - "type": 'anyone'} |
296 | | - service.permissions().create(fileId=updated_file['id'], body=permission).execute() |
297 | | - |
298 | | - return updated_file |
299 | | - |
300 | 221 | __cached_async_token = None |
301 | 222 | def get_async_token(browser): |
302 | 223 | """ Get async authorization token (CSRF protection token) that must be included in all async requests """ |
|
0 commit comments