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 json
import os
from datetime import datetime
import requests
import json
from cookbook.models import Recipe, RecipeImport, SyncLog
from cookbook.provider.provider import Provider
@ -34,16 +32,26 @@ class Dropbox(Provider):
return r
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']
if not Recipe.objects.filter(file_path__iexact=path).exists() and not RecipeImport.objects.filter(
file_path=path).exists():
if not Recipe.objects.filter(file_path__iexact=path).exists() \
and not RecipeImport.objects.filter(file_path=path).exists(): # noqa: E501
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()
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()
monitor.last_checked = datetime.now()
@ -53,7 +61,7 @@ class Dropbox(Provider):
@staticmethod
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 = {
"Authorization": "Bearer " + recipe.storage.token,
@ -84,8 +92,8 @@ class Dropbox(Provider):
r = requests.post(url, headers=headers, data=json.dumps(data))
p = r.json()
for l in p['links']:
return l['url']
for link in p['links']:
return link['url']
response = Dropbox.create_share_link(recipe)
return response['url']
@ -96,7 +104,9 @@ class Dropbox(Provider):
recipe.link = Dropbox.get_share_link(recipe)
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)
@ -111,7 +121,11 @@ class Dropbox(Provider):
data = {
"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))