下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
#!/usr/bin/python# -*- Coding: utf-8 -*-import osimport reimport shutilimport httplibimport tracebackimport threadingTitle_re = re.compile('<Title>(.+?)</Title>',re.DOTALL)album_re = re.compile('<td valign="top">所属专辑:</td>.*?title="(?P<album>.+?)">',re.DOTALL)max_threads = 20cache_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_retdef 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: passdef 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)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的虾米XMusicCache歌曲批量命名全部内容,希望文章能够帮你解决虾米XMusicCache歌曲批量命名所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)