-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathAsuraScans.lua
99 lines (67 loc) · 2.09 KB
/
AsuraScans.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
--------------------------------------
-- @name AsuraScans
-- @url https://www.asurascans.com
-- @author metafates
-- @license MIT
--------------------------------------
----- IMPORTS -----
Html = require("html")
Headless = require('headless')
Time = require("time")
--- END IMPORTS ---
----- VARIABLES -----
Browser = Headless.browser()
Page = Browser:page()
Base = "https://www.asurascans.com"
Delay = 1 -- seconds
--- END VARIABLES ---
----- MAIN -----
--- Searches for manga with given query.
-- @param query Query to search for
-- @return Table of tables with the following fields: name, url
function SearchManga(query)
local url = Base .. "/?s=" .. query
Page:navigate(url)
Time.sleep(Delay)
local mangas = {}
for i, v in ipairs(Page:elements(".bsx > a")) do
local manga = { url = v:attribute('href'), name = v:attribute('title') }
mangas[i + 1] = manga
end
return mangas
end
--- Gets the list of all manga chapters.
-- @param mangaURL URL of the manga
-- @return Table of tables with the following fields: name, url
function MangaChapters(mangaURL)
Page:navigate(mangaURL)
Time.sleep(Delay)
local chapters = {}
for _, v in ipairs(Page:elements("#chapterlist > ul li")) do
local n = tonumber(v:attribute("data-num"))
local elem = Html.parse(v:html())
local link = elem:find("a"):first()
local chapter = { url = link:attr("href"), name = link:find("span"):first():text() }
if n ~= nil then
chapters[n] = chapter
end
end
return chapters
end
--- Gets the list of all pages of a chapter.
-- @param chapterURL URL of the chapter
-- @return Table of tables with the following fields: url, index
function ChapterPages(chapterURL)
Page:navigate(chapterURL)
Time.sleep(Delay)
local pages = {}
for i, v in ipairs(Page:elements("#readerarea p img")) do
local p = { index = i, url = v:attribute("src") }
pages[i + 1] = p
end
return pages
end
--- END MAIN ---
----- HELPERS -----
--- END HELPERS ---
-- ex: ts=4 sw=4 et filetype=lua