@ -29,37 +29,41 @@ from googleapiclient.http import MediaIoBaseDownload
# If modifying these scopes, delete the file token.json.
# If modifying these scopes, delete the file token.json.
SCOPES = [ ' https://www.googleapis.com/auth/photoslibrary ' ]
class Connection :
def download ( next_page = None ) :
def __init__ ( self ) - > None :
""" Shows basic usage of the Docs API.
self . SCOPES = [ ' https://www.googleapis.com/auth/photoslibrary ' ]
Prints the title of a sample document .
self . creds = None
"""
with open ( ' nextcloud.json ' ) as nextcloud :
creds = None
data = json . load ( nextcloud )
# The file token.json stores the user's access and refresh tokens, and is
self . nextcloud_user = data [ ' user ' ]
# created automatically when the authorization flow completes for the first
self . nextcloud_pass = data [ ' pass ' ]
# time.
self . nextcloud_url = data [ ' url ' ]
if os . path . exists ( ' token.json ' ) :
self . nextcloud_destination_folder = data [ ' destination_folder ' ]
creds = Credentials . from_authorized_user_file ( ' token.json ' , SCOPES )
# If there are no (valid) credentials available, let the user log in.
def google_connect ( self ) :
if not creds or not creds . valid :
# The file token.json stores the user's access and refresh tokens, and is
if creds and creds . expired and creds . refresh_token :
# created automatically when the authorization flow completes for the first
creds . refresh ( Request ( ) )
# time.
else :
if os . path . exists ( ' token.json ' ) :
flow = InstalledAppFlow . from_client_secrets_file (
self . creds = Credentials . from_authorized_user_file ( ' token.json ' , self . SCOPES )
' credentials.json ' , SCOPES )
# If there are no (valid) credentials available, let the user log in.
creds = flow . run_local_server ( port = 0 )
if not self . creds or not self . creds . valid :
# Save the credentials for the next run
if self . creds and self . creds . expired and self . reds . refresh_token :
with open ( ' token.json ' , ' w ' ) as token :
self . creds . refresh ( Request ( ) )
token . write ( creds . to_json ( ) )
else :
flow = InstalledAppFlow . from_client_secrets_file (
with open ( ' nextcloud_auth.json ' ) as next_auth :
' credentials.json ' , self . SCOPES )
data = json . load ( next_auth )
self . creds = flow . run_local_server ( port = 0 )
nextcloud_user = data [ ' user ' ]
# Save the credentials for the next run
nextcloud_pass = data [ ' pass ' ]
with open ( ' token.json ' , ' w ' ) as token :
token . write ( self . creds . to_json ( ) )
def download ( connection , next_page = None ) :
try :
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 ( )
results = service . mediaItems ( ) . list ( pageSize = 100 , pageToken = next_page ) . execute ( )
@ -69,11 +73,11 @@ def download(next_page=None):
for item in items :
for item in items :
print ( f " { item [ ' filename ' ] } { item [ ' mimeType ' ] } " )
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 ' ] :
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 :
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
return next_page
@ -83,8 +87,9 @@ def download(next_page=None):
if __name__ == ' __main__ ' :
if __name__ == ' __main__ ' :
next_page = download ( )
connection = Connection ( )
next_page = download ( connection )
while next_page != ' null ' :
while next_page != ' null ' :
next_page = download ( next_page )
next_page = download ( connection , next_page )
print ( ' done ' )
print ( ' done ' )
# [END docs_quickstart]
# [END docs_quickstart]