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
96 lines
3.9 KiB
# 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
|
|
|
|
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
|
|
from googleapiclient.http import MediaIoBaseDownload
|
|
|
|
|
|
# If modifying these scopes, delete the file token.json.
|
|
|
|
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']
|
|
self.google_connect()
|
|
|
|
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.creds.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=connection.creds, static_discovery=False)
|
|
|
|
results = service.mediaItems().list(pageSize=20, pageToken=next_page).execute()
|
|
|
|
items = results.get('mediaItems', [])
|
|
next_page = results.get('nextPageToken')
|
|
|
|
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 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
|
|
|
|
|
|
except HttpError as err:
|
|
print(err)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
connection = Connection()
|
|
next_page = download(connection)
|
|
while next_page != None:
|
|
next_page = download(connection, next_page)
|
|
print('done')
|
|
# [END docs_quickstart]
|