nivertius-napi/napi
2016-02-20 16:32:10 +01:00

94 lines
2.1 KiB
Python
Executable file

#!/usr/bin/python
# reversed napi 0.16.3.1
#
# by gim,krzynio,dosiu,hash 2oo8.
#
#
#
# last modified: 6-I-2oo8
#
# 4pc0h f0rc3
#
# do dzialania potrzebny jest p7zip-full (tak sie nazywa paczka w debianie)
#
# POZDRAWIAMY NASZYCH FANOW!
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_stream = urllib.urlencode(request_data)
response = urllib.urlopen(base_url, request_stream)
xml = ET.XML(response.read())
content = xml.find("subtitles").find("content").text
except:
print "%s: No subtitles" % (movie_filename,)
sys.exit(2)
with open(input_filename, "w") as input_file:
input_file.write(base64.b64decode(content))
nazwa = 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(nazwa, "w") as output_file:
process = subprocess.Popen(executed, stdout=output_file)
result = process.wait()
if result != 0:
print "%s: 7zip returned non-zero code: %s" % (movie_filename, subtitle_filename)
os.remove(nazwa)
sys.exit(3)
print "%s: Subtitles fetched successfully to %s" % (movie_filename, nazwa)
os.remove(input_filename)