自动更新高清电影文件中文名
#coding:utf-8
import urllib, urllib2, cookielib
import os, time
import re
def login ():
user = 'user'
cookiename = 'chd%s.coockie' % ( user )
logined = False
cj = cookielib .LWPCookieJar ()
#如果存在cookie就直接调用
try:
cj .revert( cookiename)
logined = True
print "OK"
except Exception , e:
print e
opener = urllib2 .build_opener(urllib2.HTTPCookieProcessor (cj))
urllib2.install_opener (opener)
#如果不存在cooike就重新登录并保存下来
if not logined :
print "need logon"
login_url = ' http://chdbits.org/takelogin.php'
login_data = urllib.urlencode({'username' :'user', 'password':'pass' })
headers = { 'Referer' : ' http://chdbits.org/login.php ',
'Accept-Language' : 'zh-cn' ,
'User-Agent': 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)',
'Content-Type': 'application/x-www-form-urlencoded' ,
'Host': 'chdbits.org',
'Connection': 'Keep-Alive',
'Cache-Control': 'no-cache'}
req = urllib2.Request(login_url , login_data, headers=headers ,)
r = opener.open(req )
cj .save( cookiename)
#根据关键字搜索中文名
def chd_search (keyword):
keyword = keyword .replace('REPACK','' )
keyword = keyword .replace('RERiP','' )
#提取名字前半部分
if keyword .find( '720p' ) > 0 :
kk = keyword[:keyword.find ('720p')]
elif keyword .find( '1080p' ) > 0 :
kk = keyword[:keyword.find ('1080p')]
else:
kk = keyword
url = 'http://chdbits.org/torrents.php?incldead=1&spstate=0&inclbookmarked=0&search=%s&search_area=0&search_mode=0 ' % kk
content = urllib2 .urlopen(url).read ()
pattern = re .compile(r'<br/>(.*?)</td><td width="60"' )
name_tx = pattern .search( content)
if name_tx :
sname = name_tx.group(1 )
stitle = sname. split('/' )[0]. split ('[' )[0 ]. split ('*')[ 0 ]
else:
stitle = None
return stitle
#判断原文件名是否包含中文信息字符
def is_chinese (uchar):
hz_yes = False
for i in uchar:
if i >= u'\u4e00' and i<=u'\u9fa5' :
hz_yes = True
break
else:
continue
return hz_yes
login()
d = u'Z:\PT'
#更换当前目录查找目录文件名
os.chdir (d)
chd_names = [i for i in os .listdir(d) if not is_chinese(i )]
print 'have %s names are not Chinese' % len(chd_names )
for i in chd_names:
new_name = chd_search (i)
if new_name :
new = new_name.decode('utf8' , 'ignore' ) + ' ' + i
new = new. replace(':' ,'')
print i, '-------->',new
#改名字
os .rename( i, new)
附件列表