Manga search kinda works. Fixed issue with threaded functions

This commit is contained in:
Ryan Ward 2020-02-11 21:14:14 -05:00
parent 567908877a
commit 75316cf1da
6 changed files with 156 additions and 18 deletions

BIN
images/test.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View File

@ -23,9 +23,10 @@ function m.storeList(list)
end
end
m.azlist = titles
return titles
end
-- returns manga
m.getManga = function(title)
m.getManga = THREAD:newFunction(function(title)
local http = require("socket.http")
local manga = http.request(title.Link)
local tab = {}
@ -45,7 +46,7 @@ m.getManga = function(title)
end
end
return tab
end
end)
local queue = multi:newSystemThreadedJobQueue(16)
queue:doToAll(function()
multi,thread = require("multi"):init()
@ -70,20 +71,25 @@ m.getPages = function(manga,chapter)
local http = require("socket.http")
local tab = {}
local cc = 0
local done = false
local page = http.request(manga.Chapters[chapter].Link)
tab.pages = {page:match([[id="imgholder.-src="([^"]*)]])}
tab.nextChapter = "http://www.mangareader.net"..page:match([[Next Chapter:.-href="([^"]*)]])
local lastJob
local conn = queue.OnJobCompleted(function(jid,link)
table.insert(tab.pages,link)
cc=cc+1
if jid==lastJob then
done = true
end
end)
local count = 0
for link,page in page:gmatch([[<option value="([^"]*)">(%d*)</option>]]) do
queue:pushJob("getImage","http://www.mangareader.net"..link)
lastJob = queue:pushJob("getImage","http://www.mangareader.net"..link)
count = count + 1
end
thread.hold(function()
return count==#tab.pages
return done
end)
return tab
end

View File

@ -105,7 +105,6 @@ function multi:newSystemThreadedJobQueue(n)
thread.yield()
local dat = c.queueReturn:pop()
if dat then
print(dat)
c.OnJobCompleted:Fire(unpack(dat))
end
end

View File

@ -49,11 +49,13 @@ local THREAD_ID = 1
local OBJECT_ID = 0
function THREAD:newFunction(func,holup)
return function(...)
local t = multi:newSystemThread("SystemThreadedFunction",func,...)
local t = multi:newSystemThread("SystemThreadedFunction"..math.random(0,999999999),func,...)
return thread:newFunction(function()
return thread.hold(function()
if t.stab["returns"] then
return unpack(t.stab.returns)
local dat = t.stab.returns
t.stab.returns = nil
return unpack(dat)
end
end)
end,holup)()

View File

@ -1,23 +1,112 @@
local Set = require("set")
local mangaReader = require("manga")
local multi,thread = require("multi"):init()
local titles
multi:newThread(function()
titles = mangaReader.storeList(mangaReader.init())
end)
local scale = 2
local mangaSize = {
x=200/scale,
y=288/scale
}
function tprint (tbl, indent)
if not indent then indent = 0 end
for k, v in pairs(tbl) do
formatting = string.rep(" ", indent) .. k .. ": "
if type(v) == "table" then
print(formatting)
tprint(v, indent+1)
elseif type(v) == 'boolean' then
print(formatting .. tostring(v))
else
print(formatting .. v)
end
end
end
function searchFor(query)
query = Set(query:split(" "))
local list = {}
for i,v in pairs(titles) do
local t = Set(v.Title:split(" "))
local tab = {}
for k in Set.elements(query*t) do table.insert(tab,k) end
if #tab==Set.card(query) then
table.insert(list,v)
end
end
return list
end
function searchBy(char)
local list = {}
for i,v in pairs(titles) do
if v.Title:sub(1,1):lower()==char:sub(1,1):lower() or (char=="#" and tonumber(v.Title:sub(1,1))~=nil) then
table.insert(list,v)
end
end
return list
end
local chars = {"#"}
for i=65,90 do
table.insert(chars,string.char(i))
end
local function init(page,workspace)
local holder = page:newFrame("",15,80,-30,-95,0,0,1,1)
holder.Visibility = 0
holder.BorderSize = 0
page.ClipDescendants = true
holder:OnUpdate(function()
local c = holder:getChildren()
for i=1,#c do
local x,y = InGridX(i,holder.width,0,mangaSize.x+5,mangaSize.y+5)
c[i]:SetDualDim(x,y+5)
end
local size = math.floor(holder.width/(mangaSize.x+5))*(mangaSize.x+5)
holder:SetDualDim((page.width-size)/2)
end)
local nav = page:newFrame(10,10,-20,40,0,0,1)
local SBL = page:newFrame(0,55,540,20)
for i,v in pairs(chars) do
local temp = SBL:newTextLabel(v,v,(i-1)*20,0,20,20)
temp.Color = theme.button
temp:fitFont()
temp:OnReleased(thread:newFunction(function()
thread.hold(function() return titles end)
local list = searchBy(temp.text)
local c = holder:getChildren()
for i=#c,1,-1 do
c[i]:Destroy()
end
for i,v in pairs(list) do
thread.sleep(.5)
local manga = mangaReader.getManga(v).connect(function(manga)
print(v.Title,manga.Cover)
local temp = holder:newImageButton(nil,0,0,mangaSize.x,mangaSize.y)
temp.BorderSize = 2
temp:SetImage(manga.Cover)
end)
end
end))
end
--[[
local temp = holder:newImageLabel("images/test.jpg",0,0,mangaSize.x,mangaSize.y)
temp.BorderSize = 2
]]
SBL:centerX()
nav.Color = theme.header
nav:setRoundness(5,5,60)
local search = nav:newTextButton("Search","Search",5,5,60,-10,0,0,0,1)
search.Color = theme.button--Color.new("2196F3")
search.Color = theme.button
search:fitFont()
local func = thread:newFunction(function()
mangaReader.storeList(mangaReader.init())
local title = mangaReader.getList()[643]
local manga = mangaReader.getManga(title)
local page = mangaReader.getPages(manga,1)
end)
search:OnReleased(function()
func()
end)
local bar = nav:newTextBox("","",70,5,-75,-10,0,0,1,1)
search:OnReleased(thread:newFunction(function()
thread.hold(function() return titles end)
-- tprint(searchBy("a"))
-- tprint(searchFor(bar.text))
-- local manga = mangaReader.getManga(title)
-- local page = mangaReader.getPages(manga,1)
-- print(page)
end))
bar:fitFont()
bar.Color = theme.input
bar.XTween = 1

42
set.lua Normal file
View File

@ -0,0 +1,42 @@
local pairs, setmetatable = pairs, setmetatable
local mt -- metatable
mt = {
__add = function(s1, s2) -- union
local s = {}
for e in pairs(s1) do s[e] = true end
for e in pairs(s2) do s[e] = true end
return setmetatable(s, mt)
end,
__mul = function(s1, s2) -- intersection
local s = {}
for e in pairs(s1) do
if s2[e] then s[e] = true end
end
return setmetatable(s, mt)
end,
__sub = function(s1, s2) -- set difference
local s = {}
for e in pairs(s1) do
if not s2[e] then s[e] = true end
end
return setmetatable(s, mt)
end,
__tostring = function(s)
return table.concat(s,",")
end
}
local card = function(s) -- #elements
local n = 0
for k in pairs(s) do n = n + 1 end
return n
end
return setmetatable({elements = pairs, card = card}, {
__call = function(_, t) -- new set
local t = t or {}
local s = {}
for _, e in pairs(t) do s[e:lower()] = true end
return setmetatable(s, mt)
end
})