Python 解讀 Torrent 的種子名稱


由於不想每次打開軟件查看真實名稱,所以就只好用 Python 來處理
不過可能是由於運行在 Window Console 上所以處理中文上會亂碼..
暫時還沒有解決方法,如果有高手知道的可否告知一聲?..
難道真的要改動 Cmd 的編碼環境麼..

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
#!/usr/bin/env python
#coding=utf-8

"""
Author: Zeuxis Lo
"""

import sys
import os
import re
import codecs

#sys.stdout = codecs.lookup('utf-8')[-1](sys.stdout)

try:
import psyco # Optional, 2.5x improvement in speed
psyco.full()
except ImportError:
pass

decimal_match = re.compile('\d')

def bdecode(data):
'''Main function to decode bencoded data'''
chunks = list(data)
chunks.reverse()
root = _dechunk(chunks)
return root

def _dechunk(chunks):
item = chunks.pop()

if (item == 'd'):
item = chunks.pop()
hash = {}
while (item != 'e'):
chunks.append(item)
key = _dechunk(chunks)
hash[key] = _dechunk(chunks)
item = chunks.pop()
return hash
elif (item == 'l'):
item = chunks.pop()
list = []
while (item != 'e'):
chunks.append(item)
list.append(_dechunk(chunks))
item = chunks.pop()
return list
elif (item == 'i'):
item = chunks.pop()
num = ''
while (item != 'e'):
num += item
item = chunks.pop()
return int(num)
elif (decimal_match.findall(item)):
num = ''
while decimal_match.findall(item):
num += item
item = chunks.pop()
line = ''
for i in range(1, int(num) + 1):
line += chunks.pop()
return line
raise "Invalid input!"


if len(sys.argv) == 1:
print "Usage: ", sys.argv[0], "file"
sys.exit()
else:
if os.path.exists(sys.argv[1]):
content = open(sys.argv[1], "rb").read()
torrent = bdecode(content)
#print u"%s" % torrent["info"]["name"].decode("utf-8")
print torrent["info"]["name"]
else:
print "File: ", sys.argv[1], "Not Exists"
sys.exit()

參考:
http://wiki.theory.org/BitTorrentSpecification
http://blog.new-studio.org/2009/02/learn-python.html