From 7489a5c37945047e37b2e34eeef257916f529107 Mon Sep 17 00:00:00 2001 From: arthur Date: Wed, 11 Jan 2023 14:45:35 +0100 Subject: [PATCH] pas de doublons --- .gitignore | 4 +++- app.py | 17 +++++++++-------- requirement.txt | 3 +++ 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index f79b097..6fbdc28 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -nextcloud_auth.json +nextcloud.json +__pycache__/* +token.json \ No newline at end of file diff --git a/app.py b/app.py index 6b7cae8..022b66f 100644 --- a/app.py +++ b/app.py @@ -40,6 +40,7 @@ class Connection: self.nextcloud_pass = data['pass'] self.nextcloud_url = data['url'] self.nextcloud_destination_folder = data['destination_folder'] + self.google_connect() def google_connect(self): # The file token.json stores the user's access and refresh tokens, and is @@ -49,7 +50,7 @@ class Connection: 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: + if self.creds and self.creds.expired and self.creds.refresh_token: self.creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( @@ -65,19 +66,19 @@ def download(connection, next_page=None): try: service = build('photoslibrary', 'v1', credentials=connection.creds, static_discovery=False) - results = service.mediaItems().list(pageSize=100, pageToken=next_page).execute() + results = service.mediaItems().list(pageSize=20, pageToken=next_page).execute() items = results.get('mediaItems', []) next_page = results.get('nextPageToken') - images = [] for item in items: print(f"{item['filename']} {item['mimeType']}") 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(connection.nextcloud_user, connection.nextcloud_pass), data=requests.get(item['baseUrl']+'=dv').content) - else: - requests.put(url, auth=HTTPBasicAuth(connection.nextcloud_user, connection.nextcloud_pass), data=requests.get(item['baseUrl']+'=d').content) + if requests.get(url, auth=HTTPBasicAuth(connection.nextcloud_user, connection.nextcloud_pass)).status_code != 200: + if "video" in item['mimeType']: + requests.put(url, auth=HTTPBasicAuth(connection.nextcloud_user, connection.nextcloud_pass), data=requests.get(item['baseUrl']+'=dv').content) + else: + requests.put(url, auth=HTTPBasicAuth(connection.nextcloud_user, connection.nextcloud_pass), data=requests.get(item['baseUrl']+'=d').content) return next_page @@ -89,7 +90,7 @@ def download(connection, next_page=None): if __name__ == '__main__': connection = Connection() next_page = download(connection) - while next_page != 'null': + while next_page != None: next_page = download(connection, next_page) print('done') # [END docs_quickstart] \ No newline at end of file diff --git a/requirement.txt b/requirement.txt index 1d0d769..e2e9f30 100644 --- a/requirement.txt +++ b/requirement.txt @@ -5,14 +5,17 @@ google-api-core==2.11.0 google-api-python-client==2.71.0 google-auth==2.15.0 google-auth-httplib2==0.1.0 +google-auth-oauthlib==0.8.0 googleapis-common-protos==1.57.1 httplib2==0.21.0 idna==3.4 +oauthlib==3.2.2 protobuf==4.21.12 pyasn1==0.4.8 pyasn1-modules==0.2.8 pyparsing==3.0.9 requests==2.28.1 +requests-oauthlib==1.3.1 rsa==4.9 six==1.16.0 uritemplate==4.1.1