#-*-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 () |