Kodi Community Forum
[REQUEST] Download subtitles from napiprojekt.pl website? - Printable Version

+- Kodi Community Forum (http://forum.kodi.tv)
+-- Forum: Help and Support (/forumdisplay.php?fid=33)
+--- Forum: Add-ons Help and Support (/forumdisplay.php?fid=27)
+---- Forum: Subtitle Add-ons (/forumdisplay.php?fid=143)
+---- Thread: [REQUEST] Download subtitles from napiprojekt.pl website? (/showthread.php?tid=54801)



[REQUEST] Download subtitles from napiprojekt.pl website? - ukasz - 2009-07-16 22:34

hi

there is a cool program for downloading movie subtitles based on movie md5 checksum and size of a file.

can any 1 write a plugin that checks if txt file exists when attempting to play movie and if it doesn't exists it will download one from napiprojekt.pl

here is a python script working under linux:
Code:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#
#  Copyright (C) 2009 Arkadiusz MiĹ�kiewicz <arekm@pld-linux.org>
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#


import sys
import urllib
import subprocess
import tempfile
import os
import getopt

try:
    from hashlib import md5 as md5
except ImportError:
    from md5 import md5

napipass = 'iBlm8NTigvru0Jr0'

prog = os.path.basename(sys.argv[0])

video_files = [ 'asf', 'avi', 'divx', 'mkv', 'mp4', 'mpeg', 'mpg', 'ogm', 'rm', 'rmvb', 'wmv' ]
languages = { 'pl': 'PL', 'en': 'ENG' }

def f(z):
    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(z[i], 16)
        v = int(z[t:t+2], 16)
        b.append( ("%x" % (v*m))[-1] )

    return ''.join(b)

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
    print >> sys.stderr, "Supported options:"
    print >> sys.stderr, "     -h, --help            display this help and exit"
    print >> sys.stderr, "     -l, --lang=LANG       subtitles language"
    print >> sys.stderr, "     -n, --nobackup        make no backup when in update mode"
    print >> sys.stderr, "     -u, --update          fetch new and also update existing subtitles"
    print >> sys.stderr
    print >> sys.stderr, "pynapi $Revision: 1.27 $"
    print >> sys.stderr
    print >> sys.stderr, "Report bugs to <arekm@pld-linux.org>."

def main(argv=sys.argv):

    try:
        opts, args = getopt.getopt(argv[1:], "hl:nu", ["help", "lang", "nobackup", "update"])
    except getopt.GetoptError, err:
        print str(err)
        usage()
        return 2

    output = None
    verbose = False
    nobackup = False
    update = False
    lang = 'pl'
    for o, a in opts:
        if o == "-v":
            verbose = True
        elif o in ("-h", "--help"):
            usage()
            return 0
        elif o in ("-l", "--lang"):
            if a in languages:
                lang = a
            else:
                print >> sys.stderr, "%s: unsupported language `%s'. Supported languages: %s" % (prog, a, str(languages.keys()))
                return 1
        elif o in ("-n", "--nobackup"):
            nobackup = True
        elif o in ("-u", "--update"):
            update = True
        else:
            print >> sys.stderr, "%s: unhandled option" % prog
            return 1

    print >> sys.stderr, "%s: Subtitles language `%s'. Finding video files..." % (prog, lang)

    files = []
    for arg in args:
        if os.path.isdir(arg):
            for dirpath, dirnames, filenames in os.walk(arg, topdown=False):
                for file in filenames:
                    if file[-4:-3] == '.' and file.lower()[-3:] in video_files:
                        files.append(os.path.join(dirpath, file))
        else:
            files.append(arg)

    files.sort()

    i_total = len(files)
    i = 0

    for file in files:
        i += 1

        vfile = file + '.txt'
        if len(file) > 4:
            vfile = file[:-4] + '.txt'

        if not update and os.path.exists(vfile):
            continue

        if not nobackup and os.path.exists(vfile):
            vfile_bak = vfile + '-bak'
            try:
                os.rename(vfile, vfile_bak)
            except (IOError, OSError), e:
                print >> sys.stderr, "%s: Skipping due to backup of `%s' as `%s' failure: %s" % (prog, vfile, vfile_bak, e)
                continue
            else:
                print >> sys.stderr, "%s: Old subtitle backed up as `%s'" % (prog, vfile_bak)

        print >> sys.stderr, "%s: %d/%d: Processing subtitle for %s" % (prog, i, i_total, file)

        d = md5()
        try:
            d.update(open(file).read(10485760))
        except (IOError, OSError), e:
            print >> sys.stderr, "%s: %d/%d: Hashing video file failed: %s" % (prog, i, i_total, e)
            continue

        url = "http://napiprojekt.pl/unit_napisy/dl.php?l=%s&f=%s&t=%s&v=other&kolejka=false&nick=&pass=&napios=%s" % \
            (languages[lang], d.hexdigest(), f(d.hexdigest()), os.name)

        sub = None
        http_code = 200
        try:
            sub = urllib.urlopen(url)
            if hasattr(sub, 'getcode'):
                http_code = sub.getcode()
            sub = sub.read()
        except (IOError, OSError), e:
            print >> sys.stderr, "%s: %d/%d: Fetching subtitle failed: %s" % (prog, i, i_total, e)
            continue

        if http_code != 200:
            print >> sys.stderr, "%s: %d/%d: Fetching subtitle failed, HTTP code: %s" % (prog, i, i_total, str(http_code))
            continue

        if sub.startswith('NPc'):
            print >> sys.stderr, "%s: %d/%d: Subtitle NOT FOUND" % (prog, i, i_total)
            continue

        fp = tempfile.NamedTemporaryFile('wb', suffix=".7z")
        tfp = fp.name
        fp.write(sub)
        fp.flush()

        try:
            cmd = ['/usr/bin/7z', 'x', '-y', '-so', '-p' + napipass, tfp]
            sa = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
            (so, se) = sa.communicate(sub)
            retcode = sa.returncode
        except OSError, e:
            se = e
            retcode = True

        fp.close()

        if retcode:
            print >> sys.stderr, "%s: %d/%d: Subtitle decompression FAILED: %s" % (prog, i, i_total, se)
            continue

        fp = open(vfile, 'w')
        fp.write(so)
        fp.close()

        print >> sys.stderr, "%s: %d/%d: STORED (%d bytes)" % (prog, i, i_total, len(so))

    return 0

if __name__ == "__main__":
    ret = None
    try:
        ret = main()
    except (KeyboardInterrupt, SystemExit):
        print >> sys.stderr, "%s: Interrupted, aborting." % prog
    sys.exit(ret)



- gregd - 2009-09-10 21:53

I made (for my personal use) simple plugin to download subtitles from napiprojekt. This version works only on Windows. You can find it here.


- ukasz - 2009-09-10 22:47

do you know how that program works ? i want to make a console tool in c to download subtitles.

are you from Poland ?


- gregd - 2009-09-10 22:59

Quote:do you know how that program works
my program or your sample Tongue ?
Quote: want to make a console tool in c to download subtitles.
There is already qt version - qnapi , of course it have GUI...

Quote:are you from Poland ?
tak Big Grin


- ukasz - 2009-09-11 00:39

chodzi mi o zasde dzialania pobierania napisow. wiem ze sa spakowane 7zipem no i formuowany jest link do napisow. no i chodzi mi o to skad biora sie te dane ? jakies md5 pliku ?


- gregd - 2009-09-11 09:33

ukasz Wrote:chodzi mi o zasde dzialania pobierania napisow. wiem ze sa spakowane 7zipem no i formuowany jest link do napisow. no i chodzi mi o to skad biora sie te dane ? jakies md5 pliku ?

Liczy się md5 pierwszych 10 mb pliku. Potem dodatkowo wynik md5 jest wrzucany do funkcjii f (jakieś głupie zabezp. antyhakerowe ?):
Code:
def f(z):
    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(z[i], 16)
        v = int(z[t:t+2], 16)
        b.append( ("%x" % (v*m))[-1] )

    return ''.join(b)

Na podstawie wyniku tych dwóch operacji generowany jest link do pliku.


- wallie - 2010-02-09 23:52

Gratuluję pomysłu na plugin. W całym necie nie znalazłem niczego podobnego. Byłbym w siódmym niebie gdyby udało mi się go uruchomić. No właśnie. Jak z tego skorzystać? Będę wdzięczny za jakiekolwiek wskazówki co zrobić żeby to pobierało napisy.

Dziękuję i pozdrawiam.


- sylvec - 2010-03-19 23:00

Czy jest jakas szansa na to ze xnapi lub qnapi bedzie poprawnie dzialal pod xbmc na xboxie?


- gregd - 2010-03-20 20:43

sylvec Wrote:Czy jest jakas szansa na to ze xnapi lub qnapi bedzie poprawnie dzialal pod xbmc na xboxie?

Version modified by pajretX (http://xbmc-addons.googlecode.com/svn/trunk/plugins/video/xnapi/ - you can download it using SVN Repo Installer) - should be working fine, except network location. Python for xbox does not support samba...

PL => wersja zmodyfikowana przez pajretXa powinna działać o ile filmy nie są udostępnione przez sieć. Python na xboxie nie radzi sobie z sambą.


RE: [REQUEST] Download subtitles from napiprojekt.pl website? - Kokos - 2014-01-20 21:04

Hi guys,

As you may know - there was working functionality to download subtitles using NapiProject for XBMC (Coding by gregd). However recently it stopped working, I guess because of some NapiProject service changes.. Is there anybody there who knows Python and is able to fix the issue? That would be really great. Smile

-> https://github.com/amet/script.xbmc.subtitles/blob/frodo/script.xbmc.subtitles/resources/lib/services/Napiprojekt/service.py

Thank you and kind regards,
K