forked from metafates/mangal-scrapers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComicK.lua
155 lines (116 loc) Β· 4 KB
/
ComicK.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
------------------------------
-- @name ComicK
-- @url https://comick.fun
-- @author Sravan Balaji
-- @license MIT
------------------------------
----- IMPORTS -----
Http = require('http')
Json = require('json')
--- END IMPORTS ---
----- VARIABLES -----
Client = Http.client({ timeout = 20 })
ApiBase = 'https://api.comick.fun'
ImageBase = 'https://meo3.comick.pictures'
Limit = 50
Lang = 'en' -- Language: en = english, fr = french, etc.
Order = 1 -- Chapter Order: 0 = descending, 1 = ascending
--- 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 request_url = ApiBase .. '/search?&q=' .. query
local request = Http.request('GET', request_url)
local result = Client:do_request(request)
local result_body = Json.decode(result['body'])
local mangas = {}
local i = 1
for _, val in pairs(result_body) do
local title = val['title']
if title ~= nil then
local id = val['id']
local link = ApiBase .. '/comic/' .. tostring(id) .. '/chapter'
local manga = { url = link, name = title }
mangas[i] = manga
i = i + 1
end
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)
local request_url = mangaURL .. '?lang=' .. Lang .. '&limit=' .. Limit .. '&chap-order=' .. Order
local chapters = {}
local i = 1
local request = Http.request('GET', request_url)
local result = Client:do_request(request)
local result_body = Json.decode(result['body'])
local num_chapters = result_body['total']
local num_pages = math.ceil(num_chapters / Limit)
for j = 1, num_pages do
request = Http.request('GET', request_url .. '&page=' .. j)
result = Client:do_request(request)
result_body = Json.decode(result['body'])
for _, val in pairs(result_body['chapters']) do
local hid = val['hid']
local num = val['chap']
if num == nil then
num = 0
end
local volume = tostring(val['vol'])
if volume ~= "nil" then
volume = "Vol." .. volume
else
volume = ""
end
local title = val['title']
local chap = 'Chapter ' .. tostring(num)
local group_name = val['group_name']
if title then
chap = chap .. ': ' .. tostring(title)
end
if group_name then
chap = chap .. ' ['
for key, group in pairs(group_name) do
if key ~= 1 then
chap = chap .. ', '
end
chap = chap .. tostring(group)
end
chap = chap .. ']'
end
local link = ApiBase .. '/chapter/' .. tostring(hid)
local chapter = { url = link, name = chap, volume = volume }
chapters[i] = chapter
i = i + 1
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)
local request = Http.request('GET', chapterURL)
local result = Client:do_request(request)
local result_body = Json.decode(result['body'])
local chapter_table = result_body['chapter']
local pages = {}
local i = 1
for key, val in pairs(chapter_table['md_images']) do
local ind = key
local link = ImageBase .. '/' .. val['b2key']
local page = { url = link, index = ind }
pages[i] = page
i = i + 1
end
return pages
end
--- END MAIN ---
----- HELPERS -----
--- END HELPERS ---
-- ex: ts=4 sw=4 et filetype=lua