Advertisement
Guest User

Untitled

a guest
Apr 21st, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.81 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. #
  3. # Script downloads subtitles from napiprojekt
  4. #
  5. # based on older script
  6. # by gim,krzynio,dosiu,hash 2oo8.
  7. # last modified: 6-I-2oo8
  8. # 4pc0h f0rc3
  9.  
  10. import md5, sys, urllib, os
  11.  
  12. from tempfile import NamedTemporaryFile
  13. from subprocess import Popen, PIPE
  14.  
  15.  
  16. def f(z):
  17.     idx = [ 0xe, 0x3,  0x6, 0x8, 0x2 ]
  18.     mul = [   2,   2,    5,   4,   3 ]
  19.     add = [   0, 0xd, 0x10, 0xb, 0x5 ]
  20.  
  21.     b = []
  22.     for i in xrange(len(idx)):
  23.         a = add[i]
  24.         m = mul[i]
  25.         i = idx[i]
  26.  
  27.         t = a + int(z[i], 16)
  28.         v = int(z[t:t+2], 16)
  29.         b.append( ("%x" % (v*m))[-1] )
  30.  
  31.     return "".join(b)
  32.  
  33.  
  34. def napiurl(path):
  35.     d = md5.new();
  36.     d.update(open(path).read(10485760))
  37.     h = d.hexdigest()
  38.  
  39.     return "http://napiprojekt.pl/unit_napisy/dl.php?l=PL&" + \
  40.         "f=" + h + "&t=" + f(h) + \
  41.         "&v=other&kolejka=false&nick=&pass=&napios=" + os.name
  42.  
  43.  
  44. class Un7ZipError(Exception):
  45.     pass
  46.  
  47.  
  48. def un7zip(archive, password=None, tmpfileprefix="un7zip", tmpfilesuffix=".7z"):
  49.     tmpfile = NamedTemporaryFile(prefix=tmpfileprefix, suffix=tmpfilesuffix)
  50.     tmpfile.write(archive)
  51.     tmpfile.flush()
  52.  
  53.     cmd = ["7z", "x", "-y", "-so"]
  54.     if password is not None:
  55.         cmd += ["-p" + password]
  56.     cmd += [tmpfile.name]
  57.  
  58.     sp = Popen(cmd, stdout=PIPE, stderr=PIPE)
  59.        
  60.     content = sp.communicate()[0]
  61.  
  62.     if sp.wait() != 0:
  63.         raise Un7ZipError("Invalid archive")
  64.  
  65.     tmpfile.close() # deletes the file
  66.     return content
  67.  
  68.  
  69. def subtitlepath(moviepath):
  70.     filename, fileext = os.path.splitext(moviepath)
  71.     return filename + ".txt"
  72.  
  73.  
  74. class NoMatchingSubtitle(Exception):
  75.     pass
  76.  
  77.  
  78. def download_subtitle(filename):
  79.     url = napiurl(filename)
  80.     content_7z = urllib.urlopen(url).read()
  81.     try:
  82.         content = un7zip(content_7z, password="iBlm8NTigvru0Jr0")
  83.     except Un7ZipError:
  84.         raise NoMatchingSubtitle("No matching subtitle")
  85.  
  86.     # Don't override the same subtitles
  87.     try:
  88.         same = open(subtitlepath(filename), "r").read() == content
  89.     except IOError:
  90.         same = False
  91.  
  92.     if not same:
  93.         open(subtitlepath(filename), "w").write(content)
  94.  
  95.  
  96. def main():
  97.     if len(sys.argv) < 2:
  98.         sys.stderr.write("\nUSAGE:\n\t" + sys.argv[0] + " moviefile [moviefile, ...]\n\n")
  99.         exit(1)
  100.  
  101.     failed = False
  102.     try:
  103.         for arg in sys.argv[1:]:
  104.             try:
  105.                 download_subtitle(arg)
  106.                 print "OK " + arg
  107.             except NoMatchingSubtitle:
  108.                 failed = True
  109.                 print "NOSUBS " + arg
  110.             except IOError:
  111.                 sys.stderr.write("Cannot read file " + arg + "\n")
  112.                 exit(2)
  113.     except OSError:
  114.         print "OS error. Is 7z in PATH?"
  115.         exit(4)
  116.  
  117.     if failed:
  118.         exit(8)
  119.  
  120.  
  121. if __name__ == "__main__":
  122.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement