]> git.pld-linux.org Git - packages/pynapi.git/commitdiff
- up to 0.20; crude way of accessing napisy 24 API and pynapi would welcome rewrite
authorArkadiusz Miśkiewicz <arekm@maven.pl>
Sun, 15 Feb 2015 22:17:42 +0000 (23:17 +0100)
committerArkadiusz Miśkiewicz <arekm@maven.pl>
Sun, 15 Feb 2015 22:17:42 +0000 (23:17 +0100)
pynapi.py
pynapi.spec

index 7ca5676396fae5f66531e1215c326f2a4d598946..a6a72b631db672dafbc6d2aab315cbec98b859bb 100644 (file)
--- a/pynapi.py
+++ b/pynapi.py
 #
 # napiprojekt.pl API is used with napiproject administration consent
 # (given by Marek <kontakt@napiprojekt.pl> at Wed, 24 Feb 2010 14:43:00 +0100)
+#
+# napisy24.pl API access granted by napisy24 admins at 15 Feb 2015
+#
 
+import StringIO
 import re
 import sys
 import mimetypes
+import urllib
 import urllib2
 import time
 import os
 import getopt
 import socket
+import struct
+import zipfile
 
 try:
     from hashlib import md5 as md5
@@ -38,7 +45,15 @@ prog = os.path.basename(sys.argv[0])
 video_files = [ 'asf', 'avi', 'divx', 'm2ts', 'mkv', 'mp4', 'mpeg', 'mpg', 'ogm', 'rm', 'rmvb', 'wmv' ]
 languages = { 'pl': 'PL', 'en': 'ENG' }
 
-def f(z):
+def calculate_digest(filename):
+    d = md5()
+    try:
+        d.update(open(filename, "rb").read(10485760))
+    except (IOError, OSError), e:
+        raise Exception('Hashing video file failed: %s' % ( e ))
+    return d.hexdigest()
+
+def napiprojekt_hash(z):
     idx = [ 0xe, 0x3,  0x6, 0x8, 0x2 ]
     mul = [   2,   2,    5,   4,   3 ]
     add = [   0, 0xd, 0x10, 0xb, 0x5 ]
@@ -55,9 +70,43 @@ def f(z):
 
     return ''.join(b)
 
+def napisy24_hash(filename): 
+    try: 
+        longlongformat = '<q'  # little-endian long long
+        bytesize = struct.calcsize(longlongformat)
+
+        f = open(filename, "rb")
+
+        filesize = os.path.getsize(filename)
+        hash = filesize
+
+        if filesize < 65536 * 2:
+            raise Exception('Hashing (napisy24) video file failed: `%s\': File too small' % ( filename ))
+
+        for x in range(65536/bytesize): 
+            buffer = f.read(bytesize) 
+            (l_value,)= struct.unpack(longlongformat, buffer)  
+            hash += l_value 
+            hash = hash & 0xFFFFFFFFFFFFFFFF #to remain as 64bit number  
+
+
+        f.seek(max(0,filesize-65536),0) 
+        for x in range(65536/bytesize): 
+            buffer = f.read(bytesize) 
+            (l_value,)= struct.unpack(longlongformat, buffer)  
+            hash += l_value 
+            hash = hash & 0xFFFFFFFFFFFFFFFF 
+
+        f.close() 
+        returnedhash =  "%016x" % hash 
+        return returnedhash 
+
+    except IOError, e:
+        raise Exception('Hashing (napisy24) video file failed: %s' % ( e ))
+
 def usage():
     print >> sys.stderr, "Usage: %s [OPTIONS]... [FILE|DIR]..." % prog
-    print >> sys.stderr, "Find video files and download matching subtitles from napiprojekt server."
+    print >> sys.stderr, "Find video files and download matching subtitles from napiprojekt/napisy24 server."
     print >> sys.stderr
     print >> sys.stderr, "Supported options:"
     print >> sys.stderr, "     -h, --help            display this help and exit"
@@ -67,8 +116,6 @@ def usage():
     print >> sys.stderr, "     -u, --update          fetch new and also update existing subtitles"
     print >> sys.stderr, "     -d, --dest=DIR        destination directory"
     print >> sys.stderr
-    print >> sys.stderr, "pynapi $Revision$"
-    print >> sys.stderr
     print >> sys.stderr, "Report bugs to <arekm@pld-linux.org>."
 
 def get_desc_links(digest, file=None):
@@ -112,21 +159,73 @@ def get_cover(digest):
         return False
     return (cover, extension)
 
-def calculate_digest(file):
-    d = md5()
-    try:
-        d.update(open(file, "rb").read(10485760))
-    except (IOError, OSError), e:
-        raise Exception('Hashing video file failed: %s' % ( e ))
-    return d.hexdigest()
+def get_subtitle_napisy24(filename, digest=False, lang="pl"):
+    url = "http://napisy24.pl/run/CheckSubAgent.php"
+
+    pdata = []
+    pdata.append(('postAction', 'CheckSub'))
+    pdata.append(('ua', 'pynapi'))
+    pdata.append(('ap', 'XaA!29OkF5Pe'))
+    pdata.append(('nl', lang))
+    pdata.append(('fn', filename))
+    pdata.append(('fh', napisy24_hash(filename)))
+    pdata.append(('fs', os.path.getsize(filename)))
+    if digest:
+        pdata.append(('md5', digest))
 
-def get_subtitle(digest, lang="PL"):
+    repeat = 3
+    error = "Fetching subtitle (napisy24) failed:"
+    while repeat > 0:
+        repeat = repeat - 1
+        try:
+            sub = urllib2.urlopen(url, data=urllib.urlencode(pdata))
+            if hasattr(sub, 'getcode'):
+                http_code = sub.getcode() 
+            sub = sub.read()
+        except (IOError, OSError), e:
+            error = error + " %s" % (e)
+            time.sleep(0.5)
+            continue
+
+        if http_code != 200:
+            error = error + ",HTTP code: %s" % (str(http_code))
+            time.sleep(0.5)
+            continue
+
+        err_add = ''
+        if sub.startswith('OK-2|'):
+            pos = sub.find('||')
+            if pos >= 2 and len(sub) > (pos + 2):
+                sub = sub[pos+2:]
+
+                try:  
+                    subzip=zipfile.ZipFile(StringIO.StringIO(sub))
+                    sub=''
+                    for name in subzip.namelist():
+                        sub += subzip.read(name)
+                except Exception, e:
+                    raise Exception('Subtitle NOT FOUND%s' % e)
+            else:
+                raise Exception('Subtitle NOT FOUND (subtitle too short)')
+        elif sub.startswith('OK-'):
+            raise Exception('Subtitle NOT FOUND')
+        else:
+            raise Exception('Subtitle NOT FOUND (unknown error)')
+
+        repeat = 0
+
+    if sub is None or sub == "":
+        raise Exception(error)
+
+    return sub
+
+def get_subtitle_napiprojekt(digest, lang="PL"):
     url = "http://napiprojekt.pl/unit_napisy/dl.php?l=%s&f=%s&t=%s&v=pynapi&kolejka=false&nick=&pass=&napios=%s" % \
-        (lang, digest, f(digest), os.name)
+        (lang, digest, napiprojekt_hash(digest), os.name)
     repeat = 3
     sub = None
     http_code = 200
-    error = "Fetching subtitle failed:"
+    error = "Fetching subtitle (napiprojekt) failed:"
     while repeat > 0:
         repeat = repeat - 1
         try:
@@ -248,11 +347,20 @@ def main(argv=sys.argv):
 
         try:
             digest = calculate_digest(file)
-            sub = get_subtitle(digest, languages[lang])
         except:
             print >> sys.stderr, "%s: %d/%d: %s" % (prog, i, i_total, sys.exc_info()[1])
             continue
 
+        try:
+            raise
+            sub = get_subtitle_napiprojekt(digest, languages[lang])
+        except:
+            try:
+                sub = get_subtitle_napisy24(file, digest, lang)
+            except:
+                print >> sys.stderr, "%s: %d/%d: %s" % (prog, i, i_total, sys.exc_info()[1])
+                continue
+
         fp = open(vfile, 'wb')
         fp.write(sub)
         fp.close()
index 3579fef63b5e2b8b9946728999e20a9aa9e76879..8a1e5d6c2208cad9822433a02bfb06b9d7cd5235 100644 (file)
@@ -1,8 +1,8 @@
 Summary:       pynapi - Movie Subtitle Downloader
 Summary(pl.UTF-8):     pynapi - narzędzie do ściągania napisów do filmów
 Name:          pynapi
-Version:       0.18
-Release:       7
+Version:       0.20
+Release:       1
 License:       GPL v3+
 Group:         Applications/Multimedia
 Source0:       pynapi.py
@@ -13,11 +13,11 @@ BuildArch:  noarch
 BuildRoot:     %{tmpdir}/%{name}-%{version}-root-%(id -u -n)
 
 %description
-Command line NAPI-PROJEKT subtitles downloader.
+Command line NAPI-PROJEKT and NAPISY24 subtitles downloader.
 
 %description -l pl.UTF-8
 Obsługiwane z linii poleceń narzędzie do ściągania napisów z projektu
-NAPI.
+NAPI oraz NAPISY24.
 
 %prep
 %setup -q -c -T
This page took 0.130836 seconds and 4 git commands to generate.