-
Notifications
You must be signed in to change notification settings - Fork 0
/
putio.py
383 lines (310 loc) · 13 KB
/
putio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
from client import Client
class Putio:
def __init__(self, oauth_token=None, client_id=None):
self.OAUTH_TOKEN = oauth_token
self.CLIENT_ID = client_id
@property
def File(self):
return File(self.OAUTH_TOKEN, self.CLIENT_ID)
@property
def Account(self):
return Account(self.OAUTH_TOKEN, self.CLIENT_ID)
@property
def Transfer(self):
return Transfer(self.OAUTH_TOKEN, self.CLIENT_ID)
@property
def Friend(self):
return Friend(self.OAUTH_TOKEN, self.CLIENT_ID)
@property
def Event(self):
return Event(self.OAUTH_TOKEN, self.CLIENT_ID)
class Resource:
def __init__(self, oauth_token=None, client_id=None):
self.OAUTH_TOKEN = oauth_token
self.CLIENT_ID = client_id
self.client = self._get_client()
def _get_client(self):
client = Client(oauth_token=self.OAUTH_TOKEN,
client_id=self.CLIENT_ID)
return client
def _call(self, action=None, method=None, params=None, data=None):
url = self.BASE_URL
return self.client.call(url, action=action, params=params, data=data,
method=method)
class File(Resource):
def __init__(self, oauth_token=None, client_id=None):
self.OAUTH_TOKEN = oauth_token
self.CLIENT_ID = client_id
self.BASE_URL = 'files/'
self.client = self._get_client()
def _download_file(self, response):
filename = response.headers['Content-Disposition']
seperator_pattern = '; filename='
separator = filename.find(seperator_pattern)
filename = filename[separator + len(seperator_pattern):]
# If file name has spaces, it must have quotes around.
filename = filename.strip('"')
with open(filename, 'wb') as f:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
f.flush()
def _call(self, action=None, method=None, file_id=None, params=None,
data=None, files=None, stream=False):
url = self.BASE_URL
if file_id is not None:
url += str(file_id)
return self.client.call(url, action=action, params=params, data=data,
method=method, files=files, stream=stream)
def list_files(self):
"""Lists files in a folder."""
response = self._call(action='list')
return response.json()
def search(self, query, page=1):
"""
Searches your (and shared) files. Returns 50 results at a time.
The url for next 50 results is given in the "next" paramater..
Parameters:
query: The keyword to search.
page: Optional. Defaults to 1. If -1 given, returns all results
at a time.
Search Syntax:
from: me, shares, jack or all
type: video, audio, image, iphone or all
ext: mp3, avi, jpg, mp4 or all
"""
action = 'search/%s/page/%s' % (query, page)
response = self._call(action=action)
return response.json()
def upload(self, path, filename=None, parent_id=0):
"""
Uploads a file. If the uploaded file is a torrent file,
starts it as a transfer. This endpoint must be used with
upload.put.io domain.
"""
data = {'parent_id': parent_id}
with open(path) as f:
if filename:
files = {'file': (filename, f)}
else:
files = {'file': f}
response = self._call(action='upload', method='POST',
files=files, data=data)
return response.json()
def create_folder(self, foldername, parent_id=0):
""" Creates a new folder. """
data = {'name': foldername,
'parent_id': parent_id}
response = self._call(action='create-folder', data=data,
method='POST')
return response.json()
def get_file_property(self, file_id):
""" Returns a file's properties. """
response = self._call(file_id=str(file_id))
return response.json()
def delete(self, file_ids):
""" Deletes given files. """
data = {'file_ids': file_ids}
response = self._call(action='delete', method='POST', data=data)
return response.json()
def rename(self, file_id, name):
""" Renames given file. """
data = {'file_id': str(file_id),
'name': name}
response = self._call(action='rename', data=data, method='POST')
return response.json()
def move(self, file_ids, parent_id=0):
""" Moves files to the given destination. """
data = {'file_ids': file_ids,
'parent_id': parent_id}
response = self._call(action='move', data=data, method='POST')
return response.json()
def convert_to_mp4(self, file_id):
""" Starts the conversion of the given file to mp4. """
action = '%s/mp4' % file_id
response = self._call(action=action, method='POST')
return response.json()
def get_mp4_status(self, file_id):
""" Returns the status of mp4 conversion of the given file. """
action = '%s/mp4' % file_id
response = self._call(action=action)
return response.json()
def download(self, file_id):
"""
Downloads the contents of the file. Only files can be downdloaded,
For directories, you should use zip_and_download method.
"""
action = '%s/download' % file_id
response = self._call(action=action, stream=True)
# Since this method can't be used for downloading folders,
# see Zip-and-Download instead.
self._download_file(response)
def zip_and_download(self, file_ids):
"""
Create zipstream for given files. A redirect to created zipstream
will be returned.
"""
params = {'file_ids': file_ids}
response = self._call(action='zip', params=params)
# put.io returns error for 'application/x-directory' typed
# attachments. So, we assume that we will always get files as
# attachments not directories.
self._download_file(response)
def share(self, file_ids, friends):
""" Shares given files with given friends or all friends. """
data = {'file_ids': file_ids,
'friends': friends}
response = self._call(action='share', data=data, method='POST')
return response.json()
def list_shared_files(self):
""" Returns list of shared files and share information. """
response = self._call(action='shared')
return response.json()
def shared_with(self, file_id):
"""
Returns list of users file is shared with. Each result item
contains a share id which can be used for unsharing.
"""
action = '%s/shared-with' % file_id
response = self._call(action=action)
return response.json()
def unshare(self, file_id, friends):
""" Unshares given file from given friends or from everyone. """
action = '%s/unshare' % file_id
data = {'shares': friends}
response = self._call(action=action, data=data, method='POST')
return response.json()
def list_subtitles(self, file_id):
""" Lists available subtitles for user's preferred language. """
action = '%s/subtitles' % file_id
response = self._call(action=action)
return response.json()
def download_subtitle(self, file_id, subtitle_key='default',
subtitle_format='srt'):
""" Downloadss the contents of the subtitle file. """
action = '%s/subtitles/%s' % (file_id, subtitle_key)
params = {'format': subtitle_format}
response = self._call(action=action, params=params)
self._download_file(response)
class Event(Resource):
def __init__(self, oauth_token=None, client_id=None):
self.OAUTH_TOKEN = oauth_token
self.CLIENT_ID = client_id
self.base_url = 'events/'
self.client = self._get_client()
def list(self):
""" List of dashboard events. Includes download and share events. """
response = self._call(action='list')
return response.json()
def delete(self):
"""
Clear all dashboard events. User's home screen (dashboard) which
uses same data will also be cleared at Put.io website.
"""
response = self._call(action='delete', method='POST')
return response.json()
class Transfer(Resource):
def __init__(self, oauth_token=None, client_id=None):
self.OAUTH_TOKEN = oauth_token
self.CLIENT_ID = client_id
self.BASE_URL = 'transfers/'
self.client = self._get_client()
def _call(self, action=None, method=None, transfer_id=None, params=None,
data=None):
url = self.BASE_URL
if transfer_id is not None:
url += str(transfer_id)
return self.client.call(url, action=action, params=params, data=data,
method=method)
def add_transfer(self, url, save_parent_id=None, extract=None,
callback_url=None):
""" Adds a new transfer. """
data = {'url': url,
'save_parent_id': save_parent_id,
'extract': extract,
'callback_url': callback_url}
response = self._call(action='add', data=data, method='POST')
return response.json()
def list(self):
"""
Lists active transfers. If transfer is completed, it is
removed from the list.
"""
response = self._call(action='list')
return response.json()
def get_transfer(self, transfer_id):
""" Returns a transfer's properties. """
transfer_id = str(transfer_id)
response = self._call(transfer_id=transfer_id)
return response.json()
def retry(self, transfer_id):
""" Retry previously failed transfer. """
data = {'id': str(transfer_id)}
response = self._call(action='retry', data=data, method='POST')
return response.json()
def cancel(self, transfer_ids):
""" Deletes the given transfers. """
data = {'transfer_ids': transfer_ids}
response = self._call(action='cancel', data=data, method='POST')
return response.json()
def clean(self):
""" Clean completed transfers from the list. """
response = self._call(action='clean', method='POST')
return response.json()
class Friend(Resource):
def __init__(self, oauth_token=None, client_id=None):
self.OAUTH_TOKEN = oauth_token
self.CLIENT_ID = client_id
self.BASE_URL = 'friends/'
self.client = self._get_client()
def list(self):
""" Lists friends. """
response = self._call(action='list')
return response.json()
def waiting_requests(self):
""" Lists incoming friend requests. """
response = self._call(action='waiting-requests')
return response.json()
def send_request(self, friend_id):
""" Sends a friend request to the given username. """
action = '%s/request' % friend_id
response = self._call(action=action, method='POST')
return response.json()
def approve_request(self, friend_id):
""" Approves a friend request from the given username. """
action = '%s/approve' % friend_id
response = self._call(action=action, method='POST')
return response.json()
def deny_request(self, friend_id):
""" Denies a friend request from the given username. """
action = '%s/deny' % friend_id
response = self._call(action=action, method='POST')
return response.json()
def unfriend(self, friend_id):
""" Removes friend from friend list. """
action = '%s/unfriend' % friend_id
response = self._call(action=action, method='POST')
return response.json()
class Account(Resource):
def __init__(self, oauth_token=None, client_id=None):
self.OAUTH_TOKEN = oauth_token
self.CLIENT_ID = client_id
self.BASE_URL = 'account/'
self.client = self._get_client()
def info(self):
""" Information about user account. """
response = self._call(action='info')
return response.json()
def get_settings(self):
""" User preferences. """
response = self._call(action='settings')
return response.json()
def set_settings(self, default_folder=None, extraction_default=None,
is_invisible=None, subtitle_languages=None):
""" Updates user preferences. Only sent parameters are updated. """
data = {'default_download_folder': default_folder,
'extraction_default': extraction_default,
'is_invisible': is_invisible,
'subtitle_languages': subtitle_languages}
response = self._call(action='settings', data=data, method='POST')
return response.json()