Browse Source

ajout

master
BARRAUX Arthur 2 years ago
parent
commit
4c4b8c3991
  1. 1
      .gitignore
  2. 21
      README.md
  3. 73
      app.py
  4. 1
      credentials.json
  5. 1
      nextcloud.json
  6. 2
      token.json

1
.gitignore

@ -1,2 +1 @@
nextcloud_auth.json nextcloud_auth.json
credentials.json

21
README.md

@ -0,0 +1,21 @@
# Drive_sync
Un outil de syncronisation de google photos avec un cloud Nextcloud
## Install
Pour installer les dépendances, executer:
```bash
pip3 install -r requirement.txt
```
## Config
Tour d'abord, rendez vous dans le ficher ```nextcloud.json``` et entrez vos identifiant, l'url de votre cloud Nextcloud ainsi que le dossier de destination.
## Usage
Maintenant, exécutez le fichier ```app.py``` et toutes vos photos seront télcharger dans le dossier choisi.

73
app.py

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

1
credentials.json

@ -0,0 +1 @@
{"web":{"client_id":"114734902818-ka0meq495cli7m1ogbs5fampf8s8kgbp.apps.googleusercontent.com","project_id":"drive-sync-1673116306810","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"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","redirect_uris":["https://homelinuxserver.ddns.net"]}}

1
nextcloud.json

@ -0,0 +1 @@
{"user": "YOUR USERNAME", "pass": "YOUR PASSWORD", "url": "YOUR NEXTCLOUD URL", "destination_folder": "PHOTO FOLDER"}

2
token.json

@ -1 +1 @@
{"token": "ya29.a0AX9GBdU-N2vYtbrra2w9xyrYeDDwIeKkqo0xlISwSwmUpUCtP9bn40dEOEyYJow67UIqPocjkDHFqRINTPCBr28pwFBteTLccPLbp4ZPuVYEugcyO-wixxDy9q0Mp0zk60AFKOFRajos9U5iPmNGGyzCKaGwdpzsaCgYKAf4SAQASFQHUCsbCg9W0B-CJ6giNrNzKtABSIQ0167", "refresh_token": "1//0306exYla7apXCgYIARAAGAMSNwF-L9IrDTP2kdneYUP7CBpJ_oqtdWfvtIuuaCDaxySRfADycVywQjJ8C1jjrRCCo_1N16vlxdA", "token_uri": "https://oauth2.googleapis.com/token", "client_id": "114734902818-12qkdhcljn2cjtlffmmag6ahq85ig6bb.apps.googleusercontent.com", "client_secret": "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", "scopes": ["https://www.googleapis.com/auth/photoslibrary"], "expiry": "2023-01-11T10:29:46.805328Z"} {"token": "ya29.a0AX9GBdWHDiya0nEeiRsU9GHU9P2c-9GKjwXXV5h_ypVGcq3Bw9ApjKBMpkK9cpDiSCsWjTiizVJkIhlJl5jiaDbA_UKX9mCthshF00mjmktyNieJWecDYMUSBb_gOg4Yf9K0ql9CjUsFNcOadi7JJKnLXtTUbNgfaCgYKAScSAQASFQHUCsbCa3hEnCWJdY9cXec-WeQ4lQ0167", "refresh_token": "1//0306exYla7apXCgYIARAAGAMSNwF-L9IrDTP2kdneYUP7CBpJ_oqtdWfvtIuuaCDaxySRfADycVywQjJ8C1jjrRCCo_1N16vlxdA", "token_uri": "https://oauth2.googleapis.com/token", "client_id": "114734902818-12qkdhcljn2cjtlffmmag6ahq85ig6bb.apps.googleusercontent.com", "client_secret": "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", "scopes": ["https://www.googleapis.com/auth/photoslibrary"], "expiry": "2023-01-11T12:34:33.635808Z"}
Loading…
Cancel
Save