-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
81 lines (71 loc) · 1.72 KB
/
main.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
from api import api,downloader
import sys
import re
import os
import getpass
import error
import utils
COMMANDS = {}
def register(command):
def wrapper(func):
COMMANDS[re.compile(command)] = func
return func
return wrapper
def find_match(command):
ret = []
for i,j in COMMANDS.items():
if i.search(command):
ret.append((len(i.search(command).group()),j))
# print(ret)
if not ret:
raise error.BadCommand
return sorted(ret,key=lambda x:x[0])[-1][1]
@register('help')
@register('-h')
def usage(foo=None):
print('Too lazy to write a help')
@register(r'down ([\w\W]+?)+')
def down(command):
if not api.checkLogin():
print('You should login first')
login('login')
command = command[1:]
ids = {}
for i in command:
path,filename = os.path.split(i)
ret =[(f['fs_id'],filename) for f in api.getFileList(path) if f['server_filename'] == filename and not f['isdir']]
if ret:
ids[ret[0][0]] = {'filename':ret[0][1]}
else:
print('Get {} info failed'.format(utils.shortStr(i,25)))
ret = api.getFilesLink(ids.keys())
# print(ids)
for i in ret:
ids[int(i['fs_id'])]['link'] = i['dlink']
# print(ids)
downloader.download(ids.values())
@register('login')
def login(command):
if api.checkLogin():
return
username = input('Username:')
password = getpass.getpass()
print('logining, please wait for a while....')
try:
api.login(username, password)
except error.ApiError:
print('Login Failed! Check your username & password please.')
def main():
if len(sys.argv) < 2:
usage()
sys.exit()
command = ' '.join(sys.argv[1:])
try:
find_match(command)(sys.argv[1:])
api.syncCookie()
api.storeConfig()
except error.BadCommand:
print('Bad Command,plz look the help')
usage()
if __name__ == '__main__':
main()