diff options
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | server/opensubtitles.lua | 65 |
2 files changed, 66 insertions, 1 deletions
@@ -29,7 +29,7 @@ TODO - [x] use language from slang and fall back to english - [x] deprecate subscene (unreliable servers and poor db) - [x] add key binding to force subtitle lookup -- [ ] implement text search on all severs +- [x] implement text search on all severs - [x] remove the default retries - [ ] shill on reddit and matrix - [ ] first fetch normally when key is pressed then bring up a menu to select subs(we likely got it wrong) diff --git a/server/opensubtitles.lua b/server/opensubtitles.lua index 3817ff8..0d7af7b 100644 --- a/server/opensubtitles.lua +++ b/server/opensubtitles.lua @@ -101,6 +101,65 @@ local search_filesize = function (filesize, name, lang) 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 @@ -111,10 +170,15 @@ local search = function (path, out, info) 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 @@ -123,5 +187,6 @@ end return { search_ohash = search_ohash, search_filesize = search_filesize, + search_name = search_name, search = search } |