上次写了一篇关于mldonkey转码的东西,最近作了一次修改,不过仍然需要Python2.4版本的支持,代码如下:
mldonkey.py
# -*- coding: cp936 -*-
############################################################
# convert mldonkey's chinese
# Version: 0.1
# Author: kongxx
# Email: kongxxcn@yahoo.com.cn
############################################################
import os ,sys ,string ,urllib ,telnetlib ,operator
# define the mldonkey incoming dir
sdir='/usr/local/mldonkey/incoming/'
#flag the num
sNum='0123456789'
#length
len8 = 8
len12 = 12
lengths = [len8 ,len12]
def convert():
filenames=os.listdir(sdir)
for filename in filenames :
for length in lengths :
s1 = filename
s2 = convertName(filename ,length)
try :
if s1 != s2 :
print '############################################################'
print 'The source name is:' + s1
print 'The target name is:' + s2
print ''
confirm = raw_input('Do you confirm rename the file[yes or no]? ')
if confirm == 'yes' :
os.rename(sdir + s1 ,sdir + s2)
break
except UnicodeDecodeError:
print ''
# convert ths file name
# param s the filename
# param length
def convertName(s ,length) :
location = 0
ret = ''
if length not in lengths:
return s
while True :
if location + length <= len(s) :
subStr = s[location:location + length]
if check(subStr) :
if length == len8 :
ret += "%" + hex((int)(subStr[1:4]))[2:4] + "%" + hex((int)(subStr[5:8]))[2:4]
if length == len12 :
ret += "%" + hex((int)(subStr[1:4]))[2:4] + "%" + hex((int)(subStr[5:8]))[2:4] + "%" + hex((int)(subStr[9:12]))[2:4]
location = location + length
else :
ret += s[location :location + 1]
location = location + 1
else :
ret += s[location:]
break
ret = urllib.unquote(ret)
if ret == s : return s
try :
if length == len8 :
return ret.decode('GBK')
if length == len12 :
return ret.decode('UTF-8')
except UnicodeDecodeError:
return ret
def check(s):
if len(s) != len8 and len(s) != len12:
return False
if s[0] != '_' or s[4] != '_' :
return False
if len(s) == len12 and s[8] != '_':
return False
s = s.replace('_','')
for c in s :
if (c not in sNum) :return False
return True
convert()
在命令行输入python mldonkey.py即可以转换。
在命令行输入python mldonkey.py即可以转换。