From da622c04be457143011ac8b9558f9cade2433330 Mon Sep 17 00:00:00 2001 From: lzzy12 Date: Thu, 7 Nov 2019 14:48:35 +0530 Subject: [PATCH] Don't depend on bot to run for generate_gdrive_token.py --- bot/helper/mirror_utils/download_tools.py | 2 +- generate_drive_token.py | 25 ++++++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/bot/helper/mirror_utils/download_tools.py b/bot/helper/mirror_utils/download_tools.py index 80d9f19..fd35cf6 100644 --- a/bot/helper/mirror_utils/download_tools.py +++ b/bot/helper/mirror_utils/download_tools.py @@ -42,7 +42,7 @@ class DownloadHelper: if self.__is_torrent: # Waiting for the actual gid download = self.__get_download() - while download.is_complete != True: # Check every few seconds + while download.is_complete: # Check every few seconds status_list = get_download_status_list() index = get_download_index(status_list, self.__get_download().gid) download = self.__get_download() diff --git a/generate_drive_token.py b/generate_drive_token.py index 9573197..6683d7f 100644 --- a/generate_drive_token.py +++ b/generate_drive_token.py @@ -1,4 +1,23 @@ -from bot.helper.mirror_utils.gdriveTools import GoogleDriveHelper +import pickle +import os +from google_auth_oauthlib.flow import InstalledAppFlow +from google.auth.transport.requests import Request -drive = GoogleDriveHelper() -drive.authorize() \ No newline at end of file + +credentials = None +__G_DRIVE_TOKEN_FILE = "token.pickle" +__OAUTH_SCOPE = "https://www.googleapis.com/auth/drive.file" +if os.path.exists(__G_DRIVE_TOKEN_FILE): + with open(__G_DRIVE_TOKEN_FILE, 'rb') as f: + credentials = pickle.load(f) + if credentials is None or not credentials.valid: + if credentials and credentials.expired and credentials.refresh_token: + credentials.refresh(Request()) + else: + flow = InstalledAppFlow.from_client_secrets_file( + 'credentials.json', __OAUTH_SCOPE) + credentials = flow.run_console(port=0) + + # Save the credentials for the next run +with open(__G_DRIVE_TOKEN_FILE, 'wb') as token: + pickle.dump(credentials, token) \ No newline at end of file