diff options
author | sinanmohd <sinan@sinanmohd.com> | 2023-10-01 16:31:30 +0530 |
---|---|---|
committer | sinanmohd <sinan@sinanmohd.com> | 2023-10-01 19:59:15 +0530 |
commit | fee405edd055656dd63690e03a0d3e1fc0e96b4e (patch) | |
tree | 069ec4d564b2b05f6223a73fc4a696a745a9ffea | |
parent | e55ed555257f2ee7ede064c430ffd84cc165b334 (diff) |
server: init opensubtitles
-rw-r--r-- | lib/curl.lua | 4 | ||||
-rw-r--r-- | main.lua | 13 | ||||
-rw-r--r-- | server/opensubtitles.lua | 169 | ||||
-rwxr-xr-x | tests.lua | 14 |
4 files changed, 196 insertions, 4 deletions
diff --git a/lib/curl.lua b/lib/curl.lua index bcbcfe4..0728def 100644 --- a/lib/curl.lua +++ b/lib/curl.lua @@ -21,8 +21,8 @@ local get = function (url, headr, args) local cmd, fetch, scode headr = util.table_merge(headr, def_headr) - cmd = 'curl -s --compressed --write-out %{http_code} ' .. - url .. gen_head(headr) + cmd = "curl -s --compressed --write-out %{http_code} '" .. + url .. "' --globoff --location " .. gen_head(headr) if args then cmd = cmd .. ' ' .. args end @@ -1,6 +1,7 @@ local mutil = require 'mp.utils' local util = require 'lib/util' local subscene = require 'server/subscene' +local opensubtitles = require 'server/opensubtitles' local mkdir = function (path) local info = mutil.file_info(path) @@ -41,7 +42,7 @@ local sub_needed = function () end local sub_setup = function () - local out, name, path + local out, name, path, rc mp.osd_message('fetching subtitle') @@ -58,8 +59,16 @@ local sub_setup = function () name = util.string_vid_path_to_name(path) out = out .. '/' .. name .. '.srt' - if subscene.search(path, out) then + if not path:find('https?://') then + rc = opensubtitles.search(path, out) + end + if not rc then + rc = subscene.search(path, out) + end + + if rc then mp.commandv('rescan_external_files') + mp.set_property('sid', 1) mp.osd_message('fetch success') else mp.osd_message('fetch failure') diff --git a/server/opensubtitles.lua b/server/opensubtitles.lua new file mode 100644 index 0000000..9a5def4 --- /dev/null +++ b/server/opensubtitles.lua @@ -0,0 +1,169 @@ +#!/usr/bin/env lua + +local curl = require 'lib/curl' +local util = require 'lib/util' + +-- [[ languages supported by opensubtitles ]] -- +local languages = { + ['english'] = 'eng', + ['abkhazian'] = 'abk', + ['afrikaans'] = 'afr', + ['albanian'] = 'alb', + ['arabic'] = 'ara', + ['aragonese'] = 'arg', + ['armenian'] = 'arm', + ['assamese'] = 'asm', + ['asturian'] = 'ast', + ['azerbaijani'] = 'aze', + ['basque'] = 'baq', + ['belarusian'] = 'bel', + ['bengali'] = 'ben', + ['bosnian'] = 'bos', + ['breton'] = 'bre', + ['bulgarian'] = 'bul', + ['burmese'] = 'bur', + ['catalan'] = 'cat', + ['chinese (simplified)'] = 'chi', + ['chinese (traditional)'] = 'zht', + ['chinese bilingual'] = 'zhe', + ['croatian'] = 'hrv', + ['czech'] = 'cze', + ['danish'] = 'dan', + ['dari'] = 'prs', + ['dutch'] = 'dut', + ['esperanto'] = 'epo', + ['estonian'] = 'est', + ['extremaduran'] = 'ext', + ['finnish'] = 'fin', + ['french'] = 'fre', + ['gaelic'] = 'gla', + ['galician'] = 'glg', + ['georgian'] = 'geo', + ['german'] = 'ger', + ['greek'] = 'ell', + ['hebrew'] = 'heb', + ['hindi'] = 'hin', + ['hungarian'] = 'hun', + ['icelandic'] = 'ice', + ['igbo'] = 'ibo', + ['indonesian'] = 'ind', + ['interlingua'] = 'ina', + ['irish'] = 'gle', + ['italian'] = 'ita', + ['japanese'] = 'jpn', + ['kannada'] = 'kan', + ['kazakh'] = 'kaz', + ['khmer'] = 'khm', + ['korean'] = 'kor', + ['kurdish'] = 'kur', + ['latvian'] = 'lav', + ['lithuanian'] = 'lit', + ['luxembourgish'] = 'ltz', + ['macedonian'] = 'mac', + ['malay'] = 'may', + ['malayalam'] = 'mal', + ['manipuri'] = 'mni', + ['marathi'] = 'mar', + ['mongolian'] = 'mon', + ['montenegrin'] = 'mne', + ['navajo'] = 'nav', + ['nepali'] = 'nep', + ['northern sami'] = 'sme', + ['norwegian'] = 'nor', + ['occitan'] = 'oci', + ['odia'] = 'ori', + ['persian'] = 'per', + ['polish'] = 'pol', + ['portuguese'] = 'por', + ['portuguese (br)'] = 'pob', + ['portuguese (mz)'] = 'pom', + ['pushto'] = 'pus', + ['romanian'] = 'rum', + ['russian'] = 'rus', + ['santali'] = 'sat', + ['serbian'] = 'scc', + ['sindhi'] = 'snd', + ['sinhalese'] = 'sin', + ['slovak'] = 'slo', + ['slovenian'] = 'slv', + ['somali'] = 'som', + ['spanish'] = 'spa', + ['spanish (eu)'] = 'spn', + ['spanish (la)'] = 'spl', + ['swahili'] = 'swa', + ['swedish'] = 'swe', + ['syriac'] = 'syr', + ['tagalog'] = 'tgl', + ['tamil'] = 'tam', + ['tatar'] = 'tat', + ['telugu'] = 'tel', + ['thai'] = 'tha', + ['toki pona'] = 'tok', + ['turkish'] = 'tur', + ['turkmen'] = 'tuk', + ['ukrainian'] = 'ukr', + ['urdu'] = 'urd', + ['uzbek'] = 'uzb', + ['vietnamese'] = 'vie', + ['welsh'] = 'wel', +} + + +local language = 'english' +local domain = 'https://www.opensubtitles.org' +local retries = 10 + +local search_ohash = function (ohash) + local fetch, tries, hcode, url, id + + url = domain .. '/en' .. '/search/sublanguageid-' .. + languages[language] .. '/moviehash-' .. ohash + tries = 0 + repeat + fetch, hcode = curl.get(url, nil, nil) + tries = tries + 1 + until hcode == 200 or tries > retries + + id = fetch:match('/en/subtitleserve/sub/[^\n]*\n[^\n]*iduser%-0') + if id then + id = id:match('/en/subtitleserve/sub/%d*') + end + + if id then + return domain .. id + end +end + +local sub_fetch = function(link, out) + local tries, hcode, zip, zcode + + tries = 0 + zip = os.tmpname() + + repeat + _, hcode = curl.get(link, nil, '-o ' .. zip) + tries = tries + 1 + until hcode == 200 or tries > retries + + if hcode == 200 then + zcode = util.zip_ext_first(zip, out) + end + os.remove(zip) + + return (hcode == 200) and zcode +end + +local search = function (path, out) + local ohash, link + + ohash = util.opensubtitles_hash(path) + link = search_ohash(ohash) + if link then + return sub_fetch(link, out) + end +end + +return { + search_ohash = search_ohash, + search = search +} @@ -1,6 +1,7 @@ #!/usr/bin/env lua local subscene = require 'server/subscene' +local opensubtitles = require 'server/opensubtitles' local util = require 'lib/util' local errs = 0 @@ -26,7 +27,20 @@ local test_subscene = function () os.remove(out) end +local test_opensubtitles = function () + local ohash, id, new_id + + ohash = '395787dbe5b42001' + id = '5449593' + new_id = opensubtitles.search_ohash(ohash):match('%d*$') + if id ~= new_id then + util.error('opensubtitles: id mismatch') + errs = errs + 1 + end +end + test_subscene() +test_opensubtitles() if errs == 0 then print('ok: all tests ran successfully') |