Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,7 @@ cython_debug/
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/

# Google Drive API token
file_automation/remote/google_drive/credentials.json
token.json
2 changes: 1 addition & 1 deletion .idea/FileAutomation.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ description = ""
readme = { file = "README.md", content-type = "text/markdown" }
requires-python = ">=3.8"
license = { text = "MIT" }
dependencies = [
"google-api-python-client",
"google-auth-httplib2",
"google-auth-oauthlib",
"APScheduler"
]
classifiers = [
"Programming Language :: Python :: 3.7",
"Development Status :: 2 - Pre-Alpha",
Expand Down
17 changes: 17 additions & 0 deletions file_automation/remote/google_drive/create_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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())
1 change: 1 addition & 0 deletions file_automation/remote/google_drive/driver_instance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

59 changes: 59 additions & 0 deletions file_automation/remote/google_drive/login_google_drive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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()
1 change: 1 addition & 0 deletions file_automation/utils/exception/exception_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
token_is_exist: str = "token file is exists"
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ description = ""
readme = { file = "README.md", content-type = "text/markdown" }
requires-python = ">=3.8"
license = { text = "MIT" }
dependencies = [
"google-api-python-client",
"google-auth-httplib2",
"google-auth-oauthlib",
"APScheduler"
]
classifiers = [
"Programming Language :: Python :: 3.7",
"Development Status :: 2 - Pre-Alpha",
Expand Down