虾米XMusicCache歌曲批量命名
发布时间:2020-05-25 00:23:42 所属栏目:Python 来源:互联网
导读:虾米XMusicCache歌曲批量命名
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 #!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import re
import shutil
import httplib
import traceback
import threading
title_re = re.compile('<title>(.+?)</title>',re.DOTALL)
album_re = re.compile('<td class="item" valign="top">所属专辑:</td>.*?title="(?P<album>.+?)">',re.DOTALL)
max_threads = 20
cache_filename = 'cache.txt'
cache = {}
def listFiles(rootDir,ext = None):
list_dirs = os.walk(rootDir)
list_ret = []
for root,dirs,files in list_dirs:
for f in files:
if not ext is None:
if not f.endswith(ext):
continue
list_ret.append(os.path.join(root,f))
return list_ret
def filterFiles(lst):
global cache
def _filter(x):
x = os.path.basename(x)
if x.find('.') >= 0:
return False
try:
int(x)
except:
return False
if x in cache:
if os.path.exists(cache[x]):
return False
return True
return filter(_filter,lst)
def readHTTPInfo(uri):
try:
conn = httplib.HTTPConnection("www.xiami.com")
url = '/song/%s' % uri
conn.request("GET",url)
r = conn.getresponse()
if r.status != 200:
print r.status,r.reason
# print r.read()
return '',''
data = r.read()
conn.close()
match = title_re.search(data)
if match:
result = match.group(1)
pos2 = result.find('-')
pos = result.find(',',pos2)
if pos > 0:
result = result[:pos]
pos = result.rfind('-')
if pos > 0:
return result[:pos].strip(),result[pos+1:].strip()
print result.strip()
else:
print 'no title'
return '',''
except Exception:
traceback.print_exc()
return '',''
def utf2gbk(s):
return s.decode('utf8').encode('gbk')
def copy2RenameMp3(filename,idx = 0):
uri = os.path.basename(filename)
name,author = readHTTPInfo(uri)
mp3name = utf2gbk('%s - %s.mp3' % (author,name))
print filename,idx,'->',mp3name
if len(name) > 0 and len(author):
cache[uri] = mp3name
shutil.copy(filename,mp3name)
def readCache():
global cache
try:
fp = open(cache_filename,'rb')
lines = fp.readlines()
for x in lines:
pos = x.find(' ')
if pos >= 0:
cache[x[:pos]] = x[pos + 1:].strip()
fp.close()
except:
pass
def writeCache():
global cache
if len(cache) == 0:
return
fp = open(cache_filename,'wb')
lines = ['%s %s' % (k,v) for k,v in cache.iteritems()]
fp.write('n'.join(lines))
fp.close()
def main():
readCache()
list_files = listFiles('.')
list_files = filterFiles(list_files)
threads = []
for filename in list_files:
copy2RenameMp3(filename)
# th = threading.Thread(target = copy2RenameMp3,args = (filename,len(threads) + 1),name = filename)
# threads.append(th)
if len(threads) > max_threads:
map(lambda th: th.start(),threads)
map(lambda th: th.join(),threads)
threads = []
if threads:
map(lambda th: th.start(),threads)
map(lambda th: th.join(),threads)
writeCache()
if __name__ == '__main__':
main()
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
