Python處理MLDonkey 下載中文檔案亂碼問題 (2)

來源:互聯網
上載者:User
上次寫了一篇關於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即可以轉換。 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.