Python processing mldonkey Download Chinese files garbled problem (2)

Source: Internet
Author: User
Last time I wrote an article about MLDonkey transcoding, recently made a modification, but still need the support of the Python2.4 version, the code is as follows:

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

You can convert the Python mldonkey.py at the command line. You can convert the Python mldonkey.py at the command line.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.