aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/curl.lua31
-rw-r--r--server/opensubtitles.lua12
-rw-r--r--server/subscene.lua32
3 files changed, 30 insertions, 45 deletions
diff --git a/lib/curl.lua b/lib/curl.lua
index 04ebf95..5272508 100644
--- a/lib/curl.lua
+++ b/lib/curl.lua
@@ -17,7 +17,7 @@ local head_to_args = function (t)
return args
end
-local get = function (url, headr, args)
+local get = function (url, headr, args, tries)
local fetch, hcode, def_args
def_args = {
@@ -35,28 +35,29 @@ local get = function (url, headr, args)
headr = util.table_merge(def_headr, headr)
args = util.array_merge(args, head_to_args(headr))
- fetch = util.run(args)
- -- hcode can be nil, it means curl was't able to fulfill the http request, either
- -- because curl package is broken or mpv killed it prematurely. we can exit
- -- out of retry loop early if hcode is nil since there's no point in retrying
- hcode = fetch:match('%d*$')
- fetch = fetch:gsub('%s*%d*$', '')
+ repeat
+ fetch = util.run(args)
+ -- hcode can be nil, it means curl was't able to fulfill the http request, either
+ -- because curl package is broken or mpv killed it prematurely. we can exit
+ -- out of retry loop early if hcode is nil since there's no point in retrying
+ hcode = fetch:match('%d*$')
+ tries = tries - 1
+ until hcode == '200' or not hcode or tries <= 0
+
+ fetch = fetch:gsub('%s*%d*$', '')
return fetch, tonumber(hcode)
end
-local zip_link_to_file = function (url, headr, out, retries)
- local tries, hcode, zip, rc
+local zip_link_to_file = function (url, headr, out, tries)
+ local hcode, zip, rc, args
- tries = 0
zip = os.tmpname()
+ args = { '-o'.. zip }
- repeat
- _, hcode = get(url, headr, { '-o'.. zip })
- tries = tries + 1
- until hcode == 200 or not hcode or tries > retries
- rc = (hcode == 200)
+ _, hcode = get(url, headr, args, tries)
+ rc = (hcode == 200)
if rc then
rc = util.zip_ext_first(zip, out)
end
diff --git a/server/opensubtitles.lua b/server/opensubtitles.lua
index e9caf7b..0bace8b 100644
--- a/server/opensubtitles.lua
+++ b/server/opensubtitles.lua
@@ -111,18 +111,14 @@ local languages = {
local language = 'english'
local domain = 'https://www.opensubtitles.org'
-local retries = 10
+local tries = 10
local search_ohash = function (ohash)
- local fetch, tries, hcode, url, id
+ local fetch, 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 not hcode or tries > retries
+ fetch, hcode = curl.get(url, nil, nil, tries)
id = fetch:match('/en/subtitleserve/sub/[^\n]*\n[^\n]*iduser%-0')
if id then
@@ -144,7 +140,7 @@ local search = function (path, out)
ohash = util.opensubtitles_hash(path)
link = search_ohash(ohash)
if link then
- return curl.zip_link_to_file(link, nil, out, retries)
+ return curl.zip_link_to_file(link, nil, out, tries)
end
end
diff --git a/server/subscene.lua b/server/subscene.lua
index 27fc1df..4c99e8a 100644
--- a/server/subscene.lua
+++ b/server/subscene.lua
@@ -87,21 +87,17 @@ local languages = {
local language = 'english'
local domain = 'https://subscene.com'
local headr = {['Cookie'] = 'LanguageFilter=' ..languages[language]}
-local retries = 10
+local tries = 10
local title_search = function (key)
- local url, args, fetch, hcode, tries, title, rc
+ local url, args, fetch, hcode, title, rc
url = domain .. '/subtitles/searchbytitle'
args = { '--data-raw', 'query=' .. key }
- tries = 0
- repeat
- fetch, hcode = curl.get(url, headr, args)
- tries = tries + 1
- until hcode == 200 or not hcode or tries > retries
-
+ fetch, hcode = curl.get(url, headr, args, tries)
title = fetch:match('href="/subtitles/[^"]*')
+
if title then
title = title:gsub('href="', domain)
end
@@ -115,13 +111,9 @@ local title_search = function (key)
end
local id_fetch = function (title)
- local tab, id, name, fetch, hcode, tries, iter, line
+ local tab, id, name, fetch, hcode, iter, line
- tries = 0
- repeat
- fetch, hcode = curl.get(title, headr, nil)
- tries = tries + 1
- until hcode == 200 or not hcode or tries > retries
+ fetch, hcode = curl.get(title, headr, nil, tries)
tab = {}
iter = fetch:gmatch('[^\n\r]+')
@@ -152,15 +144,11 @@ local id_fetch = function (title)
end
local link_fetch = function (id)
- local fetch, tries, hcode, link, rc
+ local fetch, hcode, link, rc
- tries = 0
- repeat
- fetch, hcode = curl.get(id, headr, nil)
- tries = tries + 1
- until hcode == 200 or not hcode or tries > retries
- rc = hcode == 200
+ fetch, hcode = curl.get(id, headr, nil, tries)
+ rc = (hcode == 200)
if rc then
link = domain .. fetch:match('/subtitles/[%l_-]*%-text/[^"]*')
end
@@ -192,7 +180,7 @@ local search = function (path, out)
return false
end
- rc = curl.zip_link_to_file(link, headr, out, retries)
+ rc = curl.zip_link_to_file(link, headr, out, tries)
if not rc then
return false
end