From 4c4b8c3991d8e2d7840e2e037fc6a827ef6c1e6d Mon Sep 17 00:00:00 2001 From: arthur Date: Wed, 11 Jan 2023 13:18:12 +0100 Subject: [PATCH] ajout --- .gitignore | 1 - README.md | 21 ++++++++++++++ app.py | 73 ++++++++++++++++++++++++++---------------------- credentials.json | 1 + nextcloud.json | 1 + token.json | 2 +- 6 files changed, 63 insertions(+), 36 deletions(-) create mode 100644 README.md create mode 100644 credentials.json create mode 100644 nextcloud.json diff --git a/.gitignore b/.gitignore index 12a0762..f79b097 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ nextcloud_auth.json -credentials.json diff --git a/README.md b/README.md new file mode 100644 index 0000000..58f2586 --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ + +# Drive_sync + +Un outil de syncronisation de google photos avec un cloud Nextcloud + + + +## Install + +Pour installer les dépendances, executer: +```bash + pip3 install -r requirement.txt +``` + +## Config + +Tour d'abord, rendez vous dans le ficher ```nextcloud.json``` et entrez vos identifiant, l'url de votre cloud Nextcloud ainsi que le dossier de destination. +## Usage + +Maintenant, exécutez le fichier ```app.py``` et toutes vos photos seront télcharger dans le dossier choisi. + diff --git a/app.py b/app.py index f5e293d..6b7cae8 100644 --- a/app.py +++ b/app.py @@ -29,37 +29,41 @@ from googleapiclient.http import MediaIoBaseDownload # If modifying these scopes, delete the file token.json. -SCOPES = ['https://www.googleapis.com/auth/photoslibrary'] - -def download(next_page=None): - """Shows basic usage of the Docs API. - Prints the title of a sample document. - """ - 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 os.path.exists('token.json'): - creds = Credentials.from_authorized_user_file('token.json', 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( - 'credentials.json', SCOPES) - creds = flow.run_local_server(port=0) - # Save the credentials for the next run - with open('token.json', 'w') as token: - token.write(creds.to_json()) - - with open('nextcloud_auth.json') as next_auth: - data = json.load(next_auth) - nextcloud_user = data['user'] - nextcloud_pass = data['pass'] + +class Connection: + def __init__(self) -> None: + self.SCOPES = ['https://www.googleapis.com/auth/photoslibrary'] + self.creds = None + with open('nextcloud.json') as nextcloud: + data = json.load(nextcloud) + self.nextcloud_user = data['user'] + self.nextcloud_pass = data['pass'] + self.nextcloud_url = data['url'] + self.nextcloud_destination_folder = data['destination_folder'] + + def google_connect(self): + # 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 os.path.exists('token.json'): + self.creds = Credentials.from_authorized_user_file('token.json', self.SCOPES) + # If there are no (valid) credentials available, let the user log in. + if not self.creds or not self.creds.valid: + if self.creds and self.creds.expired and self.reds.refresh_token: + self.creds.refresh(Request()) + else: + flow = InstalledAppFlow.from_client_secrets_file( + 'credentials.json', self.SCOPES) + self.creds = flow.run_local_server(port=0) + # Save the credentials for the next run + with open('token.json', 'w') as token: + token.write(self.creds.to_json()) + + +def download(connection, next_page=None): try: - service = build('photoslibrary', 'v1', credentials=creds, static_discovery=False) + service = build('photoslibrary', 'v1', credentials=connection.creds, static_discovery=False) results = service.mediaItems().list(pageSize=100, pageToken=next_page).execute() @@ -69,11 +73,11 @@ def download(next_page=None): for item in items: print(f"{item['filename']} {item['mimeType']}") - url = "https://homelinuxserver.ddns.net/remote.php/dav/files/arthur/data/Backup/photos/"+item['filename'] + url = connection.nextcloud_url + '/remote.php/dav/files/' + connection.nextcloud_user + '/' + connection.nextcloud_destination_folder + '/' + item['filename'] if "video" in item['mimeType']: - requests.put(url, auth=HTTPBasicAuth(nextcloud_user, nextcloud_pass), data=requests.get(item['baseUrl']+'=dv').content) + requests.put(url, auth=HTTPBasicAuth(connection.nextcloud_user, connection.nextcloud_pass), data=requests.get(item['baseUrl']+'=dv').content) else: - requests.put(url, auth=HTTPBasicAuth(nextcloud_user, nextcloud_pass), data=requests.get(item['baseUrl']+'=d').content) + requests.put(url, auth=HTTPBasicAuth(connection.nextcloud_user, connection.nextcloud_pass), data=requests.get(item['baseUrl']+'=d').content) return next_page @@ -83,8 +87,9 @@ def download(next_page=None): if __name__ == '__main__': - next_page = download() + connection = Connection() + next_page = download(connection) while next_page != 'null': - next_page = download(next_page) + next_page = download(connection, next_page) print('done') # [END docs_quickstart] \ No newline at end of file diff --git a/credentials.json b/credentials.json new file mode 100644 index 0000000..c8cf8da --- /dev/null +++ b/credentials.json @@ -0,0 +1 @@ +{"web":{"client_id":"114734902818-ka0meq495cli7m1ogbs5fampf8s8kgbp.apps.googleusercontent.com","project_id":"drive-sync-1673116306810","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"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","redirect_uris":["https://homelinuxserver.ddns.net"]}} \ No newline at end of file diff --git a/nextcloud.json b/nextcloud.json new file mode 100644 index 0000000..b518292 --- /dev/null +++ b/nextcloud.json @@ -0,0 +1 @@ +{"user": "YOUR USERNAME", "pass": "YOUR PASSWORD", "url": "YOUR NEXTCLOUD URL", "destination_folder": "PHOTO FOLDER"} \ No newline at end of file diff --git a/token.json b/token.json index 3961315..d773edd 100644 --- a/token.json +++ b/token.json @@ -1 +1 @@ -{"token": "ya29.a0AX9GBdU-N2vYtbrra2w9xyrYeDDwIeKkqo0xlISwSwmUpUCtP9bn40dEOEyYJow67UIqPocjkDHFqRINTPCBr28pwFBteTLccPLbp4ZPuVYEugcyO-wixxDy9q0Mp0zk60AFKOFRajos9U5iPmNGGyzCKaGwdpzsaCgYKAf4SAQASFQHUCsbCg9W0B-CJ6giNrNzKtABSIQ0167", "refresh_token": "1//0306exYla7apXCgYIARAAGAMSNwF-L9IrDTP2kdneYUP7CBpJ_oqtdWfvtIuuaCDaxySRfADycVywQjJ8C1jjrRCCo_1N16vlxdA", "token_uri": "https://oauth2.googleapis.com/token", "client_id": "114734902818-12qkdhcljn2cjtlffmmag6ahq85ig6bb.apps.googleusercontent.com", "client_secret": "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", "scopes": ["https://www.googleapis.com/auth/photoslibrary"], "expiry": "2023-01-11T10:29:46.805328Z"} \ No newline at end of file +{"token": "ya29.a0AX9GBdWHDiya0nEeiRsU9GHU9P2c-9GKjwXXV5h_ypVGcq3Bw9ApjKBMpkK9cpDiSCsWjTiizVJkIhlJl5jiaDbA_UKX9mCthshF00mjmktyNieJWecDYMUSBb_gOg4Yf9K0ql9CjUsFNcOadi7JJKnLXtTUbNgfaCgYKAScSAQASFQHUCsbCa3hEnCWJdY9cXec-WeQ4lQ0167", "refresh_token": "1//0306exYla7apXCgYIARAAGAMSNwF-L9IrDTP2kdneYUP7CBpJ_oqtdWfvtIuuaCDaxySRfADycVywQjJ8C1jjrRCCo_1N16vlxdA", "token_uri": "https://oauth2.googleapis.com/token", "client_id": "114734902818-12qkdhcljn2cjtlffmmag6ahq85ig6bb.apps.googleusercontent.com", "client_secret": "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", "scopes": ["https://www.googleapis.com/auth/photoslibrary"], "expiry": "2023-01-11T12:34:33.635808Z"} \ No newline at end of file