From 2ddcf86ee17e9ef3e8cf878365649bd69852f724 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Mon, 4 Mar 2024 21:22:25 +0530 Subject: server/opensubtitles: init search_name --- server/opensubtitles.lua | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) (limited to 'server/opensubtitles.lua') 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 } -- cgit v1.2.3