-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathletitbit.py
420 lines (370 loc) · 16.1 KB
/
letitbit.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# -*- coding: utf-8 -*
"""
Simple module for accessing letitbit.net filesharing services.
Unfortunately not all methods from api implemented for now because they are not described good enough
Just use Letitbit class to upload your files and do other stuff.
"""
import json
import httplib, urllib, urllib2, cookielib
from ftplib import FTP
import os
class Error(Exception):
"""Base exception class for all other exceptions in module"""
def __init__(self, value, msg=None):
Exception.__init__(self)
self.value = value
self.msg = msg
def __str__(self):
return self.msg
class UnknownProtocolException(Error):
"""When not right protocol used in arguments list"""
def __init__(self, value, msg=None):
Error.__init__(self, value, msg)
self.msg = msg if msg else "Unknown Protocol: \"{}\"".format(self.value)
class NotSuccessfulResponseException(Error):
"""When response status from server is not 'OK'"""
def __init__(self, value, msg=None):
Error.__init__(self, value, msg)
self.msg = msg if msg else "Not Successful Response came from server: \"{}\"".format(self.value)
def empty(inList):
if isinstance(inList, list): # Is a list
return all(map(empty, inList))
return False # Not a list
class Letitbit(object):
"""Class to access to Letitbit.net file sharing services"""
protocols = ('ftp', 'http',) # list of protocols which can be used for files uploading
def __init__(self, key, protocol='ftp'):
self.key = key
self.protocol = protocol
self.headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
self.conn = httplib.HTTPConnection("api.letitbit.net")
self.data = [self.key]
self.servers = dict()
for p in Letitbit.protocols:
self.servers[p] = list()
def add_method(self, controller, method, parameters=None):
"""Creates dictionary that will be converted to JSON representation"""
meth = list()
meth.append("{}/{}".format(controller, method))
if parameters:
meth.append(parameters)
self.data.append(meth)
def run(self):
"""Creates JSON to request data from server"""
params = urllib.urlencode({'r': json.dumps(self.data)})
response = None
try:
self.conn.request("POST", "", params, self.headers)
response = self.conn.getresponse()
response = response.read()
response = json.loads(response)
finally:
self.data = [self.key]
return response
def check_key_info(self):
"""Checks statistics for current user's key"""
self.add_method('key', 'info')
result = self.run()
if result['status'] != 'OK':
raise NotSuccessfulResponseException(result['status'])
key_data = result['data'][0]
self.max_requests = key_data['max']
self.current_requests = key_data['cur']
self.total_requests = key_data['total_requests']
self.total_points = key_data['total_points']
def get_key_auth(self, login, passwd, project='letitbit.net'):
"""Returns key that is needed to access several projects like sms4file and others"""
import hashlib
passwd_hash = hashlib.md5(hashlib.md5(passwd).hexdigest()).hexdigest()
args = {
'login': login,
'pass': passwd_hash,
'project': project
}
self.add_method('key', 'auth', args)
response = self.run()
if response['status'] != 'OK':
raise NotSuccessfulResponseException(response['status'])
return response['data'][0]
def get_servers_list(self, protocol):
"""Gets servers list for chosen protocol and saves them in object's servers attribute"""
if protocol not in Letitbit.protocols:
raise UnknownProtocolException(protocol)
self.add_method(protocol, 'listing')
result = self.run()
if result['status'] !='OK':
raise UnknownProtocolException(result['status'])
servers = result['data'][0]
servers = sorted(servers, key=lambda server: server[1])
s = []
for server in servers:
s.append(server[0])
self.servers[protocol] = servers
def ftp_upload_file(self, file_full_path, server):
filename = os.path.basename(file_full_path)
file = open(file_full_path, "rb")
ftp = FTP(server)
ftp.login(self.login, self.password)
ftp.storbinary('STOR ' + filename, file)
ftp.quit()
file.close()
def process(self, protocol, server, filename):
"""Returns links list for uploaded file"""
args = {
'server': server,
'tmpname': filename,
'realname': filename
}
self.add_method(protocol, 'process', args)
response = self.run()
print response
return response
def upload_file(self, file_full_path, protocol):
""" Method allows uploading file to one of available servers depending on protocol you chose """
if protocol not in Letitbit.protocols:
raise UnknownProtocolException(protocol)
# get first server from list sorted by workload
server = self.servers[protocol][0][0]
if protocol == 'ftp':
self.ftp_upload_file(file_full_path, server)
else:
pass
filename = os.path.basename(file_full_path)
response = self.process(protocol, server, filename)
if response['status'] != 'OK' or empty(response['data']):
raise NotSuccessfulResponseException(response['status'])
return response['data'][0][0]['link'], response['data'][0][0]['uid']
def _get_auth_data(self, protocol):
"""Gets authentication information for ftp and saves it in object's attributes login and password"""
if protocol not in Letitbit.protocols:
raise UnknownProtocolException(protocol)
self.add_method(protocol, 'auth_data')
result = self.run()
if result['status'] != 'OK':
raise NotSuccessfulResponseException(result['status'])
auth_data = result['data'][0]
self.login = auth_data['login']
self.password = auth_data['pass']
def list_controllers(self, output=False):
"""Returns list of available controllers"""
self.add_method('list', 'controllers')
response = self.run()
if response['status'] != 'OK':
raise NotSuccessfulResponseException(response['status'])
if output:
for c in response['data'][0]:
print(c)
return response['data'][0]
def list_methods(self, controller=None, output=False):
"""Returns methods list in chosen controller. If there is nothing in controller attribute then it returns info about all controllers. If output is True - prints out all info."""
controllers = [controller]
methods = dict()
if not controller:
controllers = self.list_controllers()
for c in controllers:
args = {'controller': c}
self.add_method('list', 'methods', args)
response = self.run()
if response['status'] != 'OK':
raise NotSuccessfulResponseException(response['status'])
if output:
if len(controllers) > 1:
print(c)
for k, v in response['data'][0].items():
print ('\t' + k)
print("\t\tDescription: {}\n\t\tCost: {}\n\t\tCall: {}\n".format(
v.get('descr', str(None)).encode('utf-8'),
v.get('cost', 0),
v.get('call', "").encode('utf-8')).replace('\\', '')
)
print
methods[c] = response['data'][0]
return methods
def set_ftp_flag_auto(self, value=True):
args = {
'flag': int(value)
}
self.add_method('ftp', 'flag_auto', args)
response = self.run()
if response['status'] != 'OK':
raise NotSuccessfulResponseException(response['status'])
def get_direct_links(self, link, passwd=None):
"""Returns direct links to file"""
args = {
'link': link,
'pass': "" if not passwd else passwd
}
self.add_method('download', 'direct_links', args)
response = self.run()
return response['data'][0]
def check_link(self, link):
"""Returns True if file is on one or more servers and False otherwise"""
args = {
'link': link
}
self.add_method('download', 'check_link', args)
response = self.run()
if response['status'] != 'OK':
raise NotSuccessfulResponseException(response['status'])
return bool(response['data'][0])
def get_file_info(self, link):
"""Returns dictionary which contains some info about file like file size, uid and etc."""
args = {
'link': link
}
self.add_method('download', 'info', args)
response = self.run()
if response['status'] != 'OK':
raise NotSuccessfulResponseException(response['status'])
return response['data'][0]
def get_filemanager_listing(self, limit=50, page=1, folder=0):
"""Returns list of dictionaries containing various info about all files which you chose with function parameters"""
args = {
'limit': limit,
'page': page,
'folder': folder
}
self.add_method('filemanager', 'listing', args)
response = self.run()
if response['status'] != 'OK':
raise NotSuccessfulResponseException(response['status'])
return response['data'][0]
def get_filemanager_folders(self):
"""Returns list of folders existing in file manager"""
self.add_method('filemanager', 'folders')
response = self.run()
if response['status'] != 'OK':
raise NotSuccessfulResponseException(response['status'])
return response['data'][0]
def get_filemanager_aliases(self, files_info):
"""Returns uids of files aliases in other projects. files_info should be dictionary where key is file's md5 hash string and value is it's size"""
args = {
'files': files_info
}
self.add_method('filemanager', 'aliases', args)
response = self.run()
if response['status'] != 'OK':
raise NotSuccessfulResponseException(response['status'])
return response['data'][0]
def get_filemanager_vipaliases(self, files_info):
"""Returns list of aliases for vip-file.com"""
args = {
'files': files_info
}
self.add_method('filemanager', 'vipaliases', args)
response = self.run()
if response['status'] != 'OK':
raise NotSuccessfulResponseException(response['status'])
return response['data'][0]
def delete(self, files_uids):
"""Removes files which uids are in files_uids from hosts. Returns amount of removed files"""
args = {
'uids': files_uids
}
self.add_method('filemanager', 'delete', args)
response = self.run()
if response['status'] != 'OK':
raise NotSuccessfulResponseException(response['status'])
return response['data'][0]
def rename(self, file_uid, name):
"""Renames file"""
args = {
'uid': file_uid,
'name': name
}
self.add_method('filemanager', 'rename', args)
response = self.run()
if response['status'] != 'OK':
raise NotSuccessfulResponseException(response['status'])
return bool(response['data'][0])
def get_user_aliases(self):
"""Returns dictionary with current user's ID in all projects (letitbit, vip-file, etc)"""
self.add_method('user', 'aliases')
response = self.run()
if response['status'] != 'OK':
raise NotSuccessfulResponseException(response['status'])
return bool(response['data'][0])
def get_user_aliases_login(self, project='letitbit.net'):
"""Returns user's login for chosen project"""
args = {
'project': project
}
self.add_method('user', 'aliases_login', args)
response = self.run()
if response['status'] != 'OK':
raise NotSuccessfulResponseException(response['status'])
return response['data'][0]
def get_user_info(self, login=None, passwd_hash=None, project='letitbit.net'):
if login and passwd_hash:
args = {
'login': login,
'pass': passwd_hash,
'project': project
}
self.add_method('user', 'info', args)
else:
self.add_method('user', 'info')
response = self.run()
if response['status'] != 'OK':
raise NotSuccessfulResponseException(response['status'])
return response['data'][0]
def register_user(self, login, passwd, project='letitbit.net'):
args = {
'login': login,
'pass': passwd,
'project': project
}
self.add_method('user', 'register', args)
response = self.run()
if response['status'] != 'OK':
raise NotSuccessfulResponseException(response['status'])
return response['data'][0]
def assume_user(self, login, passwd, project='letitbit.net'):
"""Allows you to change current user"""
args = {
'login': login,
'pass': passwd,
'project': project
}
self.add_method('user', 'assume', args)
response = self.run()
if response['status'] != 'OK':
raise NotSuccessfulResponseException(response['status'])
def get_skymonk_link(self, uid):
"""Returns skymonk installation file link for chosen file"""
args = {
'uid': uid
}
self.add_method('preview', 'skymonk_link', args)
response = self.run()
if response['status'] != 'OK':
raise NotSuccessfulResponseException(response['status'])
return response['data'][0]
def get_flv_image(self, uid):
"""Returns flv video images link for chosen file"""
args = {
'uid': uid
}
self.add_method('preview', 'flv_image', args)
response = self.run()
if response['status'] != 'OK':
raise NotSuccessfulResponseException(response['status'])
return response['data'][0]
def get_flv_paste_code(self, link, width=600, height=450):
"""Returns html code which you can paste in your pages"""
file_info = self.get_file_info()
uid = file_info['uid']
paste_code = "<script language=\"JavaScript\" type=\"text/javascript\" src=\"http://moevideo.net/video.php?file={}&width={}&height={}\"></script>".format(uid, width, height)
return paste_code
def convert_videos(self, files_uids, login, password):
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'log': login, 'pas': password, 'inout': ""})
# try to login twice, first time you will just get cookies
opener.open('http://lib.wm-panel.com/wm-panel/user/signin-do', login_data)
opener.open('http://lib.wm-panel.com/wm-panel/user/signin-do', login_data)
msg = dict()
msg['path'] = 'ROOT/HOME/letitbit.net'
msg['fileuids[]'] = files_uids
convert_data = urllib.urlencode(msg, True)
opener.open('http://lib.wm-panel.com/wm-panel/File-Manager-Ajax?module=fileman_myfiles§ion=ajax&page=grid_action_convert', convert_data)