-
Notifications
You must be signed in to change notification settings - Fork 0
/
hubiCLI.py
executable file
·1093 lines (781 loc) · 30.8 KB
/
hubiCLI.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License
#
# hubiC is a product of OVH ( http://www.ovh.fr/hubiC/ )
#
# Author of this script : Ujoux ( http://forum.ovh.com ), i'm not affiliated with OVH
"""
hubiCLI.py : Command Line Interface to hubiC
Usage:
hubiCLI.py [-v] [-l LOGIN] [-p PASSWORD] --list [SRC]
hubiCLI.py [-v] [-l LOGIN] [-p PASSWORD] --download FILES ...
hubiCLI.py [-v] [-l LOGIN] [-p PASSWORD] --download-folder SRC
hubiCLI.py [-v] [-l LOGIN] [-p PASSWORD] --download-zip SRC FILES ...
hubiCLI.py [-v] [-l LOGIN] [-p PASSWORD] --upload DEST FILES ...
hubiCLI.py [-v] [-l LOGIN] [-p PASSWORD] --upload-folder SRC DEST
hubiCLI.py [-v] [-l LOGIN] [-p PASSWORD] --move SRC DEST FILE
hubiCLI.py [-v] [-l LOGIN] [-p PASSWORD] --rename SRC OLD NEW
hubiCLI.py [-v] [-l LOGIN] [-p PASSWORD] --delete SRC NAME
hubiCLI.py [-v] [-l LOGIN] [-p PASSWORD] --create-folder SRC NAME
hubiCLI.py [-v] [-l LOGIN] [-p PASSWORD] --publish SRC NAME [DURATION] [MESSAGE]
hubiCLI.py [-v] [-l LOGIN] [-p PASSWORD] --unpublish SRC NAME
hubiCLI.py -h | --help
hubiCLI.py --version
Arguments:
DURATION duration of publication in days
Options:
-v --verbose print status messages
-l=<LOGIN> --login=<LOGIN> use specified login
-p=<PASSWORD> --password=<PASSWORD> use specified password
Examples:
hubiCLI.py -v --list "/remote/folder"
hubiCLI.py -v --download "/folder/file1.ext" "/folder/file2.ext" "/folder/file3.ext"
hubiCLI.py -v --download-zip "/folder" "/folder/file1.ext" "/folder/file2.ext" "/folder/file3.ext"
hubiCLI.py -v --upload "/remote/folder" "/local/file1.ext" "/local/file2.ext" "/local/file3.ext"
hubiCLI.py -v --upload-folder "/my/local/folder" "/remote/folder"
"""
# do not edit before
# modify with your username and password
username = ''
password = ''
userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11'
# do not edit beyond
import sys, copy
import sched, time, math, mimetypes, datetime
import os, random, json
from pprint import pprint
import multiprocessing
from multiprocessing import Value, Lock, Process, Array
from ctypes import c_int, c_char_p
import ctypes
import re
import urllib
import urllib2
import curses
import time
import codecs
# you will probably have to install those modules (docopt, requests, progressbar)
from docopt import docopt
import requests
import progressbar
from progressbar import Bar, FileTransferSpeed, Percentage, ProgressBar
# fix unicode printing
streamWriter = codecs.lookup('utf-8')[-1]
sys.stdout = streamWriter(sys.stdout)
__author__ = "Ujoux"
__copyright__ = "Copyleft"
__credits__ = ["Ujoux"]
__license__ = "CC-BY-SA 3.0"
__version__ = "0.1"
__maintainer__ = "Ujoux"
__status__ = "Development"
headers = ''
oldSize = ''
sessionHash = ''
used = ''
quota = ''
accountType = ''
version = ''
email = ''
maxFileSize = ''
filesList = ''
filesCount = ''
fileFound = False
foundFile = None
PHPSESSID = None
HUBICID = None
stdscr = None
FILESTODOWN = []
FILESTOUP = []
DLTOGO = Value(c_int)
DLTOGO_LOCK = Lock()
UPTOGO = Value(c_int)
UPTOGO_LOCK = Lock()
if __name__ != '__main__':
sys.exit(0)
arguments = docopt(__doc__, version='0.1')
if arguments['--login'] != None:
username = arguments['--login']
if arguments['--password'] != None:
password = arguments['--password']
class UnknownLength: pass
class CustProgressBar(ProgressBar):
def updateBuffer(self, i, arr, msg, lock):
global stdscr
now = time.time()
self.seconds_elapsed = now - self.start_time
self.next_update = self.currval + self.update_interval
with lock:
arr[i] = msg
stdscr.clear()
j = 0
for dlmsg in arr:
if dlmsg != '':
stdscr.addstr(j, 0, dlmsg)
j += 1
stdscr.refresh()
self.last_update_time = now
return
def update(self, value=None, pid=None, arr=None, lock=None):
"""Updates the ProgressBar to a new value."""
if value is not None and value is not UnknownLength:
if (self.maxval is not UnknownLength
and not 0 <= value <= self.maxval):
if value >= self.maxval:
self.finish()
else:
raise ValueError('Value out of range')
self.currval = value
if not self._need_update(): return
if self.start_time is None:
raise RuntimeError('You must call "start" before calling "update"')
if pid != None:
self.updateBuffer(pid, arr, self._format_line(), lock)
class Consumer(multiprocessing.Process):
def __init__(self, task_queue, result_queue, arr, lock):
multiprocessing.Process.__init__(self)
self.task_queue = task_queue
self.result_queue = result_queue
self.arr = arr
self.lock = lock
def run(self):
proc_name = self.name
while True:
next_task = self.task_queue.get()
if next_task is None:
self.task_queue.task_done()
break
answer = next_task(arr=self.arr, lock=self.lock)
self.task_queue.task_done()
self.result_queue.put(answer)
return
class Task(object):
def __init__(self, a, pid):
self.a = a
self.pid = pid
def __call__(self, arr=None, lock=None):
realDownload(self.a, self.pid, arr, lock)
return self.a
def __str__(self):
return 'ARC'
def run(self):
print("IN")
class TaskUpload(object):
def __init__(self, folder, a, pid):
self.a = a
self.pid = pid
self.folder = folder
def __call__(self, arr=None, lock=None):
realUpload(self.folder, self.a, self.pid, arr, lock)
return self.a
def __str__(self):
return 'ARC'
def run(self):
print("IN")
# Session() to keep authentication cookies
sess = requests.Session()
print("Starting hubiCLI...")
print
def get_cookie_by_name(cj, name):
return [cookie for cookie in cj if cookie.name == name][0]
def login():
global PHPSESSID
global HUBICID
if username == '' or password == '':
print("No username or password specified")
sys.exit(0)
headers = {'User-Agent': userAgent, 'Origin': 'https://app.hubic.me'}
payload = {'sign-in-email': username, 'sign-in-password': password, 'sign-in-action': 'true'}
r = sess.post("https://app.hubic.me/v2/actions/nasLogin.php", data=payload, headers=headers)
logResp = ''
for cookie in sess.cookies:
if cookie.name == 'PHPSESSID':
PHPSESSID = cookie.value
if cookie.name == 'app.hubic.me':
HUBICID = cookie.value
if cookie.name == 'HUBIC_ACTION_RETURN':
logResp = cookie.value
logResp = urllib2.unquote(logResp.encode("utf8"))
if logResp != '':
logResp = json.JSONDecoder('utf8').decode(logResp)
statusCode = logResp['answer']['status']
message = logResp['answer']['login']['message']
message = message.replace('+', ' ')
print("Login failed -> %s (%d)" % (message, statusCode))
sys.exit(0)
if arguments['--verbose'] == True :
print("* Successfuly logged into hubiC")
print
return;
def getSettings():
global userAgent
headers = {'User-Agent': userAgent, 'Origin': 'https://app.hubic.me'}
r = sess.post("https://app.hubic.me/v2/actions/ajax/getSettings.php", headers=headers)
resp = r.json()
if resp['answer'] and resp['answer']['status'] and resp['answer']['status'] == 200:
print("* Successfuly retrieved settings")
print
global email
email = resp['answer']['settings']['hubic']['email']
global sessionHash
sessionHash = resp['answer']['settings']['hubic']['sessionHash']
global accountType
accountType = resp['answer']['settings']['hubic']['offer']
global used
used = int(resp['answer']['settings']['hubic']['used'])
global quota
quota = int(resp['answer']['settings']['hubic']['quota'])
global version
version = resp['answer']['settings']['hubic']['version']
global maxFileSize
maxFileSize = int(resp['answer']['settings']['settings']['MAX_FILE_SIZE'])
if arguments['--verbose'] == True :
print("Email : %s" % (email))
print("Account type : %s" % (accountType))
print("Used : %s out of %s (%d%%)" % (sizeof_fmt(used), sizeof_fmt(quota), (used * 100) / quota))
print("Version : %s" % (version))
print("Max file size : %s" % (sizeof_fmt(maxFileSize)))
print
else :
print("* Retrieving settings failed !")
print
return;
def listFiles(folder = '/', filesOnly=False):
global FILESTODOWN
global filesList
data = filesList
FILESTODOWN = []
walkDict(data, filterFor, folder)
for f in FILESTODOWN:
if filesOnly and f['type'] == 'application/directory':
continue
if arguments['--verbose'] == True:
print(', '.join([f['uri'], f['type'], f['creation'], f['modified'], str(f['size'])]))
else:
print(f['uri'])
return FILESTODOWN;
def getFilesList(folder = '/'):
global filesList
headers = {'User-Agent': userAgent, 'Origin': 'https://app.hubic.me'}
payload = {'action': 'get', 'folder': folder, 'container': '', 'init': 'true'}
r = sess.post("https://app.hubic.me/v2/actions/ajax/hubic-browser.php", data=payload, headers=headers)
if r.status_code != 200:
print("* Get files list NOT OK !!!")
print
return
filesList = json.JSONDecoder('latin1').decode(r.text)
if filesList['answer'] and filesList['answer']['status'] and filesList['answer']['status'] == 200:
print("* Get files list OK")
print
global filesCount
filesCount = filesList['answer']['hubic']['list']['default']['props']['count']
if arguments['--verbose'] == True :
print("Files count : %d" % filesCount)
print
else :
print("* Get files list NOT OK !!!")
print
return;
return;
def uploadBunch(folder, names):
global stdscr
global UPTOGO
global UPTOGO_LOCK
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
with UPTOGO_LOCK:
UPTOGO.value = len(names)
tasks = multiprocessing.JoinableQueue()
results = multiprocessing.Queue()
manager = multiprocessing.Manager()
arr = manager.list(['']*len(names))
lock = multiprocessing.Lock()
num_consumers = multiprocessing.cpu_count() * 2
consumers = [Consumer(tasks, results, arr, lock) for i in xrange(num_consumers)]
for w in consumers:
w.start()
pid=0
for name in names:
tasks.put(TaskUpload(os.path.join(folder, os.path.dirname(name)), name, pid))
pid += 1
for i in xrange(num_consumers):
tasks.put(None)
tasks.join()
return
def upload(folder, names):
global stdscr
global UPTOGO
global UPTOGO_LOCK
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
with UPTOGO_LOCK:
UPTOGO.value = len(names)
tasks = multiprocessing.JoinableQueue()
results = multiprocessing.Queue()
manager = multiprocessing.Manager()
arr = manager.list(['']*len(names))
lock = multiprocessing.Lock()
num_consumers = multiprocessing.cpu_count() * 2
consumers = [Consumer(tasks, results, arr, lock) for i in xrange(num_consumers)]
for w in consumers:
w.start()
pid=0
for name in names:
tasks.put(TaskUpload(os.path.join(folder, os.path.dirname(name)), name, pid))
pid += 1
for i in xrange(num_consumers):
tasks.put(None)
tasks.join()
return
class Progress(object):
def __init__(self):
self._seen = 0.0
def update(self, total, size, bar, pid, arr, lock):
self._seen += size
bar.update(self._seen, pid, arr, lock)
if self._seen >= total:
bar.finish()
class file_with_callback(file):
def __init__(self, path, mode, callback, bar, pid, arr, lock, *args):
file.__init__(self, path, mode)
self.seek(0, os.SEEK_END)
self._total = self.tell()
self.seek(0)
self._callback = callback
self._args = args
self.bar = bar
self.pid = pid
self.arr = arr
self.lock = lock
def __len__(self):
return self._total
def read(self, size):
data = file.read(self, size)
self._callback(self._total, len(data), self.bar, self.pid, self.arr, self.lock, *self._args)
return data
def uploadAll(folderSrc, folderDst):
global FILESTOUP
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
FILESTOUP = []
dirs = []
createFolder(os.path.dirname(os.path.join(folderDst, folderSrc)), os.path.basename(folderSrc), True)
for (dirpath, dirnames, filenames) in os.walk(folderSrc):
for dirname in dirnames:
dirs.append(os.path.join(dirpath, dirname))
for filename in filenames:
FILESTOUP.append(os.path.join(dirpath, filename))
for d in dirs:
createFolder(os.path.join(folderDst, os.path.dirname(d)), os.path.basename(d), True)
uploadBunch(folderDst, FILESTOUP)
return
def realUpload(folder, name, pid, arr, lock):
global sessionHash
global foundFile
global PHPSESSID
global HUBICID
global UPTOGO
global UPTOGO_LOCK
inDirFilename = os.path.basename(name)
in_file = open(name, "r")
fileData = in_file.read()
in_file.close()
fileSize = getLocalFileSize(name)
mime = mimetypes.guess_type(name)[0]
realFilename = os.path.basename(name)
headers = {'User-Agent': userAgent, 'Origin': 'https://app.hubic.me', 'Host': 'app.hubic.me'}
cookies = {'PHPSESSID': PHPSESSID, 'app.hubic.me': HUBICID}
conn = requests.Session()
cs = sizeof_fmt(fileSize)
widgets = [name, ": ", Bar(marker="|", left="[", right=" "), ' ', Percentage(), ' ', FileTransferSpeed(), "] ", " of ", cs," "]
pbar = CustProgressBar(widgets=widgets, maxval=fileSize).start()
path = name
progress = Progress()
stream = file_with_callback(path, 'r', progress.update, pbar, pid, arr, lock)
headers = {'User-Agent': userAgent, 'Origin': 'https://app.hubic.me', 'X-File-Size': fileSize, 'X-File-Dest': folder, 'X-Action': 'upload', 'X-File-Container': 'default', 'X-File-Name': realFilename, 'X-File-Type': mime, 'Content-Type': mime}
r = conn.put("http://app.hubic.me/v2/actions/ajax/hubic-browser.php", data=stream, headers=headers, cookies=cookies, stream=True)
answer = r.json()
if answer['answer'] and answer['answer']['status'] and answer['answer']['status'] == 201:
with UPTOGO_LOCK:
UPTOGO.value -= 1
if UPTOGO.value == 0:
time.sleep(1)
stdscr.erase()
curses.echo()
curses.nocbreak()
curses.endwin()
os.system('reset')
logout()
return;
def realDownload(name, pid, arr, lock):
global sessionHash
global foundFile
global PHPSESSID
global HUBICID
global DLTOGO
global DLTOGO_LOCK
foundFile = getFile(name)
filename = os.path.basename(name)
#if foundFile is None or foundFile['size'] is UnknownLength:
#print "* Download of %s failed ! -> file not found" % filename
#print
#return
fsize = foundFile['size']
thetype = foundFile['type']
key = int(math.floor(random.random()*(time.time())))
headers = {'User-Agent': userAgent, 'Origin': 'https://app.hubic.me', 'Host': 'app.hubic.me'}
cookies = {'PHPSESSID': PHPSESSID, 'app.hubic.me': HUBICID}
conn = requests.Session()
folder = ''
r = conn.get("https://app.hubic.me/v2/actions/ajax/hubic-browser.php?action=download&folder=%s&container=default&name=%s&key=%s&isFile=true&size=%s&type=%s&secret=%s" % (folder, name, key, fsize, thetype, sessionHash), headers=headers, stream=True, cookies=cookies)
progress = Progress()
dsize = int(r.headers['Content-Length'].strip())
bytes = 0
cs = sizeof_fmt(dsize)
widgets = [name, ": ", Bar(marker="|", left="[", right=" "), Percentage(), " ", FileTransferSpeed(), "] ", '', " of ", cs," "]
pbar = CustProgressBar(widgets=widgets, maxval=dsize).start()
realFilename = os.path.basename(filename)
realFile = open(realFilename, 'wb')
for buf in r.iter_content(1024, True):
if buf:
realFile.write(buf)
bytes += len(buf)
progress.update(dsize, 1024, pbar, pid, arr, lock)
realFile.close()
statinfo = os.stat(realFilename)
savedSize = statinfo.st_size
with DLTOGO_LOCK:
DLTOGO.value -= 1
if DLTOGO.value == 0:
time.sleep(1)
stdscr.erase()
curses.echo()
curses.nocbreak()
curses.endwin()
os.system('reset')
logout()
return;
def downloadAll(folder):
global DLTOGO
global DLTOGO_LOCK
global FILESTODOWN
global filesList
data = filesList
walkDict( data, filterFor, folder)
names = []
for f in FILESTODOWN:
names.append(f['uri'])
download(names)
return
def encodeURI(aString):
return urllib.quote(unicode(aString).encode('utf-8'), safe='~@#$&()*!+=:;,.?/\'');
def downloadZipped(folder, names):
global sessionHash
global foundFile
global PHPSESSID
global HUBICID
global DLTOGO
global DLTOGO_LOCK
key = int(round(time.time() * 1000))
headers = {'User-Agent': userAgent, 'Origin': 'https://app.hubic.me', 'Host': 'app.hubic.me'}
cookies = {'PHPSESSID': PHPSESSID, 'app.hubic.me': HUBICID}
conn = requests.Session()
folderSize = 0
for name in names:
foundFile = getFile(name)
if foundFile == None:
print("Skipping file not found : %s" % (name))
continue
filename = os.path.basename(name)
fsize = foundFile['size']
thetype = 'image/jpeg'
payload = {'action': 'download', 'dlname': encodeURI(folder), 'sub': 'launch', 'name': encodeURI(filename), 'uri': encodeURI(name), 'container': 'default', 'size': fsize, 'type': thetype, 'isFile': 'true', 'key': key}
r = conn.post("https://app.hubic.me/v2/actions/ajax/hubic-browser.php", data=payload, headers=headers, cookies=cookies)
a = json.JSONDecoder('utf8').decode(r.text)
if a['answer']['status'] and a['answer']['status'] == 200:
folderSize += a['answer']['download']['loaded']
zipName = os.path.basename(folder) + ".zip"
payload = {'action': 'download', 'sub': 'zip', 'dlPath': encodeURI(folder), 'dlName': zipName, 'isFile': 'true', 'key': key}
r = conn.post("https://app.hubic.me/v2/actions/ajax/hubic-browser.php", data=payload, headers=headers, cookies=cookies)
r = conn.get("https://app.hubic.me/v2/actions/ajax/hubic-browser.php?&action=download&name=%s&key=%s&isFile=false&size=%s&type=%s&secret=%s" % (encodeURI(folder), key, folderSize, 'application/zip', sessionHash), headers=headers, stream=True, cookies=cookies)
dsize = int(r.headers['Content-Length'].strip())
bytes = 0
cs = sizeof_fmt(dsize)
widgets = [zipName, ": ", Bar(marker="|", left="[", right=" "), Percentage(), " ", FileTransferSpeed(), "] ", '', " of ", cs," "]
pbar = ProgressBar(widgets=widgets, maxval=dsize).start()
realFilename = os.path.basename(zipName)
realFile = open(realFilename, 'wb')
for buf in r.iter_content(1024, True):
if buf:
realFile.write(buf)
bytes += len(buf)
pbar.update(bytes)
if bytes >= dsize:
pbar.finish()
pbar.finish()
realFile.close()
return
def download(names):
global stdscr
global DLTOGO
global DLTOGO_LOCK
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
with DLTOGO_LOCK:
DLTOGO.value = len(names)
tasks = multiprocessing.JoinableQueue()
results = multiprocessing.Queue()
manager = multiprocessing.Manager()
arr = manager.list(['']*len(names))
lock = multiprocessing.Lock()
num_consumers = multiprocessing.cpu_count() * 2
consumers = [Consumer(tasks, results, arr, lock) for i in xrange(num_consumers)]
for w in consumers:
w.start()
pid=0
for name in names:
tasks.put(Task(name, pid))
pid += 1
for i in xrange(num_consumers):
tasks.put(None)
tasks.join()
return
def move(src, dest, name):
oldFileURI = os.path.join(src, name)
newFileURI = os.path.join(dest, name)
oldFile = getFile(oldFileURI)
if oldFile == None:
print("File not found : %s" % (oldFileURI))
return
mime = oldFile['type']
headers = {'User-Agent': userAgent, 'Origin': 'https://app.hubic.me'}
payload = {'action': 'move', 'src' : src, 'srcContainer' : 'default', 'dest': dest, 'destContainer' : 'default', 'name': name, 'type': mime, 'isFile': 'true', 'force': 'false', 'isParentFolder': 'true', 'finalize' : 'false'}
r = sess.post("https://app.hubic.me/v2/actions/ajax/hubic-browser.php", data=payload, headers=headers)
resp = r.json()
if resp['answer'] and resp['answer']['status'] and resp['answer']['status'] == 201:
print("* Moved %s to %s OK" % (oldFileURI, newFileURI))
else :
print("* Move of %s to %s failed !" % (oldFileURI, newFileURI))
return;
def rename(src, old, new):
oldFileURI = os.path.join(src, old)
newFileURI = os.path.join(src, new)
oldFile = getFile(oldFileURI)
if oldFile == None:
print("File not found : %s" % (oldFileURI))
return
headers = {'User-Agent': userAgent, 'Origin': 'https://app.hubic.me'}
payload = {'action': 'rename', 'folder': src, 'container' : 'default', 'old': old, 'new': new, 'finalize' : 'false', 'itemIndex' : 'undefined', 'isParentFolder' : 'true', 'isFile': 'true'}
r = sess.post("https://app.hubic.me/v2/actions/ajax/hubic-browser.php", data=payload, headers=headers)
resp = r.json()
if resp['answer'] and resp['answer']['status'] and resp['answer']['status'] == 201:
print("* Rename of %s to %s OK" % (oldFileURI, newFileURI))
else :
print("* Rename of %s to %s failed !" % (oldFileURI, newFileURI))
return;
def createFolder(folder, name, quiet=False):
folderURI = os.path.join(folder, name)
headers = {'User-Agent': userAgent, 'Origin': 'https://app.hubic.me'}
payload = {'action': 'create', 'folder' : folder, 'container' : 'default', 'name': name}
r = sess.post("https://app.hubic.me/v2/actions/ajax/hubic-browser.php", data=payload, headers=headers)
resp = r.json()
if quiet == False:
if resp['answer'] and resp['answer']['status'] and resp['answer']['status'] == 201:
print("* Created folder %s OK" % folderURI)
else :
print("* Creation of %s failed !" % folderURI)
return;
def delete(folder, name):
fileURI = os.path.join(folder, name)
theFile = getFile(fileURI)
if theFile == None:
print("File not found : %s" % (fileURI))
return
mime = theFile['type']
isFile = (mime != 'application/directory')
headers = {'User-Agent': userAgent, 'Origin': 'https://app.hubic.me'}
payload = {'action': 'remove', 'folder' : folder, 'container' : 'default', 'name': name, 'type': mime, 'isFile': isFile}
r = sess.post("https://app.hubic.me/v2/actions/ajax/hubic-browser.php", data=payload, headers=headers)
resp = r.json()
if resp['answer'] and resp['answer']['status'] and resp['answer']['status'] == 204:
print("* Deleted %s OK" % fileURI)
else :
print("* Deletion of %s failed !" % fileURI)
return;
def publish(folder, name, duration = 10, message = ''):
fileURI = os.path.join(folder, name)
theFile = getFile(fileURI)
if theFile == None:
print("File not found : %s" % (fileURI))
return
mime = theFile['type']
isFile = (mime != 'application/directory')
headers = {'User-Agent': userAgent, 'Origin': 'https://app.hubic.me'}
payload = {'action': 'publish', 'folder': folder, 'name': name, 'container' : 'default', 'isFile': isFile, 'duration': duration, 'message': message}
r = sess.post("https://app.hubic.me/v2/actions/ajax/hubic-browser.php", data=payload, headers=headers)
resp = r.json()
if resp['answer'] and resp['answer']['status'] and resp['answer']['status'] == 200:
creation = resp['answer']['publicationItem']['creation']
expire = resp['answer']['publicationItem']['expire']
url = resp['answer']['publicationItem']['url']
creationF = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(creation)))
expireF = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(expire)))
print("* Publishing of %s OK" % fileURI)
print("Created on %s, expires on %s" % (creationF, expireF))
print("URL : %s" % (url))
else :
print("* Publishing of %s failed ! (is it already published ?)" % fileURI)
return;
def unpublish(folder, name):
fileURI = os.path.join(folder, name)
theFile = getFile(fileURI)
if theFile == None:
print("File not found : %s" % (fileURI))
return
mime = theFile['type']
isFile = (mime != 'application/directory')
headers = {'User-Agent': userAgent, 'Origin': 'https://app.hubic.me'}
payload = {'action': 'unpublish', 'folder': folder, 'name': name, 'container' : 'default', 'isFile': isFile}
r = sess.post("https://app.hubic.me/v2/actions/ajax/hubic-browser.php", data=payload, headers=headers)
if r.status_code != 200:
print("* Unpublish of %s failed !" % fileURI)
return
resp = r.json()
if resp['answer'] and resp['answer']['status'] and resp['answer']['status'] == 200:
print("* Unpublish of %s OK" % fileURI)
else :
print("* Unpublish of %s failed ! (is it really published ?)" % fileURI)
return;
def walkDict( aDict, visitor, filename, path=() ):
global fileFound
if fileFound == True:
return;
for k in aDict:
if k == 'props':
visitor( path, aDict[k], filename )
elif type(aDict[k]) != dict:
pass
else:
walkDict( aDict[k], visitor, filename, path+(k,) )
def getFileInfos(filename):
global filesList
data = filesList
walkDict( data, filterFor, filename )
if foundFile :
return True
else:
return False
def fileExists(filename):
f = getFile(filename)
if f != None:
return true
else:
return false
def getFile(filename):
global filesList
data = filesList
return searchForFilename(data, searchForFilenameSub, filename)
def searchForFilename(aDict, visitor, filename, path=(), key='props'):
for k in aDict:
if k == 'props':
f = visitor(path, aDict[k], filename)
if f != None:
return f
elif type(aDict[k]) != dict:
pass
else:
f = searchForFilename(aDict[k], visitor, filename, path+(k,))
if f != None:
return f
def searchForFilenameSub(path, element, filename, key='uri'):
try:
element['uri']
except KeyError:
pass
else:
if element['uri'] == filename:
return element
else:
return None;
def filterFor(path, element, filename):
global FILESTODOWN
try: