nivertius-napi/napi
2016-02-20 16:49:37 +01:00

96 lines
2.4 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
from xml.etree.ElementTree import XML
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
}
request_data = urllib.urlencode(request_data)
response = urllib.urlopen(base_url, request_data)
response_data = response.read()
response_xml = XML(response_data)
if response_xml.find('status').text != 'success':
print "%s: No subtitles found" % (movie_filename,)
continue
subtitle_xml = response_xml.find("subtitles")
subtitle_uploader = subtitle_xml.find('uploader').text
subtitle_author = subtitle_xml.find('author').text
subtitle_date = subtitle_xml.find('upload_date').text
content = subtitle_xml.find("content").text
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 = [
'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: downloaded subtitles: uploader '%s', author '%s', date '%s'" % (movie_filename,
subtitle_uploader, subtitle_author, subtitle_date)
os.remove(input_filename)