Browse Source

ajout

master
BARRAUX Arthur 2 years ago
parent
commit
4c4b8c3991
  1. 1
      .gitignore
  2. 21
      README.md
  3. 51
      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.

51
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']
def download(next_page=None): class Connection:
"""Shows basic usage of the Docs API. def __init__(self) -> None:
Prints the title of a sample document. self.SCOPES = ['https://www.googleapis.com/auth/photoslibrary']
""" self.creds = None
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 # The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first # created automatically when the authorization flow completes for the first
# time. # time.
if os.path.exists('token.json'): if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES) self.creds = Credentials.from_authorized_user_file('token.json', self.SCOPES)
# If there are no (valid) credentials available, let the user log in. # If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid: if not self.creds or not self.creds.valid:
if creds and creds.expired and creds.refresh_token: if self.creds and self.creds.expired and self.reds.refresh_token:
creds.refresh(Request()) self.creds.refresh(Request())
else: else:
flow = InstalledAppFlow.from_client_secrets_file( flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES) 'credentials.json', self.SCOPES)
creds = flow.run_local_server(port=0) self.creds = flow.run_local_server(port=0)
# Save the credentials for the next run # Save the credentials for the next run
with open('token.json', 'w') as token: with open('token.json', 'w') as token:
token.write(creds.to_json()) token.write(self.creds.to_json())
with open('nextcloud_auth.json') as next_auth:
data = json.load(next_auth) def download(connection, next_page=None):
nextcloud_user = data['user']
nextcloud_pass = data['pass']
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