Skip to content

Instantly share code, notes, and snippets.

@dolohow
Last active February 18, 2019 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dolohow/4236f637d86e9565bf0c1dc059caba32 to your computer and use it in GitHub Desktop.
Save dolohow/4236f637d86e9565bf0c1dc059caba32 to your computer and use it in GitHub Desktop.
subtitles_download.py
#!/usr/bin/python2
import random
import string
import md5
import chardet
import sys
import urllib
import os
import glob
def hash(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)
filelist = glob.glob(sys.argv[1])
for f in filelist:
filename = os.path.splitext(f)[0]
if os.path.exists(filename + '.pl' + '.srt'):
continue
d = md5.new();
d.update(open(f).read(10485760))
str = "http://napiprojekt.pl/unit_napisy/dl.php?l=PL&f="+d.hexdigest()+"&t="+hash(d.hexdigest())+"&v=other&kolejka=false&nick=&pass=&napios="+os.name
random_filename = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(5))
open(random_filename, "wb").write(urllib.urlopen(str).read())
nazwa=filename + '.pl' + '.srt'
if (os.system("/usr/bin/7z x -y -so -piBlm8NTigvru0Jr0 " + random_filename + " 2>/dev/null >\""+nazwa+"\"")):
os.remove(nazwa)
print('No subtitles for ' + filename)
else:
if chardet.detect(open(nazwa).read())['encoding'] != 'utf-8':
os.system('iconv -f cp1250 -t utf8 "{}" > sub.tmp'.format(nazwa))
os.system('mv sub.tmp "{}"'.format(nazwa))
os.putenv('FILENAME', os.path.basename(f))
if not '--disable-notifications' in sys.argv:
os.system("~/notification_sub")
os.remove(random_filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment