aboutsummaryrefslogtreecommitdiff
path: root/server/opensubtitles.lua
diff options
context:
space:
mode:
authorsinanmohd <sinan@sinanmohd.com>2023-10-07 19:35:23 +0530
committersinanmohd <sinan@sinanmohd.com>2023-10-08 11:56:50 +0530
commit48700c12403ea1fc77d4fa6bd84ce1a208b876b0 (patch)
treeff5b4bd812e3eb0a6c0681cc5c34eb108ff32454 /server/opensubtitles.lua
parent5f5adabdeec7f2bf0897ef81fbc7e04dfed4b015 (diff)
server/opensubtitles: init search_filesize
Diffstat (limited to 'server/opensubtitles.lua')
-rw-r--r--server/opensubtitles.lua90
1 files changed, 86 insertions, 4 deletions
diff --git a/server/opensubtitles.lua b/server/opensubtitles.lua
index b797f44..f0c6195 100644
--- a/server/opensubtitles.lua
+++ b/server/opensubtitles.lua
@@ -2,6 +2,7 @@
local curl = require 'lib/curl'
local util = require 'lib/util'
+local attr = require 'lib/attr'
-- [[ languages supported by opensubtitles ]] --
local languages = {
@@ -134,14 +135,94 @@ local search_ohash = function (ohash)
end
end
-local search = function (path, out, name)
- local ohash, link
+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_filesize = function (filesize, name)
+ local fetch, hcode, url, id, a
+
+ a = attr.build(name)
+
+ url = domain .. '/en' .. '/search/sublanguageid-' .. languages[language]
+ 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, tries)
+ if not hcode then
+ return nil
+ end
+
+ print(url)
+ util.table_print(ids_fetch(fetch))
+ id = attr.fuzzy(name, ids_fetch(fetch))
+ if id then
+ print(domain .. '/en/subtitleserve/sub/' .. id)
+ return domain .. '/en/subtitleserve/sub/' .. id
+ end
+end
+
+local search = function (path, out, info)
+ local ohash, link, name
if util.file_exists(path) then
ohash = util.opensubtitles_hash(path)
link = search_ohash(ohash)
- else
- name = name or util.string_vid_path_to_name(path)
+ end
+
+ if not link then
+ name = info.name or util.string_vid_path_to_name(path)
+ link = search_filesize(info.filesize, name)
end
if link then
@@ -151,5 +232,6 @@ end
return {
search_ohash = search_ohash,
+ search_filesize = search_filesize,
search = search
}