diff options
-rw-r--r-- | README.md | 2 | ||||
-rw-r--r-- | lib/iso639.lua | 202 | ||||
-rw-r--r-- | main.lua | 52 | ||||
-rw-r--r-- | server/opensubtitles.lua | 125 |
4 files changed, 256 insertions, 125 deletions
@@ -26,7 +26,7 @@ Dependencies TODO ---- -- [ ] use language from slang and fall back to english +- [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 diff --git a/lib/iso639.lua b/lib/iso639.lua new file mode 100644 index 0000000..6e853db --- /dev/null +++ b/lib/iso639.lua @@ -0,0 +1,202 @@ +#!/usr/bin/env luajit + +local M = {} + +local map = { + ['ab'] = 'abk', + ['aa'] = 'aar', + ['af'] = 'afr', + ['ak'] = 'aka', + ['sq'] = 'sqi', + ['am'] = 'amh', + ['ar'] = 'ara', + ['an'] = 'arg', + ['hy'] = 'hye', + ['as'] = 'asm', + ['av'] = 'ava', + ['ae'] = 'ave', + ['ay'] = 'aym', + ['az'] = 'aze', + ['bm'] = 'bam', + ['ba'] = 'bak', + ['eu'] = 'eus', + ['be'] = 'bel', + ['bn'] = 'ben', + ['bi'] = 'bis', + ['bs'] = 'bos', + ['br'] = 'bre', + ['bg'] = 'bul', + ['my'] = 'mya', + ['ca'] = 'cat', + ['ch'] = 'cha', + ['ce'] = 'che', + ['ny'] = 'nya', + ['zh'] = 'zho', + ['cu'] = 'chu', + ['cv'] = 'chv', + ['kw'] = 'cor', + ['co'] = 'cos', + ['cr'] = 'cre', + ['hr'] = 'hrv', + ['cs'] = 'ces', + ['da'] = 'dan', + ['dv'] = 'div', + ['nl'] = 'nld', + ['dz'] = 'dzo', + ['en'] = 'eng', + ['eo'] = 'epo', + ['et'] = 'est', + ['ee'] = 'ewe', + ['fo'] = 'fao', + ['fj'] = 'fij', + ['fi'] = 'fin', + ['fr'] = 'fra', + ['fy'] = 'fry', + ['ff'] = 'ful', + ['gd'] = 'gla', + ['gl'] = 'glg', + ['lg'] = 'lug', + ['ka'] = 'kat', + ['de'] = 'deu', + ['el'] = 'ell', + ['kl'] = 'kal', + ['gn'] = 'grn', + ['gu'] = 'guj', + ['ht'] = 'hat', + ['ha'] = 'hau', + ['he'] = 'heb', + ['hz'] = 'her', + ['hi'] = 'hin', + ['ho'] = 'hmo', + ['hu'] = 'hun', + ['is'] = 'isl', + ['io'] = 'ido', + ['ig'] = 'ibo', + ['id'] = 'ind', + ['ia'] = 'ina', + ['ie'] = 'ile', + ['iu'] = 'iku', + ['ik'] = 'ipk', + ['ga'] = 'gle', + ['it'] = 'ita', + ['ja'] = 'jpn', + ['jv'] = 'jav', + ['kn'] = 'kan', + ['kr'] = 'kau', + ['ks'] = 'kas', + ['kk'] = 'kaz', + ['km'] = 'khm', + ['ki'] = 'kik', + ['rw'] = 'kin', + ['ky'] = 'kir', + ['kv'] = 'kom', + ['kg'] = 'kon', + ['ko'] = 'kor', + ['kj'] = 'kua', + ['ku'] = 'kur', + ['lo'] = 'lao', + ['la'] = 'lat', + ['lv'] = 'lav', + ['li'] = 'lim', + ['ln'] = 'lin', + ['lt'] = 'lit', + ['lu'] = 'lub', + ['lb'] = 'ltz', + ['mk'] = 'mkd', + ['mg'] = 'mlg', + ['ms'] = 'msa', + ['ml'] = 'mal', + ['mt'] = 'mlt', + ['gv'] = 'glv', + ['mi'] = 'mri', + ['mr'] = 'mar', + ['mh'] = 'mah', + ['mn'] = 'mon', + ['na'] = 'nau', + ['nv'] = 'nav', + ['nd'] = 'nde', + ['nr'] = 'nbl', + ['ng'] = 'ndo', + ['ne'] = 'nep', + ['no'] = 'nor', + ['nb'] = 'nob', + ['nn'] = 'nno', + ['ii'] = 'iii', + ['oc'] = 'oci', + ['oj'] = 'oji', + ['or'] = 'ori', + ['om'] = 'orm', + ['os'] = 'oss', + ['pi'] = 'pli', + ['ps'] = 'pus', + ['fa'] = 'fas', + ['pl'] = 'pol', + ['pt'] = 'por', + ['pa'] = 'pan', + ['qu'] = 'que', + ['ro'] = 'ron', + ['rm'] = 'roh', + ['rn'] = 'run', + ['ru'] = 'rus', + ['se'] = 'sme', + ['sm'] = 'smo', + ['sg'] = 'sag', + ['sa'] = 'san', + ['sc'] = 'srd', + ['sr'] = 'srp', + ['sn'] = 'sna', + ['sd'] = 'snd', + ['si'] = 'sin', + ['sk'] = 'slk', + ['sl'] = 'slv', + ['so'] = 'som', + ['st'] = 'sot', + ['es'] = 'spa', + ['su'] = 'sun', + ['sw'] = 'swa', + ['ss'] = 'ssw', + ['sv'] = 'swe', + ['tl'] = 'tgl', + ['ty'] = 'tah', + ['tg'] = 'tgk', + ['ta'] = 'tam', + ['tt'] = 'tat', + ['te'] = 'tel', + ['th'] = 'tha', + ['bo'] = 'bod', + ['ti'] = 'tir', + ['to'] = 'ton', + ['ts'] = 'tso', + ['tn'] = 'tsn', + ['tr'] = 'tur', + ['tk'] = 'tuk', + ['tw'] = 'twi', + ['ug'] = 'uig', + ['uk'] = 'ukr', + ['ur'] = 'urd', + ['uz'] = 'uzb', + ['ve'] = 'ven', + ['vi'] = 'vie', + ['vo'] = 'vol', + ['wa'] = 'wln', + ['cy'] = 'cym', + ['wo'] = 'wol', + ['xh'] = 'xho', + ['yi'] = 'yid', + ['yo'] = 'yor', + ['za'] = 'zha', + ['zu'] = 'zul', +} + +M.toset2 = function(iso639_lang) + if iso639_lang:len() == 2 then + -- convert to set1 to set2 + return map[iso639_lang] + elseif iso639_lang:len() == 3 then + -- retun as it is if arg is already set 2. + -- we blindly trust it's valid if there's 3 chars + return iso639_lang + end +end + +return M @@ -1,7 +1,14 @@ local mutil = require 'mp.utils' local util = require 'lib.util' +local iso639 = require 'lib.iso639' local opensubtitles = require 'server.opensubtitles' +local default_lang = 'eng' + +local note = function (str) + mp.osd_message('mpvsub: ' .. str) +end + local mkdir = function (path) local info = mutil.file_info(path) if info and not info.is_dir then @@ -37,10 +44,29 @@ local sub_needed = function () return isvideo end +local getslang = function () + local sslang = {} + local slang = mp.get_property_native('slang') + + + for i = 1, #slang do + local lang = iso639.toset2(slang[i]) + if lang then + table.insert(sslang, lang) + end + end + + if #sslang < 1 then + slang.insert(default_lang) + end + + return sslang +end + local sub_setup = function () - local out, name, path, rc, filesize + local out, name, path, r, filesize, slangs, i - mp.osd_message('fetching subtitle') + note('fetching subtitle') out = mp.get_property_native('sub-file-paths')[1] if out then @@ -59,17 +85,25 @@ local sub_setup = function () end filesize = mp.get_property_native('file-size') - rc = opensubtitles.search(path, out, { - name = name, - filesize = filesize - }) + i = 1 + slangs = getslang() + repeat + r = opensubtitles.search(path, out, { + name = name, + filesize = filesize, + iso639_2_lang = slangs[i], + }) + + i = i + 1 + until r or i > #slangs - if rc then + if r then mp.commandv('rescan_external_files') mp.set_property('sid', 1) - mp.osd_message('fetch success') + + mp.osd_message('fetched ' .. slangs[i - 1] .. ' subtitles') else - mp.osd_message('fetch failure') + note('failed to fetch subtitles') end end diff --git a/server/opensubtitles.lua b/server/opensubtitles.lua index 4afc6ae..6cbec88 100644 --- a/server/opensubtitles.lua +++ b/server/opensubtitles.lua @@ -4,113 +4,6 @@ local curl = require 'lib.curl' local util = require 'lib.util' local attr = require 'lib.attr' --- [[ 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 tries = 10 @@ -166,11 +59,11 @@ local ids_fetch = function (page) return tab end -local search_ohash = function (ohash, name) +local search_ohash = function (ohash, name, lang) local fetch, hcode, url, id - url = domain .. '/en' .. '/search/sublanguageid-' .. - languages[language] .. '/moviehash-' .. ohash + url = domain .. '/en' .. '/search/sublanguageid-' .. lang .. + '/moviehash-' .. ohash fetch, hcode = curl.get(url, nil, nil, tries) id = attr.fuzzy(name, ids_fetch(fetch)) @@ -183,12 +76,12 @@ local search_ohash = function (ohash, name) end end -local search_filesize = function (filesize, name) +local search_filesize = function (filesize, name, lang) local fetch, hcode, url, id, a a = attr.build(name) - url = domain .. '/en' .. '/search/sublanguageid-' .. languages[language] + url = domain .. '/en' .. '/search/sublanguageid-' .. lang if a.season and a.episode then url = url .. '/season-' .. a.season .. '/episode-' .. a.episode end @@ -210,15 +103,17 @@ local search_filesize = function (filesize, name) end local search = function (path, out, info) - local ohash, link, name + 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) + link = search_ohash(ohash, name, lang) end if not link then - link = search_filesize(info.filesize, name) + link = search_filesize(info.filesize, name, lang) end if link then |