-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrapi
executable file
·78 lines (65 loc) · 1.97 KB
/
rapi
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
#!/usr/bin/env ruby
#encoding: utf-8
require "digest/md5"
require "open-uri"
PATH_TO_7ZIP = '/usr/bin/7z' # Linux
# PATH_TO_7ZIP = '/usr/local/Cellar/p7zip/9.20.1/bin/7z' # OSX
class Rapi
def initialize avi_file_path
@avi_file_path = avi_file_path
end
def download_txt
File.open('napisy.7z','w') do |f|
f.write(open(url).read)
end
`#{PATH_TO_7ZIP} x -y -so -piBlm8NTigvru0Jr0 napisy.7z 2>/dev/null > "#{output_file('txt')}"`
`rm napisy.7z`
if File.size?(output_file('txt')).nil?
File.unlink(output_file('txt'))
puts "Nie można znaleźć napisów dla #{@avi_file_path}"
else
puts "Pobrano napisy dla #{@avi_file_path}"
return output_file('txt')
end
end
private
def file_header
File.open(@avi_file_path).read(10485760)
end
def url
"http://napiprojekt.pl/unit_napisy/dl.php?l=PL&f=#{digest}&t=#{rapi_digest(digest)}&v=other&kolejka=false&nick=&pass=&napios=posix"
end
def digest
Digest::MD5.hexdigest(file_header)
end
def rapi_digest(z)
idx = [ 0xe, 0x3, 0x6, 0x8, 0x2 ]
mul = [ 2, 2, 5, 4, 3 ]
add = [ 0, 0xd, 0x10, 0xb, 0x5 ]
b = []
idx.length.times do |i|
a = add[i]
m = mul[i]
i = idx[i]
t = a + z.split('')[i].to_i(16)
v = z.split('')[t..t+1].join('').to_i(16)
b.push(('%x' % (v*m)).split('').last)
end
return b.join('')
end
def output_file(extension)
"#{File.dirname(@avi_file_path)}/#{File.basename(@avi_file_path, @avi_file_path.split('.').last)}#{extension}"
end
end
if ARGV.include?('-srt')
# To convert from txt to srt You need a txt2src programm
txt2srt = "#{File.dirname(__FILE__)}/txt2srt-1.0.8.jar"
txt_files = ARGV.reject {|r| r =~ /-/}.map { |arg| Rapi.new(arg).download_txt }.compact.each do |f|
cmd = "java -jar #{txt2srt} #{"\"#{f}\""}"
puts cmd
`#{cmd}`
end
`rm #{txt_files.join(' ')}`
else
ARGV.reject { |r| r =~ /^-/ }.each { |arg| Rapi.new(arg).download_txt }
end