def sub_downloader(path): hash_val = get_hash(path) name = path.split('\\')[-1].split('/')[-1] # 為了安全性著想不傳送完整路徑,只給檔名 replace = extensions for content in replace: path = path.replace(content, "") headers = {'User-Agent': 'SubDB/1.0 (shooter-subtitle-downloader/1.0; ' 'http://github.com/marksylee/shooter-subtitle-downloader)'} # step 1. find subtitle list filehash = hash_val[0] + '%3B' + hash_val[1] + '%3B' + hash_val[2] + '%3B' + hash_val[3] url = 'http://www.shooter.cn/api/subapi.php?filehash=' + filehash + '&format=json&pathinfo=' + name + '&lang=Chn' print 'get list url:', url req = urllib2.Request(url, '', headers) done = False response = None while not done: try: response = urllib2.urlopen(req).read() done = True except: print 'shooter api timeout, retry...' done = False # 找不到字幕 if response == '\xff' or response == '0xff(-1)': return None # step 2. get first 5 subtitle from subtitle list for index, res in enumerate(eval(response)): if index < 5: # 經過測試,射手 api 似乎只會回傳三筆字幕檔,但還是以防萬一限制一下上限 subtitle = res['Files'] url = subtitle[0]['Link'].replace('\u0026', '&') print 'download file url:', url req = urllib2.Request(url, '', headers) done = False while not done: try: response = urllib2.urlopen(req).read() done = True except: done = False srt_file_name = path + '-' + str(index) + ".zh.srt" with open(srt_file_name, "wb") as subtitle_file: subtitle_file.write(response) subtitle_file.close() os.chmod(subtitle_file.name, 0777) # 簡轉繁 g2butf8.translate(srt_file_name)
__author__ = 'marklee' import os import glob from g2butf8 import g2butf8 dir_path = 'C:\\Users\\marklee\\Downloads\\test' extensions = ['.ass', '.srt'] for root, subFolders, files in os.walk(dir_path): for ext in extensions: for f in glob.glob(os.path.join(root, '*' + ext)): print 'subtitle file:', f g2butf8.translate(os.path.join(root, f))