You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.9 KiB

2 years ago
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# [START docs_quickstart]
from __future__ import print_function
import os.path
import requests, json
from requests.auth import HTTPBasicAuth
from httplib2 import Http
2 years ago
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
2 years ago
from googleapiclient.discovery import build
2 years ago
from googleapiclient.errors import HttpError
from googleapiclient.http import MediaIoBaseDownload
2 years ago
# If modifying these scopes, delete the file token.json.
2 years ago
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']
2 years ago
self.google_connect()
2 years ago
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:
2 years ago
if self.creds and self.creds.expired and self.creds.refresh_token:
2 years ago
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):
2 years ago
try:
2 years ago
service = build('photoslibrary', 'v1', credentials=connection.creds, static_discovery=False)
2 years ago
results = service.mediaItems().list(pageSize=20, pageToken=next_page).execute()
items = results.get('mediaItems', [])
2 years ago
next_page = results.get('nextPageToken')
for item in items:
print(f"{item['filename']} {item['mimeType']}")
2 years ago
url = connection.nextcloud_url + '/remote.php/dav/files/' + connection.nextcloud_user + '/' + connection.nextcloud_destination_folder + '/' + item['filename']
2 years ago
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)
2 years ago
return next_page
2 years ago
2 years ago
except HttpError as err:
print(err)
2 years ago
2 years ago
if __name__ == '__main__':
2 years ago
connection = Connection()
next_page = download(connection)
2 years ago
while next_page != None:
2 years ago
next_page = download(connection, next_page)
2 years ago
print('done')
2 years ago
# [END docs_quickstart]