This repository has been archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
120 lines (110 loc) · 4.36 KB
/
cli.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
import sys, getopt
import download, result
class Cli:
help = {
'search': 'Usage : search show <name> {ep} <number_episode> {res} <resolution>\n {} : not mandatory\n <> argument',
'download': 'Usage : download <number> client <torrent_client>'}
results=None
def __init__(self, args):
if len(args) == 1:
self.prompt()
elif args:
self.args(args)
def args(self, argv):
show_ep = None
quality = None
torrent_client = str()
show_name = None
url_RSS = 'https://nyaa.si/rss?c=1_2&q=%title%'
try:
opts, args = getopt.getopt(argv, "hc:u:s:n:q:", ["help", "client=", "url=", "show=", "num=", "quality="])
except getopt.GetoptError:
print(help)
sys.exit(2)
for opt, arg in opts:
if opt == "":
self.prompt()
elif opt in ("-h", "help"):
print(help)
sys.exit()
elif opt in ("-c", "--client"):
torrent_client = arg
elif opt in ("-u", "--url"):
url_RSS = str(arg)
elif opt in ("-s", "--show"):
show_name = arg
elif opt in ("-n", "--num"):
show_ep = int(arg)
elif opt in ("-q", "--quality"):
quality = arg
if show_name == None:
show_name = input('enter show name')
self.results = result.Result(url_RSS, show_name, show_ep, quality)
def prompt(self):
show_ep = None
quality = None
torrent_client = str()
show_name = []
url_RSS = "https://nyaa.si/rss?c=1_2&q=%title%&f=%pagenumber%"
prompt = input("enter command >>> ").split(" ")
while prompt[0] not in ['search', 'download', 'help', 'exit']:
print('enter a proper command\n if you want the command list type help ')
self.prompt()
if prompt[0] == 'exit':
exit(0)
if prompt[0] == 'search':
if len(prompt) == 1 or 'show' not in prompt[1:]:
print(self.help['search'])
self.prompt()
else:
try:
for show_result in prompt[prompt.index('show')+1:]:
if show_result == 'ep' or show_result == "res":
break
else:
show_name.append(show_result)
show_name = " ".join(show_name)
except IndexError:
print('Missing name')
print(self.help[prompt[0]])
self.prompt()
if 'res' in prompt[1:]:
try:
quality = prompt[prompt.index('res') + 1]
except IndexError:
print('Missing resolution')
print(self.help[prompt[0]])
self.prompt()
if 'ep' in prompt[1:]:
try:
show_ep = int(prompt[prompt.index('ep') + 1])
except IndexError:
print('Missing number')
print(self.help[prompt[0]])
self.prompt()
self.results = result.Result(url_RSS, show_name, show_ep, quality)
self.showResult()
self.prompt()
if prompt[0] == 'help':
if len(prompt) == 1 or prompt[1] not in self.help.keys():
print('available help :')
for help in self.help.keys():
print('help ' + str(help))
elif prompt[1] in self.help.keys():
print(prompt[1] + ' help : ' + self.help[prompt[1]])
self.prompt()
if prompt[0] == 'download':
if len(prompt) == 1:
print(self.help[prompt[0]])
else:
try:
if len(prompt) == 2 and self.results:
download.Download(None, int(prompt[1])-1, self.results)
except UnboundLocalError:
print("do a research before downloading")
self.prompt()
def showResult(self):
for key, value in self.results.returnResult().items():
print(str(key) + " " + " ".join(value))
if __name__ == "__main__":
i = Cli(sys.argv[1:])