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:
Frans de Jonge
2015-08-23 20:39:08 +02:00
parent 51e8dee425
commit ba994f41ab
2 changed files with 17 additions and 3 deletions

View File

@@ -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

View File

@@ -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