#-*-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 = '000000' # 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 t 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 []) [] + "%" + hex (int) (subStr []) [2: 4] If length = len12: Ret + = "%" + hex (int) (subStr []) [] + "%" + hex (int) (subStr []) [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 t 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 () |