Created
April 18, 2010 16:37
-
-
Save pagenoare/370347 to your computer and use it in GitHub Desktop.
Fork of napi.py
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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Based on http://napi.py.googlepages.com/napi.py | |
import sys | |
import os | |
import hashlib | |
import sys | |
import urllib | |
from optparse import OptionParser | |
__version__ = '2.1' | |
class Napi(object): | |
def __init__(self, fname, lang, force_replace=True): | |
self.fname = fname | |
self.lang = lang | |
self.force_replace = force_replace | |
if not os.path.exists(self.fname): | |
self.notify("File not found. ") | |
sys.exit(1) | |
o = open(fname) | |
self.d = hashlib.md5() | |
self.d.update(o.read(10485760)) | |
self.d = self.d.hexdigest() | |
o.close() | |
def search(self): | |
params = urllib.urlencode({ | |
'l' : self.lang, | |
'f' : self.d, | |
't' : self.f(), | |
'v' : 'other', | |
'kolejka' : 'false', | |
'nick' : '', | |
'pass' : '', | |
'napios' : os.name, | |
}) | |
n = os.path.splitext(self.fname)[0] + ".txt" | |
t = os.path.splitext(self.fname)[0] + '.7z' | |
self.notify('Downloading... (%s)' % t) | |
if (self.force_replace == False) and os.path.exists(n): | |
self.notify("Subtitles has been found!") | |
return True | |
open(t, "w").close() | |
urllib.urlretrieve("http://napiprojekt.pl/unit_napisy/dl.php?%s" % params, t) | |
self.notify('Unpacking... (%s)' % n) | |
if os.system("/usr/bin/7z x -y -so -piBlm8NTigvru0Jr0 \"%s\" 2> /dev/null > \"%s\"" % (t, n)): | |
try: | |
os.remove(n) | |
except OSError: | |
pass | |
self.notify("Subtitles not found.") | |
return False | |
else: | |
self.notify("Subtitles has been found!") | |
return True | |
try: | |
os.remove(t) | |
except: | |
pass | |
def f(self): | |
idx = [ 0xe, 0x3, 0x6, 0x8, 0x2 ] | |
mul = [ 2, 2, 5, 4, 3 ] | |
add = [ 0, 0xd, 0x10, 0xb, 0x5 ] | |
b = [] | |
for i in xrange(len(idx)): | |
a = add[i] | |
m = mul[i] | |
i = idx[i] | |
t = a + int(self.d[i], 16) | |
v = int(self.d[t:t+2], 16) | |
b.append( ("%x" % (v*m))[-1] ) | |
return ''.join(b) | |
def notify(self, content): | |
print '[NAPI] %s [LANG: %s]' % (content, self.lang) | |
def main(): | |
usage = "usage: %prog [options] <file>" | |
parser = OptionParser(usage=usage, version="%prog 2.0") | |
parser.add_option( | |
"-l", "--language", default="PL", dest="lang", | |
help="subtitles language [e.g. PL, ENG] [default: %default]" | |
) | |
(options, args) = parser.parse_args() | |
if len(args) != 1: | |
parser.error("incorrect number of arguments") | |
napi = Napi(args[0], options.lang) | |
napi.search() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment