发表于 2017-5-5 11:37:49

Python处理MLDonkey 下载中文文件乱码问题 (2)

上次写了一篇关于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 =
  
  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? ')
   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
   if check(subStr) :
   if length == len8 :
   ret += "%" + hex((int)(subStr)) + "%" + hex((int)(subStr))
   if length == len12 :
   ret += "%" + hex((int)(subStr)) + "%" + hex((int)(subStr)) + "%" + hex((int)(subStr))
   location = location + length
   else :
   ret += s
   location = location + 1
   else :
   ret += s
   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 != '_' or s != '_' :
   return False
  
   if len(s) == len12 and s != '_':
   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即可以转换。
页: [1]
查看完整版本: Python处理MLDonkey 下载中文文件乱码问题 (2)