diff options
-rw-r--r-- | lib/attr.lua | 98 | ||||
-rw-r--r-- | lib/curl.lua | 36 | ||||
-rw-r--r-- | lib/iso639.lua | 366 | ||||
-rw-r--r-- | lib/util.lua | 54 | ||||
-rw-r--r-- | main.lua | 195 | ||||
-rw-r--r-- | server/opensubtitles.lua | 95 | ||||
-rw-r--r-- | stylua.toml | 4 | ||||
-rwxr-xr-x | tests.lua | 43 |
8 files changed, 451 insertions, 440 deletions
diff --git a/lib/attr.lua b/lib/attr.lua index 5735083..a20b881 100644 --- a/lib/attr.lua +++ b/lib/attr.lua @@ -1,64 +1,64 @@ #!/usr/bin/env lua -local util = require 'lib.util' +local util = require("lib.util") -local extract = function (name, patterns) +local extract = function(name, patterns) local r for _, p in pairs(patterns) do r = r or name:match(p) - name = name:gsub(p, '') + name = name:gsub(p, "") end return name, r end -local build_dlim = function (name, attrs) - local dlim ,r +local build_dlim = function(name, attrs) + local dlim, r attrs = attrs or {} - dlim = '[%-%.%s]?' + dlim = "[%-%.%s]?" local vcodecs = { - '[M]m[Pp][Ee][Gg]' .. dlim .. '[1234]', + "[M]m[Pp][Ee][Gg]" .. dlim .. "[1234]", } local acodecs = { - '[Dd][Tt][Ss]' .. dlim .. '[Hh][Dd]', - '[Dd][Dd]' .. dlim .. '[57]%.1', + "[Dd][Tt][Ss]" .. dlim .. "[Hh][Dd]", + "[Dd][Dd]" .. dlim .. "[57]%.1", } - local sources= { + local sources = { "[Ww][Ee][Bb]" .. dlim .. "[Dd][Ll]", "[Hh][Dd]" .. dlim .. "[Tt][Vv]", "[Hh][Dd]" .. dlim .. "[Tt][Ss]", } local series = { - '[Ss]%d%d?' .. dlim .. '[Ee]%d%d?', - '[Ss]%d%d?' .. dlim .. '[Ee][Pp]' .. dlim .. '%d%d?', - 'Season' .. dlim .. '%d%d?' .. dlim .. 'Episode' .. dlim .. '%d%d?' + "[Ss]%d%d?" .. dlim .. "[Ee]%d%d?", + "[Ss]%d%d?" .. dlim .. "[Ee][Pp]" .. dlim .. "%d%d?", + "Season" .. dlim .. "%d%d?" .. dlim .. "Episode" .. dlim .. "%d%d?", } local sizes = { - '%d%d%d' .. dlim .. '[Mm][Bb]', - '%d%d?%.%d%d?' .. dlim .. '[Gg][Bb]', + "%d%d%d" .. dlim .. "[Mm][Bb]", + "%d%d?%.%d%d?" .. dlim .. "[Gg][Bb]", } local depths = { - '1[02]' .. dlim .. '[Bb][Ii][Tt]' + "1[02]" .. dlim .. "[Bb][Ii][Tt]", } name, attrs.vcodec = extract(name, vcodecs) name, attrs.source = extract(name, sources) - name, attrs.acodecs = extract(name, acodecs) - name, attrs.size = extract(name, sizes) - name, attrs.depth = extract(name, depths) + name, attrs.acodecs = extract(name, acodecs) + name, attrs.size = extract(name, sizes) + name, attrs.depth = extract(name, depths) name, r = extract(name, series) if r then - attrs.season = tonumber(r:match('%d+')) - attrs.episode = tonumber(r:match('%d+$')) + attrs.season = tonumber(r:match("%d+")) + attrs.episode = tonumber(r:match("%d+$")) end return name, attrs end -local build_atom = function (name, attrs) +local build_atom = function(name, attrs) local r, year attrs = attrs or {} @@ -75,7 +75,7 @@ local build_atom = function (name, attrs) "[Ee]?[Aa][Cc]3", "[Dd][Tt][Ss]", } - local sources= { + local sources = { "[Bb][Ll][Uu][Rr][Aa][Yy]", "[Bb][Rr][Rr][Ii][Pp]", "[Dd][Vv][Dd][Rr][Ii][Pp]", @@ -90,50 +90,50 @@ local build_atom = function (name, attrs) "720[Pp]", "480[Pp]", "[Uu][Hh][Dd]", - "4[Kk]" + "4[Kk]", } local series = { - '%d%d[Xx]%d%d', + "%d%d[Xx]%d%d", } local channels = { - '6[Cc][Hh]', - '[57]%.1', + "6[Cc][Hh]", + "[57]%.1", } name, attrs.vcodec = extract(name, vcodecs) name, attrs.source = extract(name, sources) - name, attrs.res = extract(name, reses) - name, attrs.acodecs = extract(name, acodecs) - name, attrs.channel = extract(name, channels) + name, attrs.res = extract(name, reses) + name, attrs.acodecs = extract(name, acodecs) + name, attrs.channel = extract(name, channels) name, r = extract(name, series) if r then - attrs.season = tonumber(r:match('%d+')) - attrs.episode = tonumber(r:match('%d+$')) + attrs.season = tonumber(r:match("%d+")) + attrs.episode = tonumber(r:match("%d+$")) end - for y in name:gmatch('%d%d%d%d') do + for y in name:gmatch("%d%d%d%d") do year = tonumber(y) - if year > 1900 and year <= tonumber(os.date('%Y')) then + if year > 1900 and year <= tonumber(os.date("%Y")) then attrs.year = y end end - if attrs.year then - name = name:gsub(tostring(attrs.year), '') + if attrs.year then + name = name:gsub(tostring(attrs.year), "") end return name, attrs end -local build_low = function (name, attrs) +local build_low = function(name, attrs) local low_attr, lows - lows = { 'SDH' } + lows = { "SDH" } low_attr = {} for _, low in pairs(lows) do low_attr[#low_attr + 1] = name:match(low) - name = name:gsub(low, '') + name = name:gsub(low, "") end attrs = attrs or {} @@ -144,9 +144,9 @@ local build_low = function (name, attrs) return name, attrs end -local build_title = function (name, attrs) +local build_title = function(name, attrs) attrs.title = {} - for w in name:gmatch('%w+') do + for w in name:gmatch("%w+") do attrs.title[#attrs.title + 1] = w end @@ -158,7 +158,7 @@ local build_title = function (name, attrs) return attrs end -local build = function (name) +local build = function(name) local attrs = {} name = build_dlim(name, attrs) @@ -169,7 +169,7 @@ local build = function (name) return attrs end -local weigh = function (a1, a2) +local weigh = function(a1, a2) local key_score, score key_score = { @@ -186,17 +186,17 @@ local weigh = function (a1, a2) score = 0 for k, v in pairs(a1) do - if not a2[k] or k == 'episode' then + if not a2[k] or k == "episode" then goto continue end - if k == 'title' then + if k == "title" then for _, title in pairs(v) do if util.array_search(a2.title, title) then score = score + key_score.title end end - elseif k == 'season' then + elseif k == "season" then if a1.season == a2.season and a1.episode == a2.episode then score = score + key_score.season + key_score.episode end @@ -206,18 +206,18 @@ local weigh = function (a1, a2) end end - ::continue:: + ::continue:: end return score end -local fuzzy = function (name, tab) +local fuzzy = function(name, tab) local name_attr, high, score high = { score = 0, - name = next(tab) + name = next(tab), } name_attr = build(name) diff --git a/lib/curl.lua b/lib/curl.lua index cfc1c11..4c56325 100644 --- a/lib/curl.lua +++ b/lib/curl.lua @@ -1,33 +1,33 @@ #!/usr/bin/env lua -local util = require 'lib.util' +local util = require("lib.util") local def_headr = { - ['User-Agent'] = 'cia', - ['Accept-Encoding'] = 'gzip, deflate, br' + ["User-Agent"] = "cia", + ["Accept-Encoding"] = "gzip, deflate, br", } -local head_to_args = function (t) +local head_to_args = function(t) local args = {} for k, v in pairs(t) do - args[#args + 1] = '-H' .. k .. ": " .. v + args[#args + 1] = "-H" .. k .. ": " .. v end return args end -local get = function (url, headr, args) +local get = function(url, headr, args) local fetch, hcode, def_args def_args = { - 'curl', - '--silent', - '--compressed', - '--write-out', - '%{http_code}', - '--globoff', - '--location', + "curl", + "--silent", + "--compressed", + "--write-out", + "%{http_code}", + "--globoff", + "--location", url, } @@ -36,17 +36,17 @@ local get = function (url, headr, args) args = util.array_merge(args, head_to_args(headr)) fetch = util.run(args) - hcode = fetch:match('%d*$') - fetch = fetch:gsub('%s*%d*$', '') + hcode = fetch:match("%d*$") + fetch = fetch:gsub("%s*%d*$", "") return fetch, tonumber(hcode) end -local zip_link_to_file = function (url, headr, out) +local zip_link_to_file = function(url, headr, out) local hcode, zip, rc, args zip = os.tmpname() - args = { '-o'.. zip } + args = { "-o" .. zip } _, hcode = get(url, headr, args) @@ -57,7 +57,7 @@ local zip_link_to_file = function (url, headr, out) os.remove(zip) if hcode and not rc then - util.error('curl: zip_link_to_file') + util.error("curl: zip_link_to_file") end return rc diff --git a/lib/iso639.lua b/lib/iso639.lua index 6e853db..81fa433 100644 --- a/lib/iso639.lua +++ b/lib/iso639.lua @@ -3,189 +3,189 @@ 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', + ["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) diff --git a/lib/util.lua b/lib/util.lua index 5b524bf..ce41743 100644 --- a/lib/util.lua +++ b/lib/util.lua @@ -1,26 +1,26 @@ #!/usr/bin/env lua -local table_to_cmd = function (t) +local table_to_cmd = function(t) local str = "" for _, v in ipairs(t) do - v = v:gsub("'", "\'") + v = v:gsub("'", "'") str = str .. " '" .. v .. "' " end return str end -local file_exists = function (path) - return io.open(path, 'r') and true or false +local file_exists = function(path) + return io.open(path, "r") and true or false end -local run = function (args) +local run = function(args) local sig, rc, stdout, cmd if mp then cmd = mp.command_native({ - name = 'subprocess', + name = "subprocess", capture_stdout = true, args = args, }) @@ -31,16 +31,16 @@ local run = function (args) else cmd = io.popen(table_to_cmd(args)) if cmd then - stdout = cmd:read('*all') + stdout = cmd:read("*all") _, sig, rc = cmd:close() - rc = (sig == 'signal') or (sig == 'exit' and rc == 0) + rc = (sig == "signal") or (sig == "exit" and rc == 0) end end return stdout or "", rc end -local table_merge = function (t1, t2) +local table_merge = function(t1, t2) local t = {} t1 = t1 or {} t2 = t2 or {} @@ -55,7 +55,7 @@ local table_merge = function (t1, t2) return t end -local array_merge = function (a1, a2) +local array_merge = function(a1, a2) local a = {} a1 = a1 or {} a2 = a2 or {} @@ -70,7 +70,7 @@ local array_merge = function (a1, a2) return a end -local array_search = function (a, key) +local array_search = function(a, key) for _, v in pairs(a) do if v == key then return true @@ -80,16 +80,16 @@ local array_search = function (a, key) return false end -local table_print = function (t) +local table_print = function(t) for k, v in pairs(t) do - print( '|'.. k .. '=' .. v .. '|') + print("|" .. k .. "=" .. v .. "|") end end -local table_match_or_any = function (t, key) +local table_match_or_any = function(t, key) local str - str= t[key] + str = t[key] if not str then _, str = next(t, nil) end @@ -97,22 +97,22 @@ local table_match_or_any = function (t, key) return str end -local zip_ext_first = function (zip, out) +local zip_ext_first = function(zip, out) local dir, rc, srt, findcmd dir = os.tmpname() os.remove(dir) - findcmd = { 'find', dir, '-type', 'f', '-name', '*.srt' } + findcmd = { "find", dir, "-type", "f", "-name", "*.srt" } - _, rc = run({ 'unzip', '-qq', zip, '-d', dir }) - srt = run(findcmd):match('[^\n]*') - run({ 'mv', srt, out }) + _, rc = run({ "unzip", "-qq", zip, "-d", dir }) + srt = run(findcmd):match("[^\n]*") + run({ "mv", srt, out }) os.remove(dir) return rc end -local string_vid_path_to_name = function (str) +local string_vid_path_to_name = function(str) local extensions = { "mkv", "mp4", @@ -122,19 +122,19 @@ local string_vid_path_to_name = function (str) "gifv", "avi", "mpeg", - "3gp" + "3gp", } - str = str:match('[^/]*$') + str = str:match("[^/]*$") for _, ext in ipairs(extensions) do - str = str:gsub('.' .. ext, '') + str = str:gsub("." .. ext, "") end return str end -local error = function (str) - str = 'error: ' .. str +local error = function(str) + str = "error: " .. str if mp then mp.msg.warn(str) else @@ -142,7 +142,7 @@ local error = function (str) end end -local opensubtitles_hash = function (fileName) +local opensubtitles_hash = function(fileName) local fil = io.open(fileName, "rb") if not fil then return nil @@ -1,115 +1,116 @@ -local mutil = require 'mp.utils' -local util = require 'lib.util' -local iso639 = require 'lib.iso639' -local opensubtitles = require 'server.opensubtitles' +local iso639 = require("lib.iso639") +local mutil = require("mp.utils") +local opensubtitles = require("server.opensubtitles") +local util = require("lib.util") -local default_lang = 'eng' +local default_lang = "eng" -local note = function (str) - mp.osd_message('mpvsub: ' .. str) +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 - os.remove(path) - end +local mkdir = function(path) + local info = mutil.file_info(path) + if info and not info.is_dir then + os.remove(path) + end - return util.run({ 'mkdir', '-p', path }) + return util.run({ "mkdir", "-p", path }) end -local sub_needed = function () - local duration, isvideo, name - - name = mp.get_property_native('path') - if name:find('https?://www.youtube.com/') or - name:find('https?://youtu.be/') then - return false - end - - duration = mp.get_property('duration') - if not duration or tonumber(duration) < 900 then - return false - end -- ensure duration is more than 15 minutes - - for _, v in pairs(mp.get_property_native('track-list')) do - if v['type'] == 'sub' then - return false - end - if v['type'] == 'video' then - isvideo = true - end - end - - return isvideo +local sub_needed = function() + local duration, isvideo, name + + name = mp.get_property_native("path") + if + name:find("https?://www.youtube.com/") + or name:find("https?://youtu.be/") + then + return false + end + + duration = mp.get_property("duration") + if not duration or tonumber(duration) < 900 then + return false + end -- ensure duration is more than 15 minutes + + for _, v in pairs(mp.get_property_native("track-list")) do + if v["type"] == "sub" then + return false + end + if v["type"] == "video" then + isvideo = true + end + end + + return isvideo end -local getslang = function () - local sslang = {} - local slang = mp.get_property_native('slang') +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 - 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 - if #sslang < 1 then - slang.insert(default_lang) - end - - return sslang + return sslang end -local sub_setup = function () - local out, name, path, r, filesize, slangs, i - - note('fetching subtitle') - - out = mp.get_property_native('sub-file-paths')[1] - if out then - out = out:gsub('^~/', os.getenv('HOME') .. '/') - else - out = os.getenv('HOME') .. '/.local/share/mpv/subs' - mp.set_property('sub-file-paths', out) - end - - mkdir(out) - path = mp.get_property_native('path') - out = out .. '/' .. util.string_vid_path_to_name(path) .. '.srt' - - if not util.file_exists(path) then - name = mp.get_property_native('media-title') - end - filesize = mp.get_property_native('file-size') - - 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 r then - mp.commandv('rescan_external_files') - mp.osd_message('fetched ' .. slangs[i - 1] .. ' subtitles') - else - note('failed to fetch subtitles') - end +local sub_setup = function() + local out, name, path, r, filesize, slangs, i + + note("fetching subtitle") + + out = mp.get_property_native("sub-file-paths")[1] + if out then + out = out:gsub("^~/", os.getenv("HOME") .. "/") + else + out = os.getenv("HOME") .. "/.local/share/mpv/subs" + mp.set_property("sub-file-paths", out) + end + + mkdir(out) + path = mp.get_property_native("path") + out = out .. "/" .. util.string_vid_path_to_name(path) .. ".srt" + + if not util.file_exists(path) then + name = mp.get_property_native("media-title") + end + filesize = mp.get_property_native("file-size") + + 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 r then + mp.commandv("rescan_external_files") + mp.osd_message("fetched " .. slangs[i - 1] .. " subtitles") + else + note("failed to fetch subtitles") + end end -local file_listener = function () - if sub_needed() then - sub_setup() - end +local file_listener = function() + if sub_needed() then + sub_setup() + end end -mp.register_event('file-loaded', file_listener) -mp.add_key_binding('b', "mpvsub", sub_setup) +mp.register_event("file-loaded", file_listener) +mp.add_key_binding("b", "mpvsub", sub_setup) diff --git a/server/opensubtitles.lua b/server/opensubtitles.lua index 0d7af7b..5f73d7f 100644 --- a/server/opensubtitles.lua +++ b/server/opensubtitles.lua @@ -1,48 +1,48 @@ #!/usr/bin/env lua -local curl = require 'lib.curl' -local util = require 'lib.util' -local attr = require 'lib.attr' +local attr = require("lib.attr") +local curl = require("lib.curl") +local util = require("lib.util") -local domain = 'https://www.opensubtitles.org' +local domain = "https://www.opensubtitles.org" -local ids_fetch = function (page) +local ids_fetch = function(page) local iter, no_name, line, id, name, tab tab = {} no_name = 0 - iter = page:gmatch('[^\n\r]+') + iter = page:gmatch("[^\n\r]+") while true do line = iter() if not line then break end - id = line:match('/en/subtitles/%d*') + id = line:match("/en/subtitles/%d*") if id then - id = id:match('%d+$') + id = id:match("%d+$") line = iter() -- movie - if line:find('%.%.%.$') then + if line:find("%.%.%.$") then -- name cuts off... - name = line:gsub('"[^"]*$', '') + name = line:gsub('"[^"]*$', "") name = name:match('[^"]+$') else - name = line:gsub('<br/><a rel.*$', '') - name = name:match('[^>]+$') + 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 + if line:find("^%[S%d%dE%d%d%]$") then -- it's a series line = iter() - if line:find('%.%.%.$') then - name = line:gsub('^.*title="', '') + if line:find("%.%.%.$") then + name = line:gsub('^.*title="', "") name = name:match('[^"]+') else - name = line:match('[^<]+') + name = line:match("[^<]+") end else -- no name @@ -58,33 +58,37 @@ local ids_fetch = function (page) return tab end -local search_ohash = function (ohash, name, lang) +local search_ohash = function(ohash, name, lang) local fetch, hcode, url, id - url = domain .. '/en' .. '/search/sublanguageid-' .. lang .. - '/moviehash-' .. ohash + url = domain + .. "/en" + .. "/search/sublanguageid-" + .. lang + .. "/moviehash-" + .. ohash fetch, hcode = curl.get(url, nil, nil) id = attr.fuzzy(name, ids_fetch(fetch)) if hcode and not id then - util.error('opensubtitles: search_ohash failed') + util.error("opensubtitles: search_ohash failed") end if id then - return domain .. '/en/subtitleserve/sub/' .. id + return domain .. "/en/subtitleserve/sub/" .. id end end -local search_filesize = function (filesize, name, lang) +local search_filesize = function(filesize, name, lang) local fetch, hcode, url, id, a a = attr.build(name) - url = domain .. '/en' .. '/search/sublanguageid-' .. lang + url = domain .. "/en" .. "/search/sublanguageid-" .. lang if a.season and a.episode then - url = url .. '/season-' .. a.season .. '/episode-' .. a.episode + url = url .. "/season-" .. a.season .. "/episode-" .. a.episode end - url = url .. '/moviebytesize-' .. filesize + url = url .. "/moviebytesize-" .. filesize fetch, hcode = curl.get(url, nil, nil) if not hcode then @@ -93,34 +97,34 @@ local search_filesize = function (filesize, name, lang) id = attr.fuzzy(name, ids_fetch(fetch)) if hcode and not id then - util.error('opensubtitles: search_filesize failed') + util.error("opensubtitles: search_filesize failed") end if id then - return domain .. '/en/subtitleserve/sub/' .. id + return domain .. "/en/subtitleserve/sub/" .. id end end -local url_encode = function (str) +local url_encode = function(str) local url = "" - for c in str:gmatch('.') do - if c:find('%w') then + for c in str:gmatch(".") do + if c:find("%w") then url = url .. c else - url = url .. '%' .. - string.format('%x', c:byte()):upper() + url = url .. "%" .. string.format("%x", c:byte()):upper() end end return url end -local suggest_id = function (name) +local suggest_id = function(name) local url, fetch, hcode, id - url = domain .. '/libs/suggest.php?format=json3&MovieName=' .. - url_encode(name) + url = domain + .. "/libs/suggest.php?format=json3&MovieName=" + .. url_encode(name) fetch, hcode = curl.get(url, nil, nil) if not hcode then @@ -132,10 +136,10 @@ local suggest_id = function (name) return nil end - return id:match('%d+') + return id:match("%d+") end -local search_name = function (name, lang) +local search_name = function(name, lang) local fetch, hcode, movie_id, url, id movie_id = suggest_id(name) @@ -143,8 +147,11 @@ local search_name = function (name, lang) return nil end - url = domain .. '/en/search/sublanguageid-' .. lang .. - '/idmovie-' .. movie_id + url = domain + .. "/en/search/sublanguageid-" + .. lang + .. "/idmovie-" + .. movie_id fetch, hcode = curl.get(url, nil, nil) if not hcode then return nil @@ -152,18 +159,18 @@ local search_name = function (name, lang) id = attr.fuzzy(name, ids_fetch(fetch)) if not id then - util.error('opensubtitles: search_name failed') + util.error("opensubtitles: search_name failed") end if id then - return domain .. '/en/subtitleserve/sub/' .. id + return domain .. "/en/subtitleserve/sub/" .. id end end -local search = function (path, out, info) +local search = function(path, out, info) local ohash, link, name, lang - lang = info.iso639_2_lang or 'eng' + lang = info.iso639_2_lang or "eng" name = info.name or util.string_vid_path_to_name(path) if util.file_exists(path) then @@ -188,5 +195,5 @@ return { search_ohash = search_ohash, search_filesize = search_filesize, search_name = search_name, - search = search + search = search, } diff --git a/stylua.toml b/stylua.toml new file mode 100644 index 0000000..da5e588 --- /dev/null +++ b/stylua.toml @@ -0,0 +1,4 @@ +column_width = 80 + +[sort_requires] +enabled = true @@ -1,35 +1,34 @@ #!/usr/bin/env lua -local opensubtitles = require 'server.opensubtitles' +local opensubtitles = require("server.opensubtitles") -local note = function (success, name) - local sign +local note = function(success, name) + local sign - if success then - sign = '✅' - else - sign = '❌' - end + if success then + sign = "✅" + else + sign = "❌" + end - print(sign .. ' : ' .. name) + print(sign .. " : " .. name) end +local test_opensubtitles = function() + local ohash, name, id -local test_opensubtitles = function () - local ohash, name, id + ohash = "395787dbe5b42001" + name = "Fight.Club.1999.REMASTERED.720p.BRRip.XviD.AC3-RARBG" + id = opensubtitles.search_ohash(ohash, name, "eng") + note(id and id:match("%d+$") == "5449593", "search_ohash") - ohash = '395787dbe5b42001' - name = 'Fight.Club.1999.REMASTERED.720p.BRRip.XviD.AC3-RARBG' - id = opensubtitles.search_ohash(ohash, name, 'eng') - note(id and id:match('%d+$') == '5449593', 'search_ohash') + name = "Fight.Club.10th.Anniversary.Edition.1999.720p.BrRip.x264.YIFY" + id = opensubtitles.search_filesize(1074575924, name, "eng") + note(id and id:match("%d+$") == "4987774", "search_filesize") - name = 'Fight.Club.10th.Anniversary.Edition.1999.720p.BrRip.x264.YIFY' - id = opensubtitles.search_filesize(1074575924, name, 'eng') - note(id and id:match('%d+$') == '4987774', 'search_filesize') - - name = 'Fight.Club.1999.REMASTERED.720p.BRRip.XviD.AC3-RARBG' - id = opensubtitles.search_name(name, 'eng') - note(id and id:match('%d+$') == '5449593', 'search_name') + name = "Fight.Club.1999.REMASTERED.720p.BRRip.XviD.AC3-RARBG" + id = opensubtitles.search_name(name, "eng") + note(id and id:match("%d+$") == "5449593", "search_name") end test_opensubtitles() |