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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
#!/usr/bin/env lua
local curl = require 'lib.curl'
local util = require 'lib.util'
local attr = require 'lib.attr'
local domain = 'https://www.opensubtitles.org'
local ids_fetch = function (page)
local iter, no_name, line, id, name, tab
tab = {}
no_name = 0
iter = page:gmatch('[^\n\r]+')
while true do
line = iter()
if not line then
break
end
id = line:match('/en/subtitles/%d*')
if id then
id = id:match('%d+$')
line = iter() -- movie
if line:find('%.%.%.$') then
-- name cuts off...
name = line:gsub('"[^"]*$', '')
name = name:match('[^"]+$')
else
name = line:gsub('<br/><a rel.*$', '')
name = name:match('[^>]+$')
end
if not name then
line = iter()
if line:find('^%[S%d%dE%d%d%]$') then
-- it's a series
line = iter()
if line:find('%.%.%.$') then
name = line:gsub('^.*title="', '')
name = name:match('[^"]+')
else
name = line:match('[^<]+')
end
else
-- no name
name = tostring(no_name)
no_name = no_name + 1
end
end
tab[name] = id
end
end
return tab
end
local search_ohash = function (ohash, name, lang)
local fetch, hcode, url, id
url = domain .. '/en' .. '/search/sublanguageid-' .. lang ..
'/moviehash-' .. ohash
fetch, hcode = curl.get(url, nil, nil)
id = attr.fuzzy(name, ids_fetch(fetch))
if hcode and not id then
util.error('opensubtitles: search_ohash failed')
end
if id then
return domain .. '/en/subtitleserve/sub/' .. id
end
end
local search_filesize = function (filesize, name, lang)
local fetch, hcode, url, id, a
a = attr.build(name)
url = domain .. '/en' .. '/search/sublanguageid-' .. lang
if a.season and a.episode then
url = url .. '/season-' .. a.season .. '/episode-' .. a.episode
end
url = url .. '/moviebytesize-' .. filesize
fetch, hcode = curl.get(url, nil, nil)
if not hcode then
return nil
end
id = attr.fuzzy(name, ids_fetch(fetch))
if hcode and not id then
util.error('opensubtitles: search_filesize failed')
end
if id then
return domain .. '/en/subtitleserve/sub/' .. id
end
end
local url_encode = function (str)
local url = ""
for c in str:gmatch('.') do
if c:find('%w') then
url = url .. c
else
url = url .. '%' ..
string.format('%x', c:byte()):upper()
end
end
return url
end
local suggest_id = function (name)
local url, fetch, hcode, id
url = domain .. '/libs/suggest.php?format=json3&MovieName=' ..
url_encode(name)
fetch, hcode = curl.get(url, nil, nil)
if not hcode then
return nil
end
id = fetch:match('"id":%d+')
if not id then
return nil
end
return id:match('%d+')
end
local search_name = function (name, lang)
local fetch, hcode, movie_id, url, id
movie_id = suggest_id(name)
if not movie_id then
return nil
end
url = domain .. '/en/search/sublanguageid-' .. lang ..
'/idmovie-' .. movie_id
fetch, hcode = curl.get(url, nil, nil)
if not hcode then
return nil
end
id = attr.fuzzy(name, ids_fetch(fetch))
if not id then
util.error('opensubtitles: search_name failed')
end
if id then
return domain .. '/en/subtitleserve/sub/' .. id
end
end
local search = function (path, out, info)
local ohash, link, name, lang
lang = info.iso639_2_lang or 'eng'
name = info.name or util.string_vid_path_to_name(path)
if util.file_exists(path) then
ohash = util.opensubtitles_hash(path)
link = search_ohash(ohash, name, lang)
end
if not link then
link = search_filesize(info.filesize, name, lang)
end
if not link then
link = search_name(name, lang)
end
if link then
return curl.zip_link_to_file(link, nil, out)
end
end
return {
search_ohash = search_ohash,
search_filesize = search_filesize,
search_name = search_name,
search = search
}
|