2
0
mirror of https://github.com/koreader/koreader synced 2024-11-10 01:10:34 +00:00

NewsDownloader: Add flag 'include_images' to feed config

Allow the user to specify whether to download images for each individual
feed specified in feed_config.lua. Default to false to stay closest to
existing behaviour.
This commit is contained in:
Tom Hall 2019-04-17 21:26:56 +01:00 committed by Frans de Jonge
parent 1588d4c64a
commit cad4d25d24
3 changed files with 20 additions and 17 deletions

View File

@ -20,10 +20,10 @@ local EpubDownloadBackend = {
} }
local max_redirects = 5; --prevent infinite redirects local max_redirects = 5; --prevent infinite redirects
function EpubDownloadBackend:download(url, path) function EpubDownloadBackend:download(url, path, include_images)
logger.dbg("EpubDownloadBackend:download") logger.dbg("EpubDownloadBackend:download")
-- self:createEpub(path, url) -- self:createEpub(path, url)
self:createEpubWithUI(path, url, function(success) self:createEpubWithUI(path, url, include_images, function(success)
if (success) then if (success) then
logger.dbg("createEpubWithUI success") logger.dbg("createEpubWithUI success")
else else
@ -189,9 +189,8 @@ local ext_to_mimetype = {
} }
-- Create an epub file (with possibly images) -- Create an epub file (with possibly images)
function EpubDownloadBackend:createEpub(epub_path, url) function EpubDownloadBackend:createEpub(epub_path, url, include_images)
logger.dbg("EpubDownloadBackend:createEpub(", epub_path, ",", url, ")") logger.dbg("EpubDownloadBackend:createEpub(", epub_path, ",", url, ")")
local with_images = true
-- Use Trapper to display progress and ask questions through the UI. -- Use Trapper to display progress and ask questions through the UI.
-- We need to have been Trapper.wrap()'ed for UI to be used, otherwise -- We need to have been Trapper.wrap()'ed for UI to be used, otherwise
-- Trapper:info() and Trapper:confirm() will just use logger. -- Trapper:info() and Trapper:confirm() will just use logger.
@ -298,7 +297,6 @@ function EpubDownloadBackend:createEpub(epub_path, url)
logger.dbg("Images found in html:", images) logger.dbg("Images found in html:", images)
-- See what to do with images -- See what to do with images
local include_images = true
local use_img_2x = false local use_img_2x = false
if not include_images then if not include_images then
-- Remove img tags to avoid little blank squares of missing images -- Remove img tags to avoid little blank squares of missing images
@ -532,7 +530,7 @@ end
-- Wrap EpubDownloadBackend:createEpub() with UI progress info, provided -- Wrap EpubDownloadBackend:createEpub() with UI progress info, provided
-- by Trapper module. -- by Trapper module.
function EpubDownloadBackend:createEpubWithUI(epub_path, url, result_callback) function EpubDownloadBackend:createEpubWithUI(epub_path, url, include_images, result_callback)
logger.dbg("EpubDownloadBackend:createEpubWithUI(", epub_path, ",", url, ",", title, ", ...)") logger.dbg("EpubDownloadBackend:createEpubWithUI(", epub_path, ",", url, ",", title, ", ...)")
-- To do any UI interaction while building the EPUB, we need -- To do any UI interaction while building the EPUB, we need
-- to use a coroutine, so that our code can be suspended while waiting -- to use a coroutine, so that our code can be suspended while waiting
@ -545,7 +543,7 @@ function EpubDownloadBackend:createEpubWithUI(epub_path, url, result_callback)
-- Trapper) would just abort (no reader crash, no error logged). -- Trapper) would just abort (no reader crash, no error logged).
-- So we use pcall to catch any errors, log it, and report -- So we use pcall to catch any errors, log it, and report
-- the failure via result_callback. -- the failure via result_callback.
local ok, success = pcall(self.createEpub, self, epub_path, url) local ok, success = pcall(self.createEpub, self, epub_path, url, include_images)
if ok and success then if ok and success then
result_callback(true) result_callback(true)
else else

View File

@ -17,6 +17,10 @@ return {--do NOT change this line
-- 'download_full_article=false' - means use only feed description to create feeds (usually only beginning of the article) -- 'download_full_article=false' - means use only feed description to create feeds (usually only beginning of the article)
-- default value is 'true' (if no 'download_full_article' entry) -- default value is 'true' (if no 'download_full_article' entry)
-- 'include_images=true' - means download any images on the page and inlude them in the article
-- 'include_images=false' - means ignore any images, only download the text (faster download, smaller file sizes)
-- default value is 'false' (if no 'include_images' entry)
-- comment out line ("--" at line start) to stop downloading source -- comment out line ("--" at line start) to stop downloading source

View File

@ -181,12 +181,13 @@ function NewsDownloader:loadConfigAndProcessFeeds()
local url = feed[1] local url = feed[1]
local limit = feed.limit local limit = feed.limit
local download_full_article = feed.download_full_article == nil or feed.download_full_article local download_full_article = feed.download_full_article == nil or feed.download_full_article
local include_images = feed.include_images
if url and limit then if url and limit then
info = InfoMessage:new{ text = T(_("Processing %1/%2:\n%3"), idx, total_feed_entries, url) } info = InfoMessage:new{ text = T(_("Processing %1/%2:\n%3"), idx, total_feed_entries, url) }
UIManager:show(info) UIManager:show(info)
-- processFeedSource is a blocking call, so manually force a UI refresh beforehand -- processFeedSource is a blocking call, so manually force a UI refresh beforehand
UIManager:forceRePaint() UIManager:forceRePaint()
NewsDownloader:processFeedSource(url, tonumber(limit), unsupported_feeds_urls, download_full_article) NewsDownloader:processFeedSource(url, tonumber(limit), unsupported_feeds_urls, download_full_article, include_images)
UIManager:close(info) UIManager:close(info)
else else
logger.warn('NewsDownloader: invalid feed config entry', feed) logger.warn('NewsDownloader: invalid feed config entry', feed)
@ -213,7 +214,7 @@ function NewsDownloader:loadConfigAndProcessFeeds()
NewsDownloader:afterWifiAction() NewsDownloader:afterWifiAction()
end end
function NewsDownloader:processFeedSource(url, limit, unsupported_feeds_urls, download_full_article) function NewsDownloader:processFeedSource(url, limit, unsupported_feeds_urls, download_full_article, include_images)
local ok, response = pcall(function() local ok, response = pcall(function()
return DownloadBackend:getResponseAsString(url) return DownloadBackend:getResponseAsString(url)
@ -233,11 +234,11 @@ function NewsDownloader:processFeedSource(url, limit, unsupported_feeds_urls, do
if is_atom then if is_atom then
ok = pcall(function() ok = pcall(function()
return self:processAtom(feeds, limit, download_full_article) return self:processAtom(feeds, limit, download_full_article, include_images)
end) end)
elseif is_rss then elseif is_rss then
ok = pcall(function() ok = pcall(function()
return self:processRSS(feeds, limit, download_full_article) return self:processRSS(feeds, limit, download_full_article, include_images)
end) end)
end end
if not ok or (not is_rss and not is_atom) then if not ok or (not is_rss and not is_atom) then
@ -263,7 +264,7 @@ function NewsDownloader:deserializeXMLString(xml_str)
return xmlhandler.root return xmlhandler.root
end end
function NewsDownloader:processAtom(feeds, limit, download_full_article) function NewsDownloader:processAtom(feeds, limit, download_full_article, include_images)
local feed_output_dir = string.format("%s%s/", local feed_output_dir = string.format("%s%s/",
news_download_dir_path, news_download_dir_path,
util.replaceInvalidChars(getFeedTitle(feeds.feed.title))) util.replaceInvalidChars(getFeedTitle(feeds.feed.title)))
@ -276,14 +277,14 @@ function NewsDownloader:processAtom(feeds, limit, download_full_article)
break break
end end
if download_full_article then if download_full_article then
self:downloadFeed(feed, feed_output_dir) self:downloadFeed(feed, feed_output_dir, include_images)
else else
self:createFromDescription(feed, feed.context, feed_output_dir) self:createFromDescription(feed, feed.context, feed_output_dir)
end end
end end
end end
function NewsDownloader:processRSS(feeds, limit, download_full_article) function NewsDownloader:processRSS(feeds, limit, download_full_article, include_images)
local feed_output_dir = ("%s%s/"):format( local feed_output_dir = ("%s%s/"):format(
news_download_dir_path, util.replaceInvalidChars(util.htmlEntitiesToUtf8(feeds.rss.channel.title))) news_download_dir_path, util.replaceInvalidChars(util.htmlEntitiesToUtf8(feeds.rss.channel.title)))
if not lfs.attributes(feed_output_dir, "mode") then if not lfs.attributes(feed_output_dir, "mode") then
@ -295,7 +296,7 @@ function NewsDownloader:processRSS(feeds, limit, download_full_article)
break break
end end
if download_full_article then if download_full_article then
self:downloadFeed(feed, feed_output_dir) self:downloadFeed(feed, feed_output_dir, include_images)
else else
self:createFromDescription(feed, feed.description, feed_output_dir) self:createFromDescription(feed, feed.description, feed_output_dir)
end end
@ -322,14 +323,14 @@ local function getTitleWithDate(feed)
return title return title
end end
function NewsDownloader:downloadFeed(feed, feed_output_dir) function NewsDownloader:downloadFeed(feed, feed_output_dir, include_images)
local link = getFeedLink(feed.link) local link = getFeedLink(feed.link)
local news_dl_path = ("%s%s%s"):format(feed_output_dir, local news_dl_path = ("%s%s%s"):format(feed_output_dir,
getTitleWithDate(feed), getTitleWithDate(feed),
file_extension) file_extension)
logger.dbg("NewsDownloader: News file will be stored to :", news_dl_path) logger.dbg("NewsDownloader: News file will be stored to :", news_dl_path)
DownloadBackend:download(link, news_dl_path) DownloadBackend:download(link, news_dl_path, include_images)
end end
function NewsDownloader:createFromDescription(feed, context, feed_output_dir) function NewsDownloader:createFromDescription(feed, context, feed_output_dir)