Coverage for cookbook/provider/dropbox.py: 28%
75 statements
« prev ^ index » next coverage.py v7.4.0, created at 2023-12-29 00:47 +0100
« prev ^ index » next coverage.py v7.4.0, created at 2023-12-29 00:47 +0100
1import io
2import json
3import os
4from datetime import datetime
6import requests
7import validators
9from cookbook.models import Recipe, RecipeImport, SyncLog
10from cookbook.provider.provider import Provider
13class Dropbox(Provider):
15 @staticmethod
16 def import_all(monitor):
17 url = "https://api.dropboxapi.com/2/files/list_folder"
19 headers = {
20 "Authorization": "Bearer " + monitor.storage.token,
21 "Content-Type": "application/json"
22 }
24 data = {
25 "path": monitor.path
26 }
28 r = requests.post(url, headers=headers, data=json.dumps(data))
29 try:
30 recipes = r.json()
31 except ValueError:
32 log_entry = SyncLog(status='ERROR', msg=str(r), sync=monitor)
33 log_entry.save()
34 return r
36 import_count = 0
37 # TODO check if has_more is set and import that as well
38 for recipe in recipes['entries']:
39 path = recipe['path_lower']
40 if not Recipe.objects.filter(file_path__iexact=path, space=monitor.space).exists() and not RecipeImport.objects.filter(file_path=path, space=monitor.space).exists():
41 name = os.path.splitext(recipe['name'])[0]
42 new_recipe = RecipeImport(
43 name=name,
44 file_path=path,
45 storage=monitor.storage,
46 file_uid=recipe['id'],
47 space=monitor.space,
48 )
49 new_recipe.save()
50 import_count += 1
52 log_entry = SyncLog(
53 status='SUCCESS',
54 msg='Imported ' + str(import_count) + ' recipes',
55 sync=monitor,
56 )
57 log_entry.save()
59 monitor.last_checked = datetime.now()
60 monitor.save()
62 return True
64 @staticmethod
65 def create_share_link(recipe):
66 url = "https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings" # noqa: E501
68 headers = {
69 "Authorization": "Bearer " + recipe.storage.token,
70 "Content-Type": "application/json"
71 }
73 data = {
74 "path": recipe.file_uid
75 }
77 r = requests.post(url, headers=headers, data=json.dumps(data))
79 return r.json()
81 @staticmethod
82 def get_share_link(recipe):
83 url = "https://api.dropboxapi.com/2/sharing/list_shared_links"
85 headers = {
86 "Authorization": "Bearer " + recipe.storage.token,
87 "Content-Type": "application/json"
88 }
90 data = {
91 "path": recipe.file_path,
92 }
94 r = requests.post(url, headers=headers, data=json.dumps(data))
95 p = r.json()
97 for link in p['links']:
98 return link['url']
100 response = Dropbox.create_share_link(recipe)
101 return response['url']
103 @staticmethod
104 def get_file(recipe):
105 if not recipe.link:
106 recipe.link = Dropbox.get_share_link(recipe)
107 recipe.save()
109 url = recipe.link.replace('www.dropbox.', 'dl.dropboxusercontent.')
110 if validators.url(url, public=True):
111 response = requests.get(url)
113 return io.BytesIO(response.content)
115 @staticmethod
116 def rename_file(recipe, new_name):
117 url = "https://api.dropboxapi.com/2/files/move_v2"
119 headers = {
120 "Authorization": "Bearer " + recipe.storage.token,
121 "Content-Type": "application/json"
122 }
124 data = {
125 "from_path": recipe.file_path,
126 "to_path": "%s/%s%s" % (
127 os.path.dirname(recipe.file_path),
128 new_name,
129 os.path.splitext(recipe.file_path)[1]
130 )
131 }
133 r = requests.post(url, headers=headers, data=json.dumps(data))
135 return r.json()
137 @staticmethod
138 def delete_file(recipe):
139 url = "https://api.dropboxapi.com/2/files/delete_v2"
141 headers = {
142 "Authorization": "Bearer " + recipe.storage.token,
143 "Content-Type": "application/json"
144 }
146 data = {
147 "path": recipe.file_path
148 }
150 r = requests.post(url, headers=headers, data=json.dumps(data))
152 return r.json()