From c52ca29600ea5c73d7c0e5257c158c08005b0c8b Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Mon, 2 Oct 2023 11:58:20 +0530 Subject: lib/curl: preserve nil value of hcode hcode is nil because 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 --- lib/curl.lua | 5 ++++- server/opensubtitles.lua | 2 +- server/subscene.lua | 6 +++--- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/curl.lua b/lib/curl.lua index db501db..c8ae478 100644 --- a/lib/curl.lua +++ b/lib/curl.lua @@ -36,7 +36,10 @@ local get = function (url, headr, args) args = util.array_merge(args, head_to_args(headr)) fetch = util.run(args) - hcode = fetch:match('%d*$') or 000 + -- 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*$', '') return fetch, tonumber(hcode) diff --git a/server/opensubtitles.lua b/server/opensubtitles.lua index a9f2199..6e96703 100644 --- a/server/opensubtitles.lua +++ b/server/opensubtitles.lua @@ -122,7 +122,7 @@ local search_ohash = function (ohash) repeat fetch, hcode = curl.get(url, nil, nil) tries = tries + 1 - until hcode == 200 or tries > retries + until hcode == 200 or not hcode or tries > retries id = fetch:match('/en/subtitleserve/sub/[^\n]*\n[^\n]*iduser%-0') if id then diff --git a/server/subscene.lua b/server/subscene.lua index 304ffde..d6eaba6 100644 --- a/server/subscene.lua +++ b/server/subscene.lua @@ -99,7 +99,7 @@ local title_search = function (key) repeat fetch, hcode = curl.get(url, headr, args) tries = tries + 1 - until hcode == 200 or tries > retries + until hcode == 200 or not hcode or tries > retries title = fetch:match('href="/subtitles/[^"]*') if title then @@ -116,7 +116,7 @@ local id_fetch = function (title) repeat fetch, hcode = curl.get(title, headr, nil) tries = tries + 1 - until hcode == 200 or tries > retries + until hcode == 200 or not hcode or tries > retries tab = {} iter = fetch:gmatch('[^\n\r]+') @@ -149,7 +149,7 @@ local link_fetch = function (id) repeat fetch, hcode = curl.get(id, headr, nil) tries = tries + 1 - until hcode == 200 or tries > retries + until hcode == 200 or not hcode or tries > retries if hcode == 200 then link = domain .. fetch:match('/subtitles/[%l_-]*%-text/[^"]*') -- cgit v1.2.3