mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
Fix Wikipedia (HTTP is now HTTPS) and some failsafes
Rudimentary JSON validity check by seeing if the first character is {. The JSON decode function will crash in spite of pcall if it's not.
This commit is contained in:
@@ -62,8 +62,13 @@ function Translator:loadPage(target_lang, source_lang, text)
|
||||
error("Network is unreachable")
|
||||
end
|
||||
|
||||
if status ~= "HTTP/1.1 200 OK" then
|
||||
DEBUG("HTTP status not okay:", status)
|
||||
return
|
||||
end
|
||||
|
||||
local content = table.concat(sink)
|
||||
if content ~= "" then
|
||||
if content ~= "" and string.sub(content, 1,1) == "{" then
|
||||
local ok, result = pcall(JSON.decode, content)
|
||||
if ok and result then
|
||||
--DEBUG("translate result", result)
|
||||
@@ -71,6 +76,8 @@ function Translator:loadPage(target_lang, source_lang, text)
|
||||
else
|
||||
DEBUG("error:", result)
|
||||
end
|
||||
else
|
||||
DEBUG("not JSON:", content)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ local DEBUG = require("dbg")
|
||||
--]]
|
||||
|
||||
local Wikipedia = {
|
||||
wiki_server = "http://%s.wikipedia.org",
|
||||
wiki_server = "https://%s.wikipedia.org",
|
||||
wiki_path = "/w/api.php",
|
||||
wiki_params = {
|
||||
action = "query",
|
||||
@@ -59,8 +59,13 @@ function Wikipedia:loadPage(text, lang, intro, plain)
|
||||
error("Network is unreachable")
|
||||
end
|
||||
|
||||
if status ~= "HTTP/1.1 200 OK" then
|
||||
DEBUG("HTTP status not okay:", status)
|
||||
return
|
||||
end
|
||||
|
||||
local content = table.concat(sink)
|
||||
if content ~= "" then
|
||||
if content ~= "" and string.sub(content, 1,1) == "{" then
|
||||
local ok, result = pcall(JSON.decode, content)
|
||||
if ok and result then
|
||||
DEBUG("wiki result", result)
|
||||
@@ -68,6 +73,8 @@ function Wikipedia:loadPage(text, lang, intro, plain)
|
||||
else
|
||||
DEBUG("error:", result)
|
||||
end
|
||||
else
|
||||
DEBUG("not JSON:", content)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user