forked from PeterDing/iScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bt.py
executable file
·386 lines (344 loc) · 14.1 KB
/
bt.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
#!/usr/bin/env python2
# vim: set fileencoding=utf8
import bencode
import os
import sys
import re
from hashlib import sha1
import base64
import requests
import urlparse
import argparse
############################################################
headers = {
"Connection": "keep-alive",
"Accept":"text/html,application/xhtml+xml,\
application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding":"gzip,deflate,sdch",
"Accept-Language":"en-US,en;q=0.8,zh-CN;\
q=0.6,zh;q=0.4,zh-TW;q=0.2",
"User-Agent":"Mozilla/5.0 (X11; Linux x86_64) \
AppleWebKit/537.36 (KHTML, like Gecko) \
Chrome/40.0.2214.91 Safari/537.36"
}
ss = requests.session()
ss.headers.update(headers)
s = u'\x1b[%d;%dm%s\x1b[0m' # terminual color template
letters = [i for i in '.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' \
+ '0123456789']
class bt(object):
def transfer(self, string, tpath, foo=None, bar=None):
self.dir_dict = {}
self.sub_dir_index = 0
dstring = bencode.bdecode(string)
files = []
file_index = 0
## change files' name
if dstring['info'].get('files'):
for fl in dstring['info']['files']:
filename = fl['path'][-1]
if args.type_ == 'n':
newfilename = re.sub(foo, bar, filename, re.I) \
if foo and bar else filename
if filename != newfilename:
print filename, s % (1, 92, '==>'), newfilename
path = [self._get_sub_dir_index(i) \
for i in fl['path'][:-1]] + [newfilename]
else:
ext = os.path.splitext(filename)[-1]
ext = self._check_ext(ext)
path = [self._get_sub_dir_index(i) \
for i in fl['path'][:-1]] \
+ ['%s%s' % (file_index, ext)]
file_index += 1
fl['path'] = path
elif args.type_ == 'be64':
fn, ext = os.path.splitext(filename)
ext = self._check_ext(ext)
tfn = '/'.join(fl['path'][:-1] + [fn])
e_fn = base64.urlsafe_b64encode(tfn)
fl['path'] = [e_fn + '.base64' + ext]
for item in fl.keys():
#if item not in ['path', 'length', 'filehash', 'ed2k']:
if item not in ['path', 'length', 'filehash']:
del fl[item]
files.append(fl)
dstring['info']['files'] = files
## change top directory
for i in dstring['info'].keys():
if i not in ['files', 'piece length', 'pieces', 'name', 'length']:
del dstring['info'][i]
elif 'name' in i:
if args.name:
dstring['info'][i] = args.name
## delete comment and creator
for i in dstring.keys():
if i not in ['creation date', 'announce', 'info', 'encoding']:
del dstring[i]
c = bencode.bencode(dstring)
with open(tpath, 'w') as g:
g.write(c)
def _get_sub_dir_index(self, dir_):
if not self.dir_dict.get(dir_):
self.dir_dict[dir_] = str(self.sub_dir_index)
self.sub_dir_index += 1
return self.dir_dict[dir_]
else:
return self.dir_dict[dir_]
def _check_ext(self, ext):
if len(ext) > 4:
return ''
for e in ext:
if e not in letters:
return ''
return ext
def get_torrent(self, hh):
print s % (1, 93, '\n ++ get torrent from web')
def do(url, proxies=None, data=None, timeout=None):
try:
r = ss.get(url, proxies=proxies, timeout=timeout)
cnt = r.content
if r.ok and cnt and '<head>' not in cnt \
and '4:name' in cnt:
print s % (1, 92, u' √ get torrent.')
return cnt
else:
print s % (1, 91, u' × not get.')
return None
except:
return None
## with xunlei
print s % (1, 94, ' >> try:'), 'bt.box.n0808.com'
url = 'http://bt.box.n0808.com/%s/%s/%s.torrent' \
% (hh[:2], hh[-2:], hh)
ss.headers['Referer'] = 'http://bt.box.n0808.com'
result = do(url)
if result: return result
## with https://torrage.com
if ss.headers.get('Referer'): del ss.headers['Referer']
if args.proxy:
print s % (1, 94, ' >> try:'), 'torrage.com'
proxies = {
'http': args.proxy} if args.proxy else None
url = 'http://torrage.com/torrent/%s.torrent' % hh
try:
result = do(url, proxies=proxies)
if result: return result
except:
print s % (1, 91, ' !! proxy doesn\'t work:'), args.proxy
## with http://btcache.me
if ss.headers.get('Referer'): del ss.headers['Referer']
print s % (1, 94, ' >> try:'), 'btcache.me'
url = 'http://btcache.me/torrent/%s' % hh
r = ss.get(url)
key = re.search(r'name="key" value="(.+?)"', r.content)
if key:
data = {
"key": key.group(1)
}
ss.headers['Referer'] = url
url = 'http://btcache.me/download'
result = do(url, data=data, proxies=proxies)
if result: return result
else:
print s % (1, 91, u' × not get.')
## some torrent stores
if ss.headers.get('Referer'): del ss.headers['Referer']
urls = [
#'http://www.sobt.org/Tool/downbt?info=%s',
'http://www.win8down.com/url.php?hash=%s&name=name',
#'http://www.31bt.com/Torrent/%s',
'http://178.73.198.210/torrent/%s',
'http://zoink.it/torrent/%s.torrent',
'http://torcache.net/torrent/%s.torrent',
'http://torrentproject.se/torrent/%s.torrent',
'http://istoretor.com/fdown.php?hash=%s',
'http://torrentbox.sx/torrent/%s',
'http://www.torrenthound.com/torrent/%s',
'http://www.silvertorrent.org/download.php?id=%s',
]
for url in urls:
print s % (1, 94, ' >> try:'), urlparse.urlparse(url).hostname
url = url % hh
try:
result = do(url)
if result: return result
except:
print s % (1, 91, ' !! Error at connection')
## with Vuze
#print s % (1, 94, ' >> try:'), 'magnet.vuze.com'
#if ss.headers.get('Referer'): del ss.headers['Referer']
#chh = base64.b32encode(binascii.unhexlify(hh))
#url = 'http://magnet.vuze.com/magnetLookup?hash=%s' % chh
#result = do(url)
#if result: return result
return False
def magnet2torrent(self, urls, dir_):
for url in urls:
hh = re.search(r'urn:btih:(\w+)', url)
if hh:
hh = hh.group(1).upper()
else:
print s % (1, 91, ' !! magnet is wrong.'), url
continue
string = self.get_torrent(hh)
if string:
tpath = os.path.join(dir_, hh + '.torrent')
print s % (1, 97, ' ++ magnet to torrent:'), \
'magnet:?xt=urn:btih:%s' % hh
with open(tpath, 'w') as g:
g.write(string)
else:
print s % (1, 91, ' !! Can\'t get torrent from web.'), url
def torrent2magnet(self, paths):
def trans(tpath):
if tpath.lower().endswith('torrent'):
string = open(tpath).read()
try:
dd = bencode.bdecode(string)
except Exception as e:
print s % (1, 91, ' !! torrent is wrong:'), e
info = bencode.bencode(dd['info'])
hh = sha1(info).hexdigest()
print '# %s' % tpath
print 'magnet:?xt=urn:btih:%s' % hh, '\n'
for path in paths:
if os.path.exists(path):
if os.path.isdir(path):
for a, b, c in os.walk(path):
for i in c:
tpath = os.path.join(a, i)
trans(tpath)
elif os.path.isfile(path):
tpath = path
trans(tpath)
else:
print s % (1, 91, ' !! file doesn\'t existed'), \
s % (1, 93, '--'), path
def change(self, ups, dir_, foo=None, bar=None):
for up in ups:
path = up
if path.startswith('magnet:'):
hh = re.search(r'urn:btih:(\w+)', path)
if hh:
hh = hh.group(1).upper()
else:
print s % (1, 91, ' !! magnet is wrong.'), path
string = self.get_torrent(hh)
if string:
tpath = os.path.join(dir_, hh + '.torrent')
print s % (1, 97, ' ++ transfer:'), \
'magnet:?xt=urn:btih:%s' % hh
self.transfer(string, tpath, foo=foo, bar=bar)
else:
print s % (1, 91, ' !! Can\'t get torrent from web.'), path
elif os.path.exists(path):
if os.path.isdir(path):
for a, b, c in os.walk(path):
for i in c:
ipath = os.path.join(a, i)
if i.lower().endswith('torrent'):
def do():
print s % (1, 97, ' ++ transfer:'), ipath
string = open(ipath).read()
tpath = os.path.join(dir_, 'change_' + i)
self.transfer(string, tpath, foo=foo,
bar=bar)
paths.update(ipath)
if os.getcwd() == os.path.abspath(dir_):
do()
elif os.getcwd() != os.path.abspath(dir_) and \
os.path.abspath(a) != os.path.abspath(dir_):
do()
elif os.path.isfile(path):
if path.lower().endswith('torrent'):
print s % (1, 97, ' ++ transfer:'), path
string = open(path).read()
tpath = os.path.join(dir_,
'change_' + os.path.basename(path))
self.transfer(string, tpath, foo=foo, bar=bar)
else:
print s % (1, 91, ' !! file doesn\'t existed'), \
s % (1, 93, '--'), path
def import_magnet(froms):
ml = []
m_re = re.compile(r'btih:([a-zA-Z0-9]{40})')
def get_magnet(cm):
ls = m_re.findall(cm)
ls = ['magnet:?xt=urn:btih:' + i for i in ls]
return ls
for path in froms:
if path[0] == '~':
path = os.path.expanduser(path)
else:
path = os.path.abspath(path)
if os.path.isfile(path):
cm = open(path).read()
ls = get_magnet(cm)
ml += ls
elif os.path.isdir(path):
for a, b, c in os.walk(path):
for i in c:
p = os.path.join(a, i)
cm = open(p).read()
ls = get_magnet(cm)
ml += ls
else:
print s % (1, 91, ' !! path is wrong:'), path
t = set(ml)
return list(t)
def main(argv):
######################################################
# for argparse
p = argparse.ArgumentParser(
description='magnet torrent 互转,数字命名bt内容文件名' \
' 用法见 https://github.com/PeterDing/iScript')
p.add_argument('xxx', type=str, nargs='*',
help='命令对象.')
p.add_argument('-i', '--import_from', type=str, nargs='*',
help='import magnet from local.')
p.add_argument('-p', '--proxy', action='store',
default='socks5://127.0.0.1:8883',
type=str, help='proxy for torrage.com, \
eg: -p "sooks5://127.0.0.1:8883"')
p.add_argument('-d', '--directory', action='store', default=None,
type=str, help='torrents保存的路径, eg: -d /path/to/save')
p.add_argument('-n', '--name', action='store', default=None,
type=str, help='顶级文件夹名称, eg: -n thistopdirectory')
p.add_argument('-t', '--type_', action='store',
default='n', type=str,
help='类型参数,eg: ')
global args
args = p.parse_args(argv[2:])
comd = argv[1]
xxx = args.xxx
dir_ = os.getcwd() if not args.directory \
else args.directory
if not os.path.exists(dir_):
os.mkdir(dir_)
if comd == 'm' or comd == 'mt': # magnet to torrent
urls = xxx if not args.import_from \
else import_magnet(args.import_from)
x = bt()
x.magnet2torrent(urls, dir_)
elif comd == 't' or comd == 'tm': # torrent ot magnet
paths = xxx
x = bt()
x.torrent2magnet(paths)
elif comd == 'c' or comd == 'ct': # change
ups = xxx if not args.import_from \
else import_magnet(args.import_from)
x = bt()
x.change(ups, dir_, foo=None, bar=None)
elif comd == 'cr' or comd == 'ctre': # change
foo = xxx[0]
bar = xxx[1]
ups = xxx[2:] if not args.import_from \
else import_magnet(args.import_from)
x = bt()
x.change(ups, dir_, foo=foo, bar=bar)
else:
print s % (2, 91, ' !! 命令错误\n')
if __name__ == '__main__':
argv = sys.argv
main(argv)