provider/dropbox

This commit is contained in:
Tobias Lindenberg
2021-01-10 14:03:57 +01:00
parent ff8e431630
commit 41c8e53569

View File

@ -1,11 +1,9 @@
import base64
import io import io
import json
import os import os
from datetime import datetime from datetime import datetime
import requests import requests
import json
from cookbook.models import Recipe, RecipeImport, SyncLog from cookbook.models import Recipe, RecipeImport, SyncLog
from cookbook.provider.provider import Provider from cookbook.provider.provider import Provider
@ -34,16 +32,26 @@ class Dropbox(Provider):
return r return r
import_count = 0 import_count = 0
for recipe in recipes['entries']: # TODO check if has_more is set and import that as well # TODO check if has_more is set and import that as well
for recipe in recipes['entries']:
path = recipe['path_lower'] path = recipe['path_lower']
if not Recipe.objects.filter(file_path__iexact=path).exists() and not RecipeImport.objects.filter( if not Recipe.objects.filter(file_path__iexact=path).exists() \
file_path=path).exists(): and not RecipeImport.objects.filter(file_path=path).exists(): # noqa: E501
name = os.path.splitext(recipe['name'])[0] name = os.path.splitext(recipe['name'])[0]
new_recipe = RecipeImport(name=name, file_path=path, storage=monitor.storage, file_uid=recipe['id']) new_recipe = RecipeImport(
name=name,
file_path=path,
storage=monitor.storage,
file_uid=recipe['id']
)
new_recipe.save() new_recipe.save()
import_count += 1 import_count += 1
log_entry = SyncLog(status='SUCCESS', msg='Imported ' + str(import_count) + ' recipes', sync=monitor) log_entry = SyncLog(
status='SUCCESS',
msg='Imported ' + str(import_count) + ' recipes',
sync=monitor
)
log_entry.save() log_entry.save()
monitor.last_checked = datetime.now() monitor.last_checked = datetime.now()
@ -53,7 +61,7 @@ class Dropbox(Provider):
@staticmethod @staticmethod
def create_share_link(recipe): def create_share_link(recipe):
url = "https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings" url = "https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings" # noqa: E501
headers = { headers = {
"Authorization": "Bearer " + recipe.storage.token, "Authorization": "Bearer " + recipe.storage.token,
@ -84,8 +92,8 @@ class Dropbox(Provider):
r = requests.post(url, headers=headers, data=json.dumps(data)) r = requests.post(url, headers=headers, data=json.dumps(data))
p = r.json() p = r.json()
for l in p['links']: for link in p['links']:
return l['url'] return link['url']
response = Dropbox.create_share_link(recipe) response = Dropbox.create_share_link(recipe)
return response['url'] return response['url']
@ -96,7 +104,9 @@ class Dropbox(Provider):
recipe.link = Dropbox.get_share_link(recipe) recipe.link = Dropbox.get_share_link(recipe)
recipe.save() recipe.save()
response = requests.get(recipe.link.replace('www.dropbox.', 'dl.dropboxusercontent.')) response = requests.get(
recipe.link.replace('www.dropbox.', 'dl.dropboxusercontent.')
)
return io.BytesIO(response.content) return io.BytesIO(response.content)
@ -111,7 +121,11 @@ class Dropbox(Provider):
data = { data = {
"from_path": recipe.file_path, "from_path": recipe.file_path,
"to_path": os.path.dirname(recipe.file_path) + '/' + new_name + os.path.splitext(recipe.file_path)[1] "to_path": "%s/%s%s" % (
os.path.dirname(recipe.file_path),
new_name,
os.path.splitext(recipe.file_path)[1]
)
} }
r = requests.post(url, headers=headers, data=json.dumps(data)) r = requests.post(url, headers=headers, data=json.dumps(data))