From 037d0e9bf37f346073b9010e4353dce549eba40f Mon Sep 17 00:00:00 2001 From: vapormusic Date: Thu, 22 Oct 2020 13:17:44 +0700 Subject: [PATCH] next/prev chap v1.0 --- main.lua | 210 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 173 insertions(+), 37 deletions(-) diff --git a/main.lua b/main.lua index 1201118..c47efda 100644 --- a/main.lua +++ b/main.lua @@ -61,14 +61,80 @@ function WLNReader:addToMainMenu(menu_items) }, { text_func = function() - return _("Bye") + return _("Next chapter") end, callback = function() - UIManager:show(InfoMessage:new{ - text = _("Hello, plugin world"), - }) + local ReaderUI = require("apps/reader/readerui") + last_dir, last_file = ReaderUI:getLastDirFile(true) + print(last_dir) + print(last_file) + if string.find(last_dir,"wlnreader",1,true) ~= nil then + + firstsidindex =string.find(last_dir,'.(.',1,true) + finalsidindex =string.find(last_dir,'.)',1,true) + + filen = string.sub(last_file,#last_file - #last_dir) + firstsidindexu=string.find(filen,'.(.',1,true) + finalsidindexu =string.find(filen,'.)',1,true) + print(firstsidindexu) + if (firstsidindex ~= nil and finalsidindex ~= nil and firstsidindexu ~= nil and finalsidindexu ~= nil ) then + print(string.sub(last_dir,firstsidindex+3,finalsidindex-1)) + print(string.sub(filen,firstsidindexu+3,finalsidindexu-1)) + nsid = string.sub(last_dir,firstsidindex+3,finalsidindex-1) + ncv = string.sub(filen,firstsidindexu+3,finalsidindexu-1) + nstartvol = string.sub(ncv,1,string.find(ncv,"-",1,true)-1) + nstartchap = string.sub(ncv,string.find(ncv,"-",1,true)+1) + print(nsid) + print(nstartvol) + print(nstartchap) + WLNReader:searchDetail(nsid,"next",nstartvol,nstartchap) end - } + end + -- UIManager:show(InfoMessage:new{ + -- text = _("Hello, plugin world"), + -- timeout = 3 + -- }) + + end + }, + { + text_func = function() + return _("Previous chapter") + end, + callback = function() + local ReaderUI = require("apps/reader/readerui") + last_dir, last_file = ReaderUI:getLastDirFile(true) + print(last_dir) + print(last_file) + if string.find(last_dir,"wlnreader",1,true) ~= nil then + + firstsidindex =string.find(last_dir,'.(.',1,true) + finalsidindex =string.find(last_dir,'.)',1,true) + + filen = string.sub(last_file,#last_file - #last_dir) + firstsidindexu=string.find(filen,'.(.',1,true) + finalsidindexu =string.find(filen,'.)',1,true) + print(firstsidindexu) + if (firstsidindex ~= nil and finalsidindex ~= nil and firstsidindexu ~= nil and finalsidindexu ~= nil ) then + print(string.sub(last_dir,firstsidindex+3,finalsidindex-1)) + print(string.sub(filen,firstsidindexu+3,finalsidindexu-1)) + nsid = string.sub(last_dir,firstsidindex+3,finalsidindex-1) + ncv = string.sub(filen,firstsidindexu+3,finalsidindexu-1) + nstartvol = string.sub(ncv,1,string.find(ncv,"-",1,true)-1) + nstartchap = string.sub(ncv,string.find(ncv,"-",1,true)+1) + print(nsid) + print(nstartvol) + print(nstartchap) + WLNReader:searchDetail(nsid,"prev",nstartvol,nstartchap) + end + end + -- UIManager:show(InfoMessage:new{ + -- text = _("Hello, plugin world"), + -- timeout = 3 + -- }) + + end + }, } } end @@ -156,7 +222,7 @@ function WLNReader:printSearchResult(title) table.insert(WLNReader.path,title) print(WLNReader.path[2]) print("passed") - WLNReader:searchDetail(responses.data.results[i].sid) + WLNReader:searchDetail(responses.data.results[i].sid,"","","") UIManager:close(WLNSearch) end print(temp.text) @@ -180,20 +246,21 @@ function WLNReader:printSearchResult(title) else UIManager:show(InfoMessage:new{ text = _("No novels found!"), - timeout = 5, + timeout = 3, }) WLNReader: searchInput() end return responses.id end -function WLNReader:searchDetail(sid) +function WLNReader:searchDetail(sid,mode,startvol,startchap) -- get response from site -- local request_body = '{"mode" : "search-advanced","title-search-text" : "' .. title .. '"}' + targetchap = "" local request_body = '{"id": "'.. sid ..'", "mode": "get-series-id"}' local url = "https://www.wlnupdates.com/api" local responses = self:_makeJsonRequest(url, "POST", request_body) - print("series id: ".. responses.data.releases[1].srcurl) + --print("series id: ".. responses.data.releases[1].srcurl) --- print results if #responses.data.releases > 0 then local WLNSearch2 = Menu:new{ @@ -206,20 +273,26 @@ function WLNReader:searchDetail(sid) self.results = {} --print(type(responses.data.releases[6].chapter)) --print(type(responses.data.releases[27].chapter)) + bruhx = #responses.data.releases print(#responses.data.releases) - + targetx = 1 if (#responses.data.releases > 15 ) then k =#responses.data.releases else k = #responses.data.releases end for i=1, k do temp = {} + - if type(responses.data.releases[i].volume) ~= "number" then vol = "" else vol = responses.data.releases[i].volume - end + if type(responses.data.releases[i].volume) ~= "number" then vol = "" else vol = responses.data.releases[i].volume + end if type(responses.data.releases[i].chapter) ~= "number" then chap = "" else chap = responses.data.releases[i].chapter end - + if (#mode > 0 and (vol == startvol or vol == tonumber(startvol)) and (chap == startchap or chap == tonumber(startchap))) then + targetx = i + print("found:"..targetx) + end temp.text = "Volume ".. vol .. ", Chapter " .. chap temp.name = nil temp.callback = function() + print("ok") if type(responses.data.releases[i].volume) ~= "number" then vol1 = "" else vol1 = responses.data.releases[i].volume end if type(responses.data.releases[i].chapter) ~= "number" then chap1 = "" else chap1 = responses.data.releases[i].chapter end @@ -239,17 +312,20 @@ function WLNReader:searchDetail(sid) qstart = string.find(cropped,'', qstart, true) --print(qstart) - + if (qstart ~= nil and qend ~= nil) then truelink = string.sub(cropped,qstart + #lenx,qend-1) - print(truelink) + --print(truelink) if (truelink ~= nil and #truelink > 5) then responses.data.releases[i].srcurl = string.sub(cropped,qstart + #lenx,qend-1) end + end end end -- print(string.find(responsesu,"entry-content", 1, true)) -- print(string.find(responsesu,"entry-footer", 1, true)) - WLNReader:downloadEbook(responses.data.releases[i].srcurl,tempname) + --print(responses.data.title) + WLNReader:downloadEbook(responses.data.releases[i].srcurl,tempname,responses.data.title,sid,vol1 .."-"..chap1) + end print(temp.text) table.insert(self.results, temp) @@ -258,7 +334,55 @@ function WLNReader:searchDetail(sid) local items = #self.results WLNSearch2.paths = {"1"} WLNSearch2:switchItemTable("Results", self.results , items, nil) + if #mode == 0 then UIManager:show(WLNSearch2) + else + print("quick mode") + + if (mode == "next" and targetx > 1) then + targetx = targetx - 1 + end + if (mode == "prev" and targetx < bruhx) then + targetx = targetx + 1 + end + print(targetx) + + print(#responses.data.releases) + print(type(responses.data.releases[targetx].volume) ) + if type(responses.data.releases[targetx].volume) ~= "number" then vol1 = "" else vol1 = responses.data.releases[targetx].volume end + if type(responses.data.releases[targetx].chapter) ~= "number" then chap1 = "" else chap1 = responses.data.releases[targetx].chapter end + + tempname = responses.data.title .." - ".. "Volume ".. vol1 .. ", Chapter " .. chap1 + responsesu = self:_makeHtmlRequest(responses.data.releases[targetx].srcurl, "GET", "") + print(#responsesu) + qstart = string.find(responsesu,"entry-content", 1, true) + qend = string.find(responsesu,"entry-footer", 1, true) + print(qstart) + print(qend) + if (qstart ~= nil and qend ~= nil) then + cropped = string.sub(responsesu,qstart,qend) + if #cropped < 17000 then + -- sus + lenx = '', qstart, true) + --print(cropped) + if (qstart ~= nil and qend ~= nil) then + truelink = string.sub(cropped,qstart + #lenx,qend-1) + --print(truelink) + if (truelink ~= nil and #truelink > 5) then + responses.data.releases[targetx].srcurl = string.sub(cropped,qstart + #lenx,qend-1) + end + end + end + end + -- print(string.find(responsesu,"entry-content", 1, true)) + -- print(string.find(responsesu,"entry-footer", 1, true)) + --print(responses.data.title) + WLNReader:downloadEbook(responses.data.releases[targetx].srcurl,tempname,responses.data.title,sid,vol1 .."-"..chap1) + + end + function WLNSearch2:onReturn() print(#WLNReader.path) local x = WLNReader.path[1] @@ -274,7 +398,7 @@ function WLNReader:searchDetail(sid) else UIManager:show(InfoMessage:new{ text = _("No chapters"), - timeout = 5, + timeout = 3, }) WLNReader:printSearchResult(WLNReader.path[2]) end @@ -283,7 +407,7 @@ end -function WLNReader:downloadEbook(url,name) +function WLNReader:downloadEbook(url,name,title,sid,vc) local request_body = '{"title": "'.. name ..'", "urls": ["' .. url .. '"]}' print(request_body) local url2 = "https://epub.press/api/v1/books" @@ -292,23 +416,23 @@ print(responses.id) download_id = responses.id fulllink ="https://epub.press/api/v1/books/".. download_id .."/download?filetype=epub" --DownloadBackend:download(fulllink, Device.home_dir .. "/wlnreader/"..name..".pdf") -WLNReader:_makeRequestGET("https://epub.press/api/v1/books/".. download_id .."/download", Device.home_dir .. "/wlnreader/"..name..".epub") +WLNReader:_makeRequestGET("https://epub.press/api/v1/books/".. download_id .."/download", Device.home_dir .. "/wlnreader/"..title.." .(."..sid..".)/",name..".(."..vc..".).epub") end -function WLNReader:_makeRequestGET(url, fname) - local sink = {} - https.request{ - url = url, - method = "GET", - header = { ["Host"] = "epub.press" }, - sink = ltn12.sink.table(sink) - } - - if not sink[1] then - error("No response from WLN Server") - end - -- print log from response body - print(table.concat(sink)) +function WLNReader:_makeRequestGET(url, folder,fname) + -- local sink = {} + -- https.request{ + -- url = url, + -- method = "GET", + -- header = { ["Host"] = "epub.press" }, + -- sink = ltn12.sink.table(sink) + -- } + + -- if not sink[1] then + -- error("No response from WLN Server") + -- end + -- -- print log from response body + -- print(table.concat(sink)) for i=1,10 do local data = "" @@ -336,13 +460,19 @@ function WLNReader:_makeRequestGET(url, fname) print(type(data)) print(#data) if #data > 1000 then - io.output(fname) + if not(FolderExists(Device.home_dir .. "/wlnreader/")) then + lfs.mkdir(Device.home_dir .. "/wlnreader/") + end + if not(FolderExists(folder)) then + lfs.mkdir(folder) + end + io.output(folder .. fname) io.write(data) io.close() print("file downloaded") local ReaderUI = require("apps/reader/readerui") - ReaderUI:showReader(fname) + ReaderUI:showReader(folder .. fname) break end end @@ -364,7 +494,7 @@ function WLNReader:searchInput() { text = _("Cancel"), callback = function() - UIManager:close(search_server_dialog) + UIManager:close(self.search_server_dialog) end, }, { @@ -382,7 +512,13 @@ function WLNReader:searchInput() self.search_server_dialog:onShowKeyboard() end - +function FolderExists(strFolderName) + if lfs.attributes(strFolderName:gsub("\\$",""),"mode") == "directory" then + return true + else + return false + end +end return WLNReader