Skip to content

Instantly share code, notes, and snippets.

@dagrha
Created February 3, 2024 21:10
Show Gist options
  • Save dagrha/f24f74ff8c304d5d4b2af6425da99012 to your computer and use it in GitHub Desktop.
Save dagrha/f24f74ff8c304d5d4b2af6425da99012 to your computer and use it in GitHub Desktop.
KOReader User Patch to add all ebooks found on device to the Favorites collection
-- KOReader user patch to add all ebooks found on device to the Favorites collection
function find_files(directory, file_extensions)
local result = {}
-- Helper function to recursively search for files
local function search_files(dir)
for file in io.popen('ls -1 "' .. dir .. '"'):lines() do
local path = dir .. '/' .. file
local file_extension = file:match("^.+(%..+)$")
if file_extension and file_extensions[file_extension] then
table.insert(result, path)
elseif file_extension == nil then
-- Check if the path is a directory and recursively search in it
if file ~= "." and file ~= ".." then
search_files(path)
end
end
end
end
-- Convert file_extensions list to a table
local extensions_table = {}
for _, extension in pairs(file_extensions) do
extensions_table[extension] = true
end
-- Call the helper function to search for files
search_files(directory)
return result
end
function add_ebooks_to_collection(files)
local collection_file = io.open("/mnt/us/koreader/settings/collection.lua", "w")
--local collection_file = io.open("./koreader/settings/collection.lua", "w")
collection_file:write("-- /mnt/us/koreader/settings/collection.lua" .. "\n" .. "return {" .. "\n" .. ' ["favorites"] = {' .. "\n")
local count
count = 0
for _, file in ipairs(files) do
count = count + 1
collection_file:write(' [' .. count .. '] = {' .. '\n')
collection_file:write(' ["file"] = "' .. file .. '",' .. "\n")
collection_file:write(' ["order"] = ' .. count .. ',' .. "\n")
collection_file:write(' },' .. '\n')
end
collection_file:write(" }," .. "\n")
collection_file:write("}" .. "\n")
collection_file:close()
end
-- Define the base folder on the ereader to start the search for ebooks
local directory_path = "/mnt/us"
--local directory_path = "."
-- Define file extensions to look for, each as a table entry
local file_extensions = {
[".epub"] = true,
[".pdf"] = true,
}
local files = find_files(directory_path, file_extensions)
if #files > 0 then
add_ebooks_to_collection(files)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment