Created
March 19, 2018 19:34
-
-
Save spajak/35266ab48296a5360554a2823c6454bf to your computer and use it in GitHub Desktop.
Napiprojekt API. Pobieranie napisów do filmu
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
import sys | |
import hashlib | |
import base64 | |
import os.path | |
import urllib.request, urllib.parse | |
import xml.etree.ElementTree as ET | |
URL='http://www.napiprojekt.pl/api/api-napiprojekt3.php' | |
def hash(fileName): | |
h = hashlib.md5() | |
c = 2**10 | |
n = 0 | |
with open(fileName, 'rb') as f: | |
while n < 2**10 * 10: | |
data = f.read(c) | |
if not data: | |
break | |
h.update(data) | |
n += 1 | |
f.close() | |
return h.hexdigest() | |
def get(hash, language='PL'): | |
params = { | |
'downloaded_subtitles_lang': language, | |
'downloaded_subtitles_txt': '1', | |
'downloaded_subtitles_id': hash, | |
'client': 'NapiProjekt', | |
'mode': '1' | |
} | |
params = urllib.parse.urlencode(params).encode('ascii') | |
request = urllib.request.Request(url=URL, data=params, method='POST') | |
with urllib.request.urlopen(request) as f: | |
root = ET.fromstring(f.read().decode('utf8')) | |
text = root.find('subtitles').find('content').text | |
return base64.b64decode(text).decode('utf8') | |
if __name__ == "__main__": | |
if len(sys.argv) < 2: | |
print("Please provide file name") | |
exit(1) | |
fileName = sys.argv[1] | |
if not os.path.isfile(fileName): | |
print("File not found") | |
exit(1) | |
outputFileName = os.path.splitext(fileName)[0] + ".txt" | |
with open(outputFileName, 'w', encoding='utf8') as f: | |
f.write(get(hash(fileName))) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment