-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpirateship.py
executable file
·178 lines (147 loc) · 6.15 KB
/
pirateship.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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2022 i7heaven <[email protected]>
#
# Distributed under terms of the MIT license.
import requests, re, json, sys, subprocess, urllib
from rich.table import Table
from rich.console import Console
from rich import box
from os.path import expanduser
PIRATE_URL = "https://thepiratebay.org"
PIRATE_API_URL = "https://apibay.org"
MAGNET_FORMAT = "magnet:?xt=urn:btih:{}&dn={}"
PROXY_HOST=''
PROXY_PORT=''
MAIN_CAT = {'0' : '???'}
SUB_CAT = {'0' : '???'}
raw_main_js = None
try:
with open(expanduser("~") + "/.pirateship/config") as f:
for line in f.readlines():
if not line.startswith('#') and not line.find("PROXY_HOST=") == -1:
PROXY_HOST = line[11:].rstrip('\n')
elif not line.startswith('#') and not line.find("PROXY_PORT=") == -1:
PROXY_PORT = line[11:].rstrip('\n')
elif not line.startswith('#') and not line.find("PIRATE_URL=") == -1:
PIRATE_URL = line[11:].rstrip('\n')
elif not line.startswith('#') and not line.find("PIRATE_API_URL=") == -1:
PIRATE_API_URL = line[15:].rstrip('\n')
except FileNotFoundError:
pass
def request(url, params={}):
if len(PROXY_HOST) > 0 and len(PROXY_PORT) > 0:
return requests.get(url, params, proxies=dict(http="socks5h://" + PROXY_HOST + ":" + PROXY_PORT, https="socks5h://" + PROXY_HOST + ":" + PROXY_PORT))
else:
return requests.get(url, params)
def get_readable_size(size, decimal_places=2):
for unit in ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB']:
if size < 1024.0 or unit == 'PiB':
break
size /= 1024.0
return f"{size:.{decimal_places}f} {unit}"
def ensure_main_js():
global raw_main_js
if raw_main_js == None:
raw_main_js = request(PIRATE_URL + "/static/main.js")
def fillin_categories():
global raw_main_js
ensure_main_js()
raw_categories = ""
for main_line in raw_main_js.iter_lines():
#if re.search("function print_category", str(main_line)):
raw_categories = main_line.decode("utf-8")
if len(raw_categories) > 0:
pattern = "cc\[0\][ ]{0,1}==[ ]{0,1}[0-9]{1}\)[ ]{0,1}main[ ]{0,1}=[ ]{0,1}'[a-zA-Z0-9-_ \(\)\/]+'|cat[ ]{0,1}==[ ]{0,1}[0-9]{3}\)[ ]{0,1}return maintxt[ ]{0,1}\+[ ]{0,1}'[a-zA-Z0-9-_ \(\)\/]*'"
matches = re.findall(pattern, raw_categories)
for match in matches:
if match.startswith("cc[0]"):
num = "(?<!\[)[0-9]{1}(?!])"
ptn = "(?<=')[a-zA-Z0-9-_ \(\)\/]*(?=')"
#num = "((?!cc\[0\]==)|(?!cc\[0\] == ))[0-9]{1}(?=\))"
#ptn = "((?!main=')|(?!main = ))[a-zA-Z0-9-_ \(\)\/]+(?=')"
cc_matches = re.findall(ptn, match)
num_matches = re.findall(num, match)
MAIN_CAT[num_matches[0]] = cc_matches[0]
else:
num = "[0-9]{1,3}"
ptn = "(?<=')[a-zA-Z0-9-_ \(\)\/]+(?=')"
cat_matches = re.findall(ptn, match)
num_matches = re.findall(num, match)
SUB_CAT[num_matches[0]] = cat_matches[0]
def get_trackers():
global raw_main_js
ensure_main_js()
tracker_list = []
raw_trackers = ""
for main_line in raw_main_js.iter_lines():
if re.search("function print_trackers", str(main_line)):
raw_trackers = main_line.decode("utf-8")
if len(raw_trackers) > 0:
pattern = "(?!encodeURIComponent\()[^\(]+(?=\))"
matches = re.findall(pattern, raw_trackers)
if len(matches) > 0:
for tracker in matches:
tracker_list.append(urllib.parse.unquote(tracker.replace("'", "")))
return tracker_list
def get_category(category_num):
try:
main = MAIN_CAT[category_num[:1]]
sub = SUB_CAT[category_num]
except:
main = "not"
sub = "found"
return main + " > " + sub
def get_search_result_list(keyword):
result_list = []
converted_keyword = keyword.replace(' ', '+')
search = {'q' : converted_keyword, 'cat' : '0'}
raw_search_results = request(PIRATE_API_URL + "/q.php", search)
return json.loads(raw_search_results.content)
def search(keyword):
fillin_categories()
tracker_list = get_trackers()
search_result = get_search_result_list(keyword)
link_results = []
names = []
i = 0
table = Table(title="PirateShip", box=box.ROUNDED)
table.add_column("编号", justify="right", style="cyan")
table.add_column("类型", justify="right", style="magenta")
table.add_column("名称", justify="right", style="green")
table.add_column("编号", justify="right", style="cyan")
table.add_column("大小", justify="right", style="red")
for search in search_result:
magnet_link = MAGNET_FORMAT.format(search["info_hash"], urllib.parse.quote(search["name"]))
for tracker in tracker_list:
magnet_link = magnet_link + "&tr=" + urllib.parse.quote_plus(tracker)
table.add_row(str(i), get_category(search["category"]), search["name"], str(i), get_readable_size(int(search["size"])))
link_results.append(magnet_link)
names.append(search["name"])
i = i + 1
if len(link_results) > 0:
console = Console()
console.print(table)
while 1:
option = input("选择编号或输入exit退出:")
if option == "exit":
exit(0)
else:
try:
selected_index = int(option)
if selected_index < len(link_results) and selected_index >= 0:
print("名称:" + names[selected_index] + ",链接:" + link_results[selected_index])
break;
else:
raise ValueError("")
except ValueError:
print("编号错误, 请重新输入")
except KeyboardInterrupt:
exit(0);
if __name__ == "__main__":
if len(sys.argv) > 1:
search(sys.argv[1])
else:
print("缺少搜索关键字!")