mirror of https://github.com/koreader/koreader
NewsDownloader externalize download engine (#3618)
parent
b0f0e839fb
commit
d0f9b53eb3
@ -0,0 +1,24 @@
|
||||
local http = require("socket.http")
|
||||
local https = require("ssl.https")
|
||||
local logger = require("logger")
|
||||
local ltn12 = require("ltn12")
|
||||
local socket_url = require("socket.url")
|
||||
|
||||
local InternalDownloadBackend = {}
|
||||
|
||||
function InternalDownloadBackend:getResponseAsString(url)
|
||||
local resp_lines = {}
|
||||
local parsed = socket_url.parse(url)
|
||||
local httpRequest = parsed.scheme == 'http' and http.request or https.request
|
||||
httpRequest({ url = url, sink = ltn12.sink.table(resp_lines), })
|
||||
return table.concat(resp_lines)
|
||||
end
|
||||
|
||||
function InternalDownloadBackend:download(link, path)
|
||||
logger.dbg("InternalDownloadBackend: News file will be stored to :", path)
|
||||
local parsed = socket_url.parse(link)
|
||||
local httpRequest = parsed.scheme == 'http' and http.request or https.request
|
||||
httpRequest({ url = link, sink = ltn12.sink.file(io.open(path, "w")), })
|
||||
end
|
||||
|
||||
return InternalDownloadBackend
|
@ -0,0 +1,20 @@
|
||||
local logger = require("logger")
|
||||
local http_request = require "http.request"
|
||||
|
||||
-- Currently unused. TODO @mwoz123 ADD LUA-HTTP AS LIBRARY
|
||||
local LuaHttpDownloadBackend = {}
|
||||
|
||||
function LuaHttpDownloadBackend:getResponseAsString(url)
|
||||
local _, stream = assert(http_request.new_from_uri(url):go())
|
||||
local body = assert(stream:get_body_as_string())
|
||||
logger.dbg("Response body :", body)
|
||||
return body
|
||||
end
|
||||
|
||||
function LuaHttpDownloadBackend:download(link, path)
|
||||
local _, stream = assert(http_request.new_from_uri(link):go())
|
||||
stream:save_body_to_file(path)
|
||||
stream:shutdown()
|
||||
end
|
||||
|
||||
return LuaHttpDownloadBackend
|
Loading…
Reference in New Issue