-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnapi
executable file
·166 lines (148 loc) · 3.83 KB
/
napi
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env ruby
require 'digest'
require 'net/http'
require 'base64'
API_VERSION = ENV.fetch('NAPIPROJEKT_API_VERSION', 3)
ARCHIVE_PASSWORD = 'iBlm8NTigvru0Jr0'.freeze
ALLOWED_EXTENSIONS = %w(avi mp4 mkv mov webm flv rmvb mpg mpeg 3gp).freeze
TEN_MEGABYTES = 10_485_760
T_IDX = [0xe, 0x3, 0x6, 0x8, 0x2].freeze
T_MUL = [0x2, 0x2, 0x5, 0x4, 0x3].freeze
T_ADD = [0x0, 0xd, 0x10, 0xb, 0x5].freeze
DEFAULT_ENCODING = 'windows-1250'.freeze
CLIENTS = [
NAPIPROJEKT_PYTHON = 'NapiProjektPython'.freeze,
NAPIPROJEKT = 'Napiprojekt'.freeze,
AUTOMOVE = 'AutoMove'.freeze
].freeze
DOWNLOADED_SUBTITLES_TXT_MODES = [
DOWNLOADED_SUBTITLES_TXT_MODE_1 = 1,
DOWNLOADED_SUBTITLES_TXT_MODE_2 = 2
].freeze
DOWNLOADED_SUBTITLES_LANGUAGES = [
POLISH = 'PL'.freeze,
ENGLISH = 'ENG'.freeze
].freeze
API_MODES = [
API_MODE_1 = 1,
API_MODE_31 = 31,
API_MODE_32 = 32
].freeze
CLIENT_VERSIONS = [
'2.2.0.2399',
'1.0'
].freeze
LANGUAGE = ENV.fetch('NAPIPROJEKT_LANGUAGE', POLISH)
def download_method_name
"download_v#{API_VERSION}"
end
def save_method_name
"save_v#{API_VERSION}"
end
def md5(path)
Digest::MD5.hexdigest(File.read(path, TEN_MEGABYTES))
end
def f(md5sum)
b = ''
(0..4).each do |i|
a = T_ADD[i]
m = T_MUL[i]
g = T_IDX[i]
t = a + md5sum[g].to_i(16)
v = md5sum[t..t + 1].to_i(16)
x = v * m % 0x10
z = x.to_s(16)
b += z
end
b.downcase
end
def download_v1(md5sum)
response = get_response_v1(md5sum)
process_response_v1(response)
end
def get_response_v1(md5sum)
f_param = md5sum
t_param = f(md5sum)
url = "http://napiprojekt.pl/unit_napisy/dl.php?l=#{LANGUAGE}&f=#{f_param}"\
"&t=#{t_param}&v=pynapi&kolejka=false&nick=&pass=&napios=posix"
Net::HTTP.get(URI(url))
end
def process_response_v1(response_body)
return if response_body.lines.count < 5
response_body.dup.force_encoding(DEFAULT_ENCODING).encode('utf-8')
rescue Encoding::UndefinedConversionError
response_body.dup
end
def download_v3(md5sum)
response = get_response_v3(md5sum)
process_response_v3(response)
end
def get_response_v3(md5sum)
form = {
mode: API_MODE_1,
client: NAPIPROJEKT,
client_ver: CLIENT_VERSIONS.first,
downloaded_subtitles_id: md5sum,
downloaded_subtitles_txt: DOWNLOADED_SUBTITLES_TXT_MODE_1,
downloaded_subtitles_lang: LANGUAGE,
# user_nick: '',
# user_password: '',
# downloaded_cover_id: md5sum,
# downloaded_subtitles_txt: 2,
# advert_type: 'flashAllowed',
# video_info_hash: md5sum,
# nazwa_pliku: 'example.mp4',
# rozmiar_pliku_bajty: 1024,
# the: 'end'
}
url = 'http://napiprojekt.pl/api/api-napiprojekt3.php'
Net::HTTP.post_form(URI(url), form).body
end
def process_response_v3(response_body)
regex = /<content><!\[CDATA\[(?<subtitles>.+)\]\]><\/content>/
match = response_body.match(regex)
return unless match
encoded_subs = match[:subtitles]
Base64.decode64(encoded_subs)
end
def save(dir, base, response)
sub_path = "#{dir}/#{base}.txt"
File.write(sub_path, response)
end
def find_subtitles(path)
extension = File.extname(path).delete('.')
unless ALLOWED_EXTENSIONS.include?(extension)
puts "Format #{extension} is not supported"
return false
end
md5sum = md5(path)
response = send(download_method_name, md5sum)
if response
dir = File.dirname(path)
base = File.basename(path, '.*')
save(dir, base, response)
puts "[OK] #{path}"
return true
else
puts "[UNAV] #{path}"
return false
end
end
def find_subtitles_folder(path)
entries = Dir["#{path}/**/*"]
entries.each do |entry|
extension = File.extname(entry).delete('.')
next unless ALLOWED_EXTENSIONS.include?(extension)
Process.fork do
find_subtitles(entry)
exit
end
end
Process.waitall
end
path = ARGV[0]
if File.directory?(path)
find_subtitles_folder(path)
elsif File.file?(path)
find_subtitles(path)
end