玩蛇网提供最新Python编程技术信息以及Python资源下载!

自动更新高清电影文件中文名python代码

自动更新高清电影文件中文名
 
标签: <无>
 

 

1. [代码][Python]代码  

#coding:utf-8

import urllib, urllib2, cookielib
import os, time
import re


def login ():
    user = 'yourname'

    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' :'yourname', 'password':'password' })
        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)

玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/code/c997.html

相关文章 Recommend

玩蛇网Python互助QQ群,欢迎加入-->: 106381465 玩蛇网Python新手群
修订日期:2017年01月10日 - 09时38分34秒 发布自玩蛇网

您现在的位置: 玩蛇网首页 > Python源码实例 > 正文内容
我要分享到:

必知PYTHON教程 Must Know PYTHON Tutorials

必知PYTHON模块 Must Know PYTHON Modules