@ -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 . c reds , 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]