Skip to content

Instantly share code, notes, and snippets.

@lmtani
Created February 16, 2020 19:52
Show Gist options
  • Save lmtani/46fff5cdab2b12a49f99a90a898bf3bd to your computer and use it in GitHub Desktop.
Save lmtani/46fff5cdab2b12a49f99a90a898bf3bd to your computer and use it in GitHub Desktop.
API client para legendas
import requests
import os
import hashlib
# url = "http://sandbox.thesubdb.com"
url = "http://api.thesubdb.com"
user_agent = "SubDB/1.0 (Pyrrot/0.1; http://github.com/jrhames/pyrrot-cli)"
def hash_thesubdb(video_path):
"""Compute a hash using TheSubDB's algorithm.
:param str video_path: path of the video.
:return: the hash.
:rtype: str
"""
readsize = 64 * 1024
if os.path.getsize(video_path) < readsize:
return
with open(video_path, 'rb') as f:
data = f.read(readsize)
f.seek(-readsize, os.SEEK_END)
data += f.read(readsize)
return hashlib.md5(data).hexdigest()
if __name__ == "__main__":
import sys
file_path = sys.argv[1]
language = sys.argv[2]
out_name = sys.argv[3]
file_hash = hash_thesubdb(file_path)
# resp = requests.get(f"{url}/?action=languages", headers={"User-Agent": user_agent, 'Content-Type': 'text/html'})
resp = requests.get(f"{url}/?action=download&hash={file_hash}&language={language}", headers={"User-Agent": user_agent})
if resp.status_code != 200:
raise Exception("Legenda nao encontrada.")
with open(f"{out_name}.srt", "w+", encoding=resp.encoding) as out:
out.write(resp.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment