login signup | whydoineedaccount?
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/python
# vim: filencoding=utf-8 foldmethod=indent
# 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!

#
# Drobne modyfikacje michalzxc
#

#
# Poprawiony styl i logika programu: i0cus@jabster.pl, 21/01/08
# Dodano: 
#   * okno wyboru pliku zenity;
#   * ujednolicony interfejs komunikatów (cli oraz gui);
# TODO:
#   * sprawdzenie logiki programu (sanity checks);
#   * wyeksportowanie bloków do funkcji, by umożliwić:
#   * pobieranie wielu plików w trybie okienkowym;
#   * tryb interaktywny?
# Uwagi:
#   * dos2unix wymaga frodos (pakiet w ubuntu)
#

import md5
import sys
import urllib
import os
import shutil
import commands

def f(z):
    'foobar hash function? ;-)'
    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 write2zenity(msg, msg_type='info'):
    'zenity message sink'
    os.system('zenity --%s --text="%s" --title="Napiprojekt"' % (msg_type, msg))

def get_file(msg):
    'zenity "file-input" dialog'
    return commands.getoutput('zenity --file-selection title="Napiprojekt"')

def write2stdout(msg, msg_type='info'):
    'stdout message sink'
    print '%s: %s' % (msg_type, msg)

printout = write2stdout

if 'zenity' in sys.argv[1:]:
    printout = write2zenity

mname, filenames = '', [s for s in sys.argv[1:] if s != 'zenity']
if filenames:
    mname = filenames.pop(0)
else:    
    if printout is write2zenity:
        mname = get_file('Wskaż film do którego szukasz napisów')

if not mname:
        printout('Wskaż film do którego szukasz napisów.', 'error')
        sys.exit(2)
elif not os.path.isfile(mname):
    printout('Plik: "%s" nie istnieje' % mname, 'error')
    sys.exit(2)

fname = mname[:mname.rfind('.')+1]+'txt' # dowolne rozserzenie

d = md5.new();
d.update(open(mname).read(10485760))

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

try:
    open('napisy.7z','w').write(urllib.urlopen(url).read())
except IOError, e:
    printout('Błąd w czasie pobierania napisów, zobacz stderr.', 'error')
    print>>sys.stderr, e
    sys.exit(2)

if (os.system('/usr/bin/7z x -y -so -piBlm8NTigvru0Jr0 napisy.7z 2>/dev/null >"%s.wyp"' % fname)):
    os.remove('%s.wyp' % fname)
    printout('Nie znaleziono napisów do filmu %s.' % os.path.basename(mname), 'error')
else:
    shutil.move('%s.wyp' % fname, fname)
    os.system('dos2unix -U "%s"' % fname)
    kodowanie=commands.getoutput('/usr/bin/file "%s"' % fname)
    kodowanie=kodowanie[len(fname)+2:]
    iconv_cmd = '/usr/bin/iconv --from-code=%s --to-code=utf-8 --output="%s.tmp" "%s"'
    from_enc = None
    if ('UTF-8 Unicode text' in kodowanie):
        pass
    elif ('ISO-8859 text' in kodowanie):
        from_enc = 'iso-8859-2'
    else:
        #TODO: niektóre napisy są rozpoznawane jako ,,niestandardowe ASCII'', co wtedy?
        from_enc = 'Windows-1250'
    if not from_enc is None:
        os.system(iconv_cmd % (from_enc, fname, fname))
        shutil.move('%s.tmp' % fname, fname)   
    printout('Pobrano napisy %s.' % os.path.basename(fname))

os.remove('napisy.7z')
back to top ↑

Did you know, that…?

wklej.org it's a NoPaste site, which allows you to paste here any text, or source code, which will be available under special URL. Thanks to this, you can make forums or IRC channels more readable