mirror of
https://source.perfectable.org/nivertius/napi.git
synced 2025-07-02 03:45:05 +02:00
86 lines
2.1 KiB
Python
Executable file
86 lines
2.1 KiB
Python
Executable file
#!/usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
# reversed napi 0.16.3.1
|
|
# by gim,krzynio,dosiu,hash 2oo8.
|
|
# cleaned by nivertius
|
|
|
|
import hashlib
|
|
import sys
|
|
import urllib
|
|
import os
|
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
import base64
|
|
import subprocess
|
|
|
|
base_url = "http://napiprojekt.pl/api/api-napiprojekt3.php"
|
|
password = "iBlm8NTigvru0Jr0"
|
|
input_filename = '/tmp/napi-temp-file'
|
|
|
|
from getopt import getopt
|
|
|
|
opts, args = getopt(sys.argv[1:], '', ['language='])
|
|
|
|
language = "PL"
|
|
|
|
for optname, optvalue in opts:
|
|
if optname == '--language':
|
|
language = optvalue
|
|
|
|
if len(args) < 1:
|
|
print "usage: %s <movie>" % (sys.argv[0],)
|
|
sys.exit(0)
|
|
|
|
for movie_filename in args:
|
|
|
|
digest = hashlib.md5()
|
|
try:
|
|
with open(movie_filename, "r") as movie_file:
|
|
digest.update(movie_file.read(10 * 1024 * 1024))
|
|
except IOError:
|
|
print "%s: No such file" % movie_filename
|
|
continue
|
|
|
|
movie_digest = digest.hexdigest()
|
|
|
|
request_data = {
|
|
"downloaded_subtitles_id" : movie_digest,
|
|
"mode" : "31",
|
|
"client" : "NapiProjekt",
|
|
"downloaded_subtitles_lang" : language
|
|
}
|
|
try:
|
|
request_data = urllib.urlencode(request_data)
|
|
response = urllib.urlopen(base_url, request_data)
|
|
response_xml = ET.XML(response.read())
|
|
content = response_xml.find("subtitles").find("content").text
|
|
except:
|
|
print "%s: No subtitles" % (movie_filename,)
|
|
sys.exit(2)
|
|
decoded_content = base64.b64decode(content)
|
|
with open(input_filename, "w") as input_file:
|
|
input_file.write(decoded_content)
|
|
subtitle_filename = movie_filename[:-4] + ".srt"
|
|
|
|
executed = [
|
|
'/usr/bin/7z', # executable
|
|
'x', # command: extract
|
|
'-p' + password, # use password
|
|
'-y', # assume yes on all queries
|
|
'-so', # into standard out
|
|
input_filename, # input file
|
|
]
|
|
|
|
with open(subtitle_filename, "w") as subtitle_file:
|
|
process = subprocess.Popen(executed, stdout=subtitle_file)
|
|
result = process.wait()
|
|
|
|
if result != 0:
|
|
print "%s: 7zip returned non-zero code: %s" % (movie_filename, subtitle_filename)
|
|
os.remove(subtitle_filename)
|
|
sys.exit(3)
|
|
|
|
print "%s: Subtitles fetched successfully to %s" % (movie_filename, subtitle_filename)
|
|
os.remove(input_filename)
|
|
|