diff --git a/file_automation/remote/google_drive/create_token.py b/file_automation/remote/google_drive/create_token.py deleted file mode 100644 index ed2eba6..0000000 --- a/file_automation/remote/google_drive/create_token.py +++ /dev/null @@ -1,17 +0,0 @@ -from pathlib import Path - -from google_auth_oauthlib.flow import InstalledAppFlow - -from file_automation.utils.exception.exception_tags import token_is_exist - - -def create_token(credentials_path: str): - scopes = ["https://www.googleapis.com/auth/drive"] - token_path = Path(Path.cwd(), "token.json") - if token_path.exists(): - print(token_is_exist, file=sys.stderr) - flow = InstalledAppFlow.from_client_secrets_file( - str(credentials_path), scopes) - creds = flow.run_local_server(port=0) - with open(str(token_path), 'w') as token: - token.write(creds.to_json()) diff --git a/file_automation/remote/google_drive/dir/__init__.py b/file_automation/remote/google_drive/dir/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/file_automation/remote/google_drive/download/__init__.py b/file_automation/remote/google_drive/download/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/file_automation/remote/google_drive/driver_instance.py b/file_automation/remote/google_drive/driver_instance.py index 8b13789..08f556c 100644 --- a/file_automation/remote/google_drive/driver_instance.py +++ b/file_automation/remote/google_drive/driver_instance.py @@ -1 +1,43 @@ +from pathlib import Path +from google.auth.transport.requests import Request +from google.oauth2.credentials import Credentials +from google_auth_oauthlib.flow import InstalledAppFlow +from googleapiclient.discovery import build +from googleapiclient.errors import HttpError + + +class GoogleDrive(object): + + def __init__(self, token_path: str, credentials_path: str): + self.google_drive_instance = None + self.creds = None + self.service = None + self.scopes = ["https://www.googleapis.com/auth/drive"] + token_path = Path(token_path) + credentials_path = Path(credentials_path) + creds = None + # The file token.json stores the user's access and refresh tokens, and is + # created automatically when the authorization flow completes for the first + # time. + if token_path.exists(): + creds = Credentials.from_authorized_user_file(str(token_path), self.scopes) + # If there are no (valid) credentials available, let the user log in. + if not creds or not creds.valid: + if creds and creds.expired and creds.refresh_token: + creds.refresh(Request()) + else: + flow = InstalledAppFlow.from_client_secrets_file( + str(credentials_path), self.scopes) + creds = flow.run_local_server(port=0) + # Save the credentials for the next run + with open(str(token_path), 'w') as token: + token.write(creds.to_json()) + + try: + self.service = build('drive', 'v3', credentials=creds) + except HttpError as error: + print(f'An error occurred: {error}') + + +GoogleDrive(str(Path(Path.cwd(), "token.json")), str(Path(Path.cwd(), "credentials.json"))) diff --git a/file_automation/remote/google_drive/file_field/__init__.py b/file_automation/remote/google_drive/file_field/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/file_automation/remote/google_drive/login_google_drive.py b/file_automation/remote/google_drive/login_google_drive.py deleted file mode 100644 index 3b72862..0000000 --- a/file_automation/remote/google_drive/login_google_drive.py +++ /dev/null @@ -1,59 +0,0 @@ -from __future__ import print_function - -from pathlib import Path - -from google.auth.transport.requests import Request -from google.oauth2.credentials import Credentials -from google_auth_oauthlib.flow import InstalledAppFlow -from googleapiclient.discovery import build -from googleapiclient.errors import HttpError - -# If modifying these scopes, delete the file token.json. -SCOPES = ["https://www.googleapis.com/auth/drive"] -token_path = Path(Path.cwd(), "token.json") -credentials_path = Path(Path.cwd(), "credentials.json") - - -def main(): - """Shows basic usage of the Drive v3 API. - Prints the names and ids of the first 10 files the user has access to. - """ - creds = None - # The file token.json stores the user's access and refresh tokens, and is - # created automatically when the authorization flow completes for the first - # time. - if token_path.exists(): - creds = Credentials.from_authorized_user_file(str(token_path), SCOPES) - # If there are no (valid) credentials available, let the user log in. - if not creds or not creds.valid: - if creds and creds.expired and creds.refresh_token: - creds.refresh(Request()) - else: - flow = InstalledAppFlow.from_client_secrets_file( - str(credentials_path), SCOPES) - creds = flow.run_local_server(port=0) - # Save the credentials for the next run - with open(str(token_path), 'w') as token: - token.write(creds.to_json()) - - try: - service = build('drive', 'v3', credentials=creds) - - # Call the Drive v3 API - results = service.files().list( - pageSize=10, fields="nextPageToken, files(id, name)").execute() - items = results.get('files', []) - - if not items: - print('No files found.') - return - print('Files:') - for item in items: - print(u'{0} ({1})'.format(item['name'], item['id'])) - except HttpError as error: - # TODO(developer) - Handle errors from drive API. - print(f'An error occurred: {error}') - - -if __name__ == '__main__': - main() diff --git a/file_automation/remote/google_drive/manager/__init__.py b/file_automation/remote/google_drive/manager/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/file_automation/remote/google_drive/search/__init__.py b/file_automation/remote/google_drive/search/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/file_automation/remote/google_drive/upload/__init__.py b/file_automation/remote/google_drive/upload/__init__.py new file mode 100644 index 0000000..e69de29