Last active
August 29, 2015 14:23
-
-
Save CatTail/689afb24df0913bed2ad to your computer and use it in GitHub Desktop.
Fetch subtitle from http://splayer.org/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# https://docs.google.com/document/d/1ufdzy6jbornkXxsD-OGl3kgWa4P9WO5NZb6_QYZiGI0/preview | |
import sys | |
import os | |
import md5 | |
import requests | |
# calculate file hash | |
CHUNK_SIZE = 4 * 1024 | |
hash = [] | |
videoname = sys.argv[1] | |
language = 'Chn' if len(sys.argv) < 3 else sys.argv[2] | |
info = os.stat(videoname) | |
videosize = info.st_size | |
def calculate(content): | |
m = md5.new() | |
m.update(content) | |
return m.hexdigest() | |
f = open(videoname) | |
f.seek(4 * 1024, 0) | |
hash.append(f.read(CHUNK_SIZE)) | |
f.seek(videosize / 3 * 2, 0) | |
hash.append(f.read(CHUNK_SIZE)) | |
f.seek(videosize / 3, 0) | |
hash.append(f.read(CHUNK_SIZE)) | |
f.seek(videosize - 8 * 1024, 0) | |
hash.append(f.read(CHUNK_SIZE)) | |
hash = map(calculate, hash) | |
hash = ';'.join(hash) | |
f.close() | |
# fetch subtitles | |
payload = {'filehash': hash, 'pathinfo': videoname, 'format': 'json', 'lang': language} | |
res = requests.post('https://www.shooter.cn/api/subapi.php', data=payload) | |
index = 0 | |
for subinfo in res.json(): | |
for fileinfo in subinfo['Files']: | |
res = requests.get(fileinfo['Link']) | |
subname = res.headers['content-disposition'][len('attachment; filename='):] | |
name, ext = os.path.splitext(subname) | |
subname = name + language + ext | |
subname = "%s.%s%s%s" % (name, language, index, ext) | |
print subname | |
print fileinfo['Link'] | |
with open(subname, 'wb') as fd: | |
for chunk in res.iter_content(CHUNK_SIZE): | |
fd.write(chunk) | |
index = index + 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment