6
6
import sys
7
7
import re
8
8
from hashlib import sha1
9
- import binascii
10
9
import base64
11
10
import requests
12
11
import urlparse
15
14
############################################################
16
15
headers = {
17
16
"Connection" : "keep-alive" ,
18
- "Accept" :"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" ,
17
+ "Accept" :"text/html,application/xhtml+xml,\
18
+ application/xml;q=0.9,image/webp,*/*;q=0.8" ,
19
19
"Accept-Encoding" :"gzip,deflate,sdch" ,
20
- "Accept-Language" :"en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,zh-TW;q=0.2" ,
21
- "User-Agent" :"Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 " \
22
- "(KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36"
20
+ "Accept-Language" :"en-US,en;q=0.8,zh-CN;\
21
+ q=0.6,zh;q=0.4,zh-TW;q=0.2" ,
22
+ "User-Agent" :"Mozilla/5.0 (X11; Linux x86_64) \
23
+ AppleWebKit/537.36 (KHTML, like Gecko) \
24
+ Chrome/40.0.2214.91 Safari/537.36"
23
25
}
24
26
25
27
ss = requests .session ()
26
28
ss .headers .update (headers )
27
29
28
30
s = u'\x1b [%d;%dm%s\x1b [0m' # terminual color template
29
- letters = [i for i in '.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' + '0123456789' ]
31
+ letters = [i for i in '.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' \
32
+ + '0123456789' ]
30
33
31
34
class bt (object ):
32
35
def transfer (self , string , tpath , foo = None , bar = None ):
@@ -42,16 +45,18 @@ def transfer(self, string, tpath, foo=None, bar=None):
42
45
for fl in dstring ['info' ]['files' ]:
43
46
filename = fl ['path' ][- 1 ]
44
47
if args .type_ == 'n' :
45
- newfilename = re .sub (foo , bar , filename , re .I ) if foo and bar else filename
48
+ newfilename = re .sub (foo , bar , filename , re .I ) \
49
+ if foo and bar else filename
46
50
if filename != newfilename :
47
51
print filename , s % (1 , 92 , '==>' ), newfilename
48
- path = [self ._get_sub_dir_index (i ) for i in fl [ 'path' ][: - 1 ]] \
49
- + [newfilename ]
52
+ path = [self ._get_sub_dir_index (i ) \
53
+ for i in fl [ 'path' ][: - 1 ]] + [newfilename ]
50
54
else :
51
55
ext = os .path .splitext (filename )[- 1 ]
52
56
ext = self ._check_ext (ext )
53
- path = [self ._get_sub_dir_index (i ) for i in fl ['path' ][:- 1 ]] \
54
- + ['%s%s' % (file_index , ext )]
57
+ path = [self ._get_sub_dir_index (i ) \
58
+ for i in fl ['path' ][:- 1 ]] \
59
+ + ['%s%s' % (file_index , ext )]
55
60
file_index += 1
56
61
fl ['path' ] = path
57
62
@@ -125,7 +130,8 @@ def do(url, proxies=None, data=None, timeout=None):
125
130
126
131
## with xunlei
127
132
print s % (1 , 94 , ' >> try:' ), 'bt.box.n0808.com'
128
- url = 'http://bt.box.n0808.com/%s/%s/%s.torrent' % (hh [:2 ], hh [- 2 :], hh )
133
+ url = 'http://bt.box.n0808.com/%s/%s/%s.torrent' \
134
+ % (hh [:2 ], hh [- 2 :], hh )
129
135
ss .headers ['Referer' ] = 'http://bt.box.n0808.com'
130
136
result = do (url )
131
137
if result : return result
@@ -207,7 +213,8 @@ def magnet2torrent(self, urls, dir_):
207
213
string = self .get_torrent (hh )
208
214
if string :
209
215
tpath = os .path .join (dir_ , hh + '.torrent' )
210
- print s % (1 , 97 , ' ++ magnet to torrent:' ), 'magnet:?xt=urn:btih:%s' % hh
216
+ print s % (1 , 97 , ' ++ magnet to torrent:' ), \
217
+ 'magnet:?xt=urn:btih:%s' % hh
211
218
with open (tpath , 'w' ) as g :
212
219
g .write (string )
213
220
else :
@@ -237,7 +244,8 @@ def trans(tpath):
237
244
tpath = path
238
245
trans (tpath )
239
246
else :
240
- print s % (1 , 91 , ' !! file doesn\' t existed' ), s % (1 , 93 , '--' ), path
247
+ print s % (1 , 91 , ' !! file doesn\' t existed' ), \
248
+ s % (1 , 93 , '--' ), path
241
249
242
250
def change (self , ups , dir_ , foo = None , bar = None ):
243
251
for up in ups :
@@ -251,7 +259,8 @@ def change(self, ups, dir_, foo=None, bar=None):
251
259
string = self .get_torrent (hh )
252
260
if string :
253
261
tpath = os .path .join (dir_ , hh + '.torrent' )
254
- print s % (1 , 97 , ' ++ transfer:' ), 'magnet:?xt=urn:btih:%s' % hh
262
+ print s % (1 , 97 , ' ++ transfer:' ), \
263
+ 'magnet:?xt=urn:btih:%s' % hh
255
264
self .transfer (string , tpath , foo = foo , bar = bar )
256
265
else :
257
266
print s % (1 , 91 , ' !! Can\' t get torrent from web.' ), path
@@ -266,7 +275,8 @@ def do():
266
275
print s % (1 , 97 , ' ++ transfer:' ), ipath
267
276
string = open (ipath ).read ()
268
277
tpath = os .path .join (dir_ , 'change_' + i )
269
- self .transfer (string , tpath , foo = foo , bar = bar )
278
+ self .transfer (string , tpath , foo = foo ,
279
+ bar = bar )
270
280
paths .update (ipath )
271
281
if os .getcwd () == os .path .abspath (dir_ ):
272
282
do ()
@@ -277,10 +287,12 @@ def do():
277
287
if path .lower ().endswith ('torrent' ):
278
288
print s % (1 , 97 , ' ++ transfer:' ), path
279
289
string = open (path ).read ()
280
- tpath = os .path .join (dir_ , 'change_' + os .path .basename (path ))
290
+ tpath = os .path .join (dir_ ,
291
+ 'change_' + os .path .basename (path ))
281
292
self .transfer (string , tpath , foo = foo , bar = bar )
282
293
else :
283
- print s % (1 , 91 , ' !! file doesn\' t existed' ), s % (1 , 93 , '--' ), path
294
+ print s % (1 , 91 , ' !! file doesn\' t existed' ), \
295
+ s % (1 , 93 , '--' ), path
284
296
285
297
def import_magnet (froms ):
286
298
ml = []
@@ -317,7 +329,8 @@ def get_magnet(cm):
317
329
def main (argv ):
318
330
######################################################
319
331
# for argparse
320
- p = argparse .ArgumentParser (description = 'magnet torrent 互转,数字命名bt内容文件名' \
332
+ p = argparse .ArgumentParser (
333
+ description = 'magnet torrent 互转,数字命名bt内容文件名' \
321
334
' 用法见 https://github.com/PeterDing/iScript' )
322
335
p .add_argument ('xxx' , type = str , nargs = '*' , \
323
336
help = '命令对象.' )
@@ -337,11 +350,13 @@ def main(argv):
337
350
comd = argv [1 ]
338
351
xxx = args .xxx
339
352
340
- dir_ = os .getcwd () if not args .directory else args .directory
353
+ dir_ = os .getcwd () if not args .directory \
354
+ else args .directory
341
355
if not os .path .exists (dir_ ):
342
356
os .mkdir (dir_ )
343
357
if comd == 'm' or comd == 'mt' : # magnet to torrent
344
- urls = xxx if not args .import_from else import_magnet (args .import_from )
358
+ urls = xxx if not args .import_from \
359
+ else import_magnet (args .import_from )
345
360
x = bt ()
346
361
x .magnet2torrent (urls , dir_ )
347
362
@@ -351,14 +366,16 @@ def main(argv):
351
366
x .torrent2magnet (paths )
352
367
353
368
elif comd == 'c' or comd == 'ct' : # change
354
- ups = xxx if not args .import_from else import_magnet (args .import_from )
369
+ ups = xxx if not args .import_from \
370
+ else import_magnet (args .import_from )
355
371
x = bt ()
356
372
x .change (ups , dir_ , foo = None , bar = None )
357
373
358
374
elif comd == 'cr' or comd == 'ctre' : # change
359
375
foo = xxx [0 ]
360
376
bar = xxx [1 ]
361
- ups = xxx [2 :] if not args .import_from else import_magnet (args .import_from )
377
+ ups = xxx [2 :] if not args .import_from \
378
+ else import_magnet (args .import_from )
362
379
x = bt ()
363
380
x .change (ups , dir_ , foo = foo , bar = bar )
364
381
0 commit comments