mirror of
https://source.perfectable.org/nivertius/napi.git
synced 2025-07-01 14:03:57 +02:00
96 lines
2.9 KiB
Python
Executable file
96 lines
2.9 KiB
Python
Executable file
#!/usr/bin/python3
|
|
# -*- coding: utf-8 -*-
|
|
# reversed napi 0.16.3.1
|
|
# by gim,krzynio,dosiu,hash 2oo8.
|
|
# cleaned by nivertius
|
|
|
|
import hashlib
|
|
import sys
|
|
import urllib
|
|
from urllib.parse import urlencode
|
|
from urllib.request import urlopen
|
|
import os
|
|
import argparse
|
|
import base64
|
|
import subprocess
|
|
|
|
from xml.etree.ElementTree import XML
|
|
|
|
base_url = "http://napiprojekt.pl/api/api-napiprojekt3.php"
|
|
password = "iBlm8NTigvru0Jr0"
|
|
input_filename = '/dev/shm/napi-temp-file'
|
|
default_language = "PL"
|
|
|
|
parser = argparse.ArgumentParser(description='Searches subtitles for movies from napiproject')
|
|
parser.add_argument('-l', '--language', dest='language', default=default_language,
|
|
help='requested subtitle language')
|
|
parser.add_argument('-f', '--filename', dest='filename',
|
|
help='forced filename')
|
|
parser.add_argument('movies', metavar='movie', nargs='+',
|
|
help='movie for which to find subtitles')
|
|
|
|
args = parser.parse_args()
|
|
|
|
for movie_filename in args.movies:
|
|
|
|
digest = hashlib.md5()
|
|
try:
|
|
with open(movie_filename, "rb") 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" : args.language
|
|
}
|
|
|
|
request_data = urlencode(request_data).encode()
|
|
response = urlopen(base_url, request_data)
|
|
response_data = response.read()
|
|
response_xml = XML(response_data)
|
|
|
|
response_status = response_xml.find('status')
|
|
if response_status is None or response_status.text != 'success':
|
|
print("%s: No subtitles found for language %s" % (movie_filename, args.language))
|
|
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, "wb") as input_file:
|
|
input_file.write(decoded_content)
|
|
subtitle_filename = args.filename or 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(os.devnull, 'w') as error_file:
|
|
with open(subtitle_filename, "w") as subtitle_file:
|
|
process = subprocess.Popen(executed, stdout=subtitle_file, stderr=error_file)
|
|
result = process.wait()
|
|
|
|
if result != 0:
|
|
print("%s: 7zip returned non-zero code: %s" % (movie_filename, subtitle_filename))
|
|
os.remove(subtitle_filename)
|
|
continue
|
|
|
|
print("%s: downloaded subtitles: language '%s', uploader '%s', author '%s', date '%s'" % (movie_filename,
|
|
args.language, subtitle_uploader, subtitle_author, subtitle_date))
|
|
os.remove(input_filename)
|