-
Notifications
You must be signed in to change notification settings - Fork 0
/
nyaaget
executable file
·154 lines (128 loc) · 4.27 KB
/
nyaaget
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
#!/usr/bin/env python3
import feedparser
import sys
import os
from urllib.parse import urlencode
search_url = "https://nyaa.si/?page=rss&"
add_command = "qbittorrent-nox '{0}' > /dev/null"
def get_search(search, sort=1, page=1, user=None):
args={
'q': search,
'sort': sort,
'offset': page
}
if user:
args['u'] = user
url = search_url + urlencode(args)
feed = feedparser.parse(url)
result = list()
for entry in feed['entries']:
title = entry['title']
link = entry['link']
seed_s = entry['nyaa_seeders']
size = entry['nyaa_size']
# Pretty print seeders
seed_i = int(seed_s)
if seed_i >= 1000:
seed_i = round(seed_i / 1000)
seeders = "{}k seeders".format(seed_i)
else:
seeders = "{} seeders".format(seed_i)
result.append((title, link, seeders, size))
return result
class Pager:
page = 1
sort = 1
search = ""
user = None
results_buf = []
class SORT_MODE:
DATE = 1
SEEDERS = 2
def __init__(self, results_per_page):
self.results_per_page = results_per_page
def __reset(self):
self.page = 1
self.results_buf = []
def __refill(self):
self.results_buf.extend(get_search(self.search, self.sort, self.page, self.user))
self.page = self.page + 1
def set_search(self, search, user=None, sort=0):
self.__reset()
self.search = search
if user:
self.user = user
if sort:
self.sort = sort
def set_sort(self, sort):
self.__reset()
self.sort = sort
def next_page(self):
if(len(self.results_buf) < self.results_per_page):
self.__refill()
ret_list = self.results_buf[:self.results_per_page]
self.results_buf = self.results_buf[self.results_per_page:]
return ret_list
def add_magnet(magnet):
os.system(add_command.format(magnet))
if __name__ == "__main__":
page = 1
search = ""
user = ""
search_pager = Pager(20)
if len(sys.argv) > 1:
if sys.argv[1] == "--user":
user = sys.argv[2]
search = " ".join(sys.argv[3:])
search_pager.set_search(search, user=user)
else:
search = " ".join(sys.argv[1:])
search_pager.set_search(search)
results = search_pager.next_page()
while True:
# Terminal size
columns = int(os.popen('stty size', 'r').read().split()[1])
# Print header
header = "--- {0} --- Page {1} ---".format(search, page)
print("{1:-<{0}}".format(columns, header))
# Print results
for i in range(0, len(results)):
result_left = "{0:02d}) {1}".format(i + 1, results[i][0])
result_right = "[{0:>11} :: {1:9}]".format(results[i][2], results[i][3])
# print(result_left)
# print(result_right)
print("{1:{0}.{0}}{2}".format(columns-len(result_right), result_left, result_right))
try:
s = input('--> ')
print(s)
except KeyboardInterrupt:
exit()
if s == "":
page = page + 1
results = search_pager.next_page()
continue
elif s == "q":
break
elif s == "date":
page = 1
search_pager.set_sort(Pager.SORT_MODE.DATE)
results = search_pager.next_page()
elif s == "seed":
page = 1
search_pager.set_sort(Pager.SORT_MODE.SEEDERS)
results = search_pager.next_page()
elif s.split(' ')[0].isdigit():
toget = s.split(' ')
for x in toget:
if x.isdigit() and int(x) >= 1 and int(x) <= len(results):
add_magnet(results[int(x) - 1][1])
elif s.split('-')[0].isdigit() and s.split('-')[1].isdigit():
toget = s.split('-')
for x in range(int(toget[0]), int(toget[1]), 1 if int(toget[0]) < int(toget[1]) else -1):
add_magnet(results[int(x) - 1][1])
add_magnet(results[int(toget[1]) - 1][1])
else:
page = 1
search = s
search_pager.set_search(search)
results = search_pager.next_page()