-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mangadex.py
417 lines (376 loc) · 22.5 KB
/
mangadex.py
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import discord
from discord import app_commands
from discord.ext import commands
import aiohttp
from PIL import Image
import io
from util_discord import command_check, description_helper, get_guild_prefix
BASE_URL = "https://api.mangadex.org"
provider = "https://gdjkhp.github.io/img/mangadex-logo.png"
pagelimit=12
async def help_manga(ctx: commands.Context):
if await command_check(ctx, "manga", "media"): return await ctx.reply("command disabled", ephemeral=True)
p = await get_guild_prefix(ctx)
sources = [f"`{p}dex` mangadex", f"`{p}nato` manganato"]
await ctx.reply("\n".join(sources))
async def dex_search(ctx: commands.Context, arg: str):
if await command_check(ctx, "manga", "media"): return await ctx.reply("command disabled", ephemeral=True)
if not arg: return await ctx.reply(f"usage: `{await get_guild_prefix(ctx)}dex <query>`")
msg = await ctx.reply("please wait")
res = await search_manga(arg)
if not res: return await msg.edit(content="none found")
await get_statistics(res)
await msg.edit(view=SearchView(ctx, arg, res, 0), embed=buildSearch(arg, res, 0), content=None)
async def req_real(url: str, params: dict=None):
async with aiohttp.ClientSession() as session:
async with session.get(url, params=params) as response:
if response.status == 200:
return await response.json()
async def convert_to_webp(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
if response.status == 200:
image_data = await response.read()
image = Image.open(io.BytesIO(image_data))
image_bytes = io.BytesIO()
image.save(image_bytes, format='WebP')
image_bytes.seek(0)
return image_bytes.getvalue()
def get_max_page(length):
if length % pagelimit != 0: return length - (length % pagelimit)
return length - pagelimit
def format_number(num):
if 1000 <= num < 1000000:
return f"{num // 1000}k"
elif 1000000 <= num < 1000000000:
return f"{num // 1000000}m"
elif 1000000000 <= num < 1000000000000:
return f"{num // 1000000000}b"
else:
return str(num)
async def search_manga(query):
search_url = f"{BASE_URL}/manga"
params = {"title": query}
data = await req_real(search_url, params)
if data["total"] > 0: return data["data"]
async def get_chapters(manga_id, offset):
chapters_url = f"{BASE_URL}/manga/{manga_id}/feed"
params = {"translatedLanguage[]": ["en"], "order[chapter]": "asc", "limit": 500, "offset": offset}
data = await req_real(chapters_url, params)
return data["data"]
async def get_pages(chapter_id):
pages_url = f"{BASE_URL}/at-home/server/{chapter_id}"
data = await req_real(pages_url)
if not data["result"] == "ok": return
pages = []
for image in data["chapter"]["data"]:
pages.append(f'https://uploads.mangadex.org/data/{data["chapter"]["hash"]}/{image}')
return pages
async def get_cover_art(manga):
cover_id = next((item["id"] for item in manga["relationships"] if item["type"] == "cover_art"), None)
if not cover_id: return None
response = await req_real(f"{BASE_URL}/cover/{cover_id}")
return f'https://uploads.mangadex.org/covers/{manga["id"]}/{response["data"]["attributes"]["fileName"]}'
async def get_author(manga):
author_url = f"{BASE_URL}/author"
author_ids = []
for item in manga["relationships"]:
if item["id"] in author_ids: continue
if item["type"] == "author" or item["type"] == "artist":
author_ids.append(item["id"])
if not author_ids: return None
params = {"ids[]": author_ids}
response = await req_real(author_url, params)
authors = []
for author in response["data"]:
authors.append(author["attributes"]["name"])
return ", ".join(authors)
async def get_scanlation(chapter):
group_url = f"{BASE_URL}/group"
group_ids = []
for item in chapter["relationships"]:
if item["id"] in group_ids: continue
if item["type"] == "scanlation_group":
group_ids.append(item["id"])
if not group_ids: return None
params = {"ids[]": group_ids}
response = await req_real(group_url, params)
groups = []
for group in response["data"]:
groups.append(group["attributes"]["name"])
return ", ".join(groups)
async def get_statistics(manga):
stats_url = f"{BASE_URL}/statistics/manga"
ids = []
for item in manga:
ids.append(item["id"])
params = {"manga[]": ids}
response = await req_real(stats_url, params)
for item in manga:
item["stats"] = response["statistics"][item["id"]]
def buildSearch(arg: str, result: list, index: int) -> discord.Embed:
embed = discord.Embed(title=f"Search results: `{arg}`", description=f"{len(result)} found", color=0x00ff00)
embed.set_thumbnail(url=provider)
i = index
while i < len(result):
stats = f"⭐{round(result[i]['stats']['rating']['bayesian'], 2)} 🔖{format_number(result[i]['stats']['follows'])}"
if (i < index+pagelimit): embed.add_field(name=f"[{i + 1}] `{next(iter(result[i]['attributes']['title'].values()))}`", value=stats)
i += 1
return embed
def buildManga(details: dict, count: int, total: int) -> discord.Embed:
tags = []
for tag in details['attributes']['tags']:
tags.append(next(iter(tag['attributes']['name'].values())))
author = f"**Author:** {details['author']}\n"
year = f"**Year:** {details['attributes']['year']}\n"
status = f"**Status:** {details['attributes']['status']}\n"
volumes = f"**Volumes:** {details['attributes']['lastVolume'] if details['attributes']['lastVolume'] else '⁉️'}\n"
chapters = f"**Chapters:** {details['attributes']['lastChapter'] if details['attributes']['lastChapter'] else '⁉️'}\n"
demo = f"**Demographic:** {details['attributes']['publicationDemographic'] if details['attributes']['publicationDemographic'] else '⁉️'}\n"
genres = f"**Genres:** {', '.join(tags)}\n\n"
desc = f"{next(iter(details['attributes']['description'].values())) if details['attributes']['description'] else ''}"
desc = author+year+status+volumes+chapters+demo+genres+desc
embed = discord.Embed(title=next(iter(details['attributes']['title'].values())), description=desc, color=0x00ff00)
embed.set_thumbnail(url=provider)
embed.set_footer(text=f"{min(count, total)}/{total}")
return embed
def buildPage(pages, pagenumber, chapters, index, details, group) -> discord.Embed:
title = f'\n{chapters[index]["attributes"]["title"]}' if chapters[index]["attributes"]["title"] else ""
group = f"\n{group}" if group else ""
ch = chapters[index]["attributes"]["chapter"] if chapters[index]["attributes"]["chapter"] else "⁉️"
desc = f'Chapter {ch}{title}\n{group}' # {index+1}/{len(chapters)}
embed = discord.Embed(title=next(iter(details['attributes']['title'].values())), description=desc, color=0x00ff00)
embed.set_thumbnail(url=provider)
embed.set_footer(text=f"{pagenumber+1}/{len(pages)}")
return embed
class CancelButton(discord.ui.Button):
def __init__(self, ctx: commands.Context, r: int):
super().__init__(emoji="❌", style=discord.ButtonStyle.success, row=r)
self.ctx = ctx
async def callback(self, interaction: discord.Interaction):
if interaction.user != self.ctx.author:
return await interaction.response.send_message(f"Only <@{self.ctx.author.id}> can interact with this message.",
ephemeral=True)
await interaction.response.defer()
await interaction.delete_original_response()
class DisabledButton(discord.ui.Button):
def __init__(self, e: str, r: int):
super().__init__(emoji=e, style=discord.ButtonStyle.success, disabled=True, row=r)
# search
class SearchView(discord.ui.View):
def __init__(self, ctx: commands.Context, arg: str, result: list, index: int):
super().__init__(timeout=None)
last_index = min(index + pagelimit, len(result))
self.add_item(SelectChoice(ctx, index, result))
if index - pagelimit > -1:
self.add_item(nextPage(ctx, arg, result, 0, "⏪"))
self.add_item(nextPage(ctx, arg, result, index - pagelimit, "◀️"))
else:
self.add_item(DisabledButton("⏪", 1))
self.add_item(DisabledButton("◀️", 1))
if not last_index == len(result):
self.add_item(nextPage(ctx, arg, result, last_index, "▶️"))
max_page = get_max_page(len(result))
self.add_item(nextPage(ctx, arg, result, max_page, "⏩"))
else:
self.add_item(DisabledButton("▶️", 1))
self.add_item(DisabledButton("⏩", 1))
self.add_item(CancelButton(ctx, 1))
class nextPage(discord.ui.Button):
def __init__(self, ctx: commands.Context, arg: str, result: list, index: int, l: str):
super().__init__(emoji=l, style=discord.ButtonStyle.success)
self.result, self.index, self.arg, self.ctx = result, index, arg, ctx
async def callback(self, interaction: discord.Interaction):
if interaction.user != self.ctx.author:
return await interaction.response.send_message(f"Only <@{self.ctx.author.id}> can interact with this message.",
ephemeral=True)
await interaction.response.edit_message(embed=buildSearch(self.arg, self.result, self.index),
view=SearchView(self.ctx, self.arg, self.result, self.index))
class SelectChoice(discord.ui.Select):
def __init__(self, ctx: commands.Context, index: int, result: list):
super().__init__(placeholder=f"{min(index + pagelimit, len(result))}/{len(result)} found")
i, self.result, self.ctx = index, result, ctx
while i < len(result):
stats = f"⭐{round(result[i]['stats']['rating']['bayesian'], 2)} 🔖{format_number(result[i]['stats']['follows'])}"
if (i < index+pagelimit): self.add_option(label=f"[{i + 1}] {next(iter(result[i]['attributes']['title'].values()))}"[:100],
value=i, description=stats)
if (i == index+pagelimit): break
i += 1
async def callback(self, interaction: discord.Interaction):
if interaction.user != self.ctx.author:
return await interaction.response.send_message(f"Only <@{self.ctx.author.id}> can interact with this message.",
ephemeral=True)
await interaction.response.defer()
selected = self.result[int(self.values[0])]
chapters, offset = [], 0
while True:
collect = await get_chapters(selected["id"], offset)
if not collect: break
chapters += collect
offset+=500
selected["cover"] = await convert_to_webp(await get_cover_art(selected))
selected["author"] = await get_author(selected)
await interaction.followup.send(embed=buildManga(selected, pagelimit, len(chapters)),
view=ChapterView(self.ctx, selected, chapters, 0),
file=discord.File(io.BytesIO(selected["cover"]), filename='image.webp'))
await interaction.delete_original_response()
# chapter
class nextPageCH(discord.ui.Button):
def __init__(self, ctx: commands.Context, details: dict, index: int, row: int, l: str, chapters: list):
super().__init__(emoji=l, style=discord.ButtonStyle.success, row=row)
self.details, self.index, self.ctx, self.chapters = details, index, ctx, chapters
async def callback(self, interaction: discord.Interaction):
if interaction.user != self.ctx.author:
return await interaction.response.send_message(f"Only <@{self.ctx.author.id}> can interact with this message.",
ephemeral=True)
# if interaction.message.attachments: await interaction.message.remove_attachments(interaction.message.attachments[0])
await interaction.response.defer()
await interaction.followup.send(embed=buildManga(self.details, self.index+pagelimit, len(self.chapters)),
view=ChapterView(self.ctx, self.details, self.chapters, self.index),
file=discord.File(io.BytesIO(self.details["cover"]), filename='image.webp'))
await interaction.delete_original_response()
class ChapterView(discord.ui.View):
def __init__(self, ctx: commands.Context, details: dict, chapters: list, index: int):
super().__init__(timeout=None)
i = index
column, row, last_index = 0, -1, len(chapters)
while i < len(chapters):
if column % 4 == 0: row += 1
if (i < index+pagelimit): self.add_item(ButtonChapter(ctx, i, chapters, details, row))
if (i == index+pagelimit): last_index = i
i += 1
column += 1
if index - pagelimit > -1:
self.add_item(nextPageCH(ctx, details, 0, 3, "⏪", chapters))
self.add_item(nextPageCH(ctx, details, index - pagelimit, 3, "◀️", chapters))
else:
self.add_item(DisabledButton("⏪", 3))
self.add_item(DisabledButton("◀️", 3))
if not last_index == len(chapters):
self.add_item(nextPageCH(ctx, details, last_index, 3, "▶️", chapters))
max_page = get_max_page(len(chapters))
self.add_item(nextPageCH(ctx, details, max_page, 3, "⏩", chapters))
else:
self.add_item(DisabledButton("▶️", 3))
self.add_item(DisabledButton("⏩", 3))
self.add_item(CancelButton(ctx, 3))
class ButtonChapter(discord.ui.Button):
def __init__(self, ctx: commands.Context, index: int, chapters: list, details: dict, row: int, l: str = None):
e = None
if not l:
l = chapters[index]["attributes"]["chapter"] if chapters[index]["attributes"]["chapter"] else "⁉️"
style = discord.ButtonStyle.primary
else:
e, l = l, None
style = discord.ButtonStyle.success
super().__init__(label=l, style=style, row=row, emoji=e)
self.index, self.chapters, self.ctx, self.details = index, chapters, ctx, details
async def callback(self, interaction: discord.Interaction):
if interaction.user != self.ctx.author:
return await interaction.response.send_message(f"Only <@{self.ctx.author.id}> can interact with this message.",
ephemeral=True)
# if interaction.message.attachments: await interaction.message.remove_attachments(interaction.message.attachments[0])
await interaction.response.defer()
pages = await get_pages(self.chapters[self.index]["id"])
if not pages:
await interaction.followup.send(content="no pages found")
await interaction.followup.send(view=ChapterView(self.ctx, self.details, self.chapters, (self.index//pagelimit)*pagelimit),
embed=buildManga(self.details, (self.index//pagelimit)*pagelimit+pagelimit, len(self.chapters)),
file=discord.File(io.BytesIO(self.details["cover"]), filename='image.webp'))
else:
group = await get_scanlation(self.chapters[self.index])
file = await convert_to_webp(pages[0])
await interaction.followup.send(view=PageView(self.ctx, self.details, pages, self.index, 0, self.chapters, group),
embed=buildPage(pages, 0, self.chapters, self.index, self.details, group),
file=discord.File(io.BytesIO(file), filename='image.webp'))
await interaction.delete_original_response()
# page
class nextPageReal(discord.ui.Button):
def __init__(self, ctx: commands.Context, details: dict, pagenumber: int, row: int, l: str, pages: list, index: int, chapters: list, group: str):
super().__init__(emoji=l, style=discord.ButtonStyle.success, row=row)
self.details, self.pagenumber, self.ctx, self.pages, self.index, self.chapters, self.group = details, pagenumber, ctx, pages, index, chapters, group
async def callback(self, interaction: discord.Interaction):
if interaction.user != self.ctx.author:
return await interaction.response.send_message(f"Only <@{self.ctx.author.id}> can interact with this message.",
ephemeral=True)
# if interaction.message.attachments: await interaction.message.remove_attachments(interaction.message.attachments[0])
await interaction.response.defer()
file = await convert_to_webp(self.pages[self.pagenumber])
await interaction.followup.send(embed=buildPage(self.pages, self.pagenumber, self.chapters, self.index, self.details, self.group),
view=PageView(self.ctx, self.details, self.pages, self.index, self.pagenumber, self.chapters, self.group),
file=discord.File(io.BytesIO(file), filename='image.webp'))
await interaction.delete_original_response()
class PageView(discord.ui.View):
def __init__(self, ctx: commands.Context, details: dict, pages: list, index: int, pagenumber: int, chapters: list, group: str):
super().__init__(timeout=None)
column, row, pageviewlimit = 0, -1, 8
i = (pagenumber // pageviewlimit) * pageviewlimit
while i < len(pages):
if column % 4 == 0: row += 1
if (i < ((pagenumber // pageviewlimit) * pageviewlimit)+pageviewlimit):
self.add_item(ButtonPage(ctx, i, pages, details, row, index, chapters, group))
i += 1
column += 1
if not pagenumber == 0:
self.add_item(nextPageReal(ctx, details, 0, 2, "⏪", pages, index, chapters, group))
self.add_item(nextPageReal(ctx, details, pagenumber - 1, 2, "◀️", pages, index, chapters, group))
else:
self.add_item(DisabledButton("⏪", 2))
self.add_item(DisabledButton("◀️", 2))
if pagenumber + 1 < len(pages):
self.add_item(nextPageReal(ctx, details, pagenumber+1, 2, "▶️", pages, index, chapters, group))
self.add_item(nextPageReal(ctx, details, len(pages)-1, 2, "⏩", pages, index, chapters, group))
else:
self.add_item(DisabledButton("▶️", 2))
self.add_item(DisabledButton("⏩", 2))
if index > 0: self.add_item(ButtonChapter(ctx, index-1, chapters, details, 3, "⏮️"))
else: self.add_item(DisabledButton("⏮️", 3))
self.add_item(CancelButton(ctx, 3))
self.add_item(ButtonBack(ctx, details, 3, index, chapters))
if index+1 < len(chapters): self.add_item(ButtonChapter(ctx, index+1, chapters, details, 3, "⏭️"))
else: self.add_item(DisabledButton("⏭️", 3))
class ButtonPage(discord.ui.Button):
def __init__(self, ctx: commands.Context, pagenumber: int, pages: list, details: dict, row: int, index: int, chapters: list, group: str):
super().__init__(label=str(pagenumber+1), style=discord.ButtonStyle.primary, row=row)
self.pagenumber, self.pages, self.ctx, self.details, self.index, self.chapters, self.group = pagenumber, pages, ctx, details, index, chapters, group
async def callback(self, interaction: discord.Interaction):
if interaction.user != self.ctx.author:
return await interaction.response.send_message(f"Only <@{self.ctx.author.id}> can interact with this message.",
ephemeral=True)
# if interaction.message.attachments: await interaction.message.remove_attachments(interaction.message.attachments[0])
await interaction.response.defer()
file = await convert_to_webp(self.pages[self.pagenumber])
await interaction.followup.send(view=PageView(self.ctx, self.details, self.pages, self.index, self.pagenumber, self.chapters, self.group),
embed=buildPage(self.pages, self.pagenumber, self.chapters, self.index, self.details, self.group),
file=discord.File(io.BytesIO(file), filename='image.webp'))
await interaction.delete_original_response()
class ButtonBack(discord.ui.Button):
def __init__(self, ctx: commands.Context, details: dict, row: int, index: int, chapters: list):
super().__init__(emoji="📖", style=discord.ButtonStyle.success, row=row)
self.ctx, self.details, self.index, self.chapters = ctx, details, index, chapters
async def callback(self, interaction: discord.Interaction):
if interaction.user != self.ctx.author:
return await interaction.response.send_message(f"Only <@{self.ctx.author.id}> can interact with this message.",
ephemeral=True)
# if interaction.message.attachments: await interaction.message.remove_attachments(interaction.message.attachments[0])
await interaction.response.defer()
await interaction.followup.send(view=ChapterView(self.ctx, self.details, self.chapters, (self.index//pagelimit)*pagelimit),
embed=buildManga(self.details, (self.index//pagelimit)*pagelimit+pagelimit, len(self.chapters)),
file=discord.File(io.BytesIO(self.details["cover"]), filename='image.webp'))
await interaction.delete_original_response()
class CogDex(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.hybrid_command(description=f'{description_helper["emojis"]["media"]} {description_helper["media"]["manga"]}')
@app_commands.allowed_installs(guilds=True, users=True)
@app_commands.allowed_contexts(guilds=True, dms=True, private_channels=True)
async def manga(self, ctx: commands.Context):
await help_manga(ctx)
@commands.hybrid_command(description=f"{description_helper['emojis']['manga']} mangadex")
@app_commands.describe(query="Search query")
@app_commands.allowed_installs(guilds=True, users=True)
@app_commands.allowed_contexts(guilds=True, dms=True, private_channels=True)
async def dex(self, ctx: commands.Context, *, query:str=None):
await dex_search(ctx, query)
async def setup(bot: commands.Bot):
await bot.add_cog(CogDex(bot))