-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
animepahe.py
273 lines (246 loc) · 14.5 KB
/
animepahe.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
import discord
from bs4 import BeautifulSoup as BS
import aiohttp
from discord.ext import commands
import os
import re
from util_discord import command_check, description_helper, get_guild_prefix
from curl_cffi.requests import AsyncSession
import sys
import asyncio
from discord import app_commands
if sys.platform == 'win32':
asyncio.set_event_loop_policy(
asyncio.WindowsSelectorEventLoopPolicy()
)
session = AsyncSession(impersonate='chrome110')
headers = {"cookie": os.getenv('PAHE')}
pagelimit=12
provider="https://gdjkhp.github.io/img/apdoesnthavelogotheysaidapistooplaintheysaid.png"
async def help_anime(ctx: commands.Context):
if await command_check(ctx, "anime", "media"): return await ctx.reply("command disabled", ephemeral=True)
p = await get_guild_prefix(ctx)
sources = [f"`{p}gogo` gogoanime", f"`{p}pahe` animepahe"]
await ctx.reply("\n".join(sources))
async def new_req_old(url: str, headers: dict, json_mode: bool):
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers) as response:
if response.status == 200:
return await response.json() if json_mode else await response.read()
async def new_req(url: str, headers: dict, json_mode: bool):
req = await session.get(url, headers=headers)
return req.json() if json_mode else req.content
def soupify(data): return BS(data, "lxml")
def get_max_page(length):
if length % pagelimit != 0: return length - (length % pagelimit)
return length - pagelimit
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):
value = f"**{result[i]['type']}** - {result[i]['episodes']} {'episodes' if result[i]['episodes'] > 1 else 'episode'}\n({result[i]['status']})"
value+= f"\n{result[i]['season']} {result[i]['year']}"
if (i < index+pagelimit): embed.add_field(name=f"[{i + 1}] `{result[i]['title']}`", value=value)
i += 1
return embed
def format_links(string: str, links: list):
items = string.split('\n', 1)[-1].split(', ')
result = "**External Links:**\n"
for i in range(len(links)):
result += f"[{items[i]}]({links[i]}), "
return result.rstrip(", ")
def enclose_words(texts: list[str]):
new_list = []
for word in texts:
split = word.split(":")
split[0] = f"**{split[0]}:**"
new_list.append(" ".join(split))
return new_list
def buildAnime(details: dict) -> discord.Embed:
cook_deets = "\n".join(details["details"])
cook_deets+= f'\n**Genres:** {", ".join(details["genres"])}'
embed = discord.Embed(title=details['title'], description=cook_deets, color=0x00ff00)
embed.set_thumbnail(url=provider)
embed.set_image(url = details['poster'])
embed.set_footer(text="Note: Use Adblockers :)")
return embed
async def pahe_search(ctx: commands.Context, arg: str):
if await command_check(ctx, "anime", "media"): return await ctx.reply("command disabled", ephemeral=True)
if not arg: return await ctx.reply(f"usage: `{await get_guild_prefix(ctx)}pahe <query>`")
results = await new_req(f"https://animepahe.ru/api?m=search&q={arg.replace(' ', '+')}", headers, True)
if not results: return await ctx.reply("none found")
await ctx.reply(embed=buildSearch(arg, results["data"], 0), view=SearchView(ctx, arg, results["data"], 0))
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 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 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 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):
if (i < index+pagelimit): self.add_option(label=f"[{i + 1}] {result[i]['title']}"[:100], value=i,
description=f"{result[i]['season']} {result[i]['year']}")
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.edit_message(view=None)
selected = self.result[int(self.values[0])]
r_search = await new_req(f"https://animepahe.ru/api?m=release&id={selected['session']}&sort=episode_asc&page=1", headers, True)
if not r_search.get('data'): return await interaction.edit_original_response(content="no episodes found", embed=None)
req = await new_req(f"https://animepahe.ru/play/{selected['session']}/{r_search['data'][0]['session']}", headers, False)
soup = soupify(req)
items = soup.find("div", {"class": "clusterize-scroll"}).findAll("a")
urls = [items[i].get("href") for i in range(len(items))]
ep_texts = [items[i].text for i in range(len(items))]
req = await new_req(f"https://animepahe.ru/anime/{selected['session']}", headers, False)
soup = soupify(req)
details = soup.find("div", {"class": "anime-info"}).findAll("p")
external = soup.find("p", {"class": "external-links"}).findAll("a")
genres = soup.find("div", {"class": "anime-genre"}).findAll("li")
selected["genres"] = [re.sub(r"^\s+|\s+$|\s+(?=\s)", "", genres[i].text) for i in range(len(genres))]
not_final = [re.sub(r"^\s+|\s+$|\s+(?=\s)", "", details[i].text) for i in range(len(details))]
externals = [external[i].get("href").replace("//", "https://") for i in range(len(external))]
selected["details"] = enclose_words(not_final)
selected["details"][len(selected["details"])-1] = format_links(selected["details"][len(selected["details"])-1], externals)
await interaction.edit_original_response(embed=buildAnime(selected), view=EpisodeView(self.ctx, selected, urls, ep_texts, 0))
# episode
class nextPageEP(discord.ui.Button):
def __init__(self, ctx: commands.Context, details: list, index: int, row: int, l: str, urls: list, ep_texts: list):
super().__init__(emoji=l, style=discord.ButtonStyle.success, row=row)
self.details, self.index, self.ctx, self.urls, self.ep_texts = details, index, ctx, urls, ep_texts
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(view=EpisodeView(self.ctx, self.details, self.urls, self.ep_texts, self.index))
class EpisodeView(discord.ui.View):
def __init__(self, ctx: commands.Context, details: dict, urls: list, ep_texts: list, index: int):
super().__init__(timeout=None)
i = index
column, row, last_index = 0, -1, len(urls)
while i < len(urls):
if column % 4 == 0: row += 1
if (i < index+pagelimit): self.add_item(ButtonEpisode(ctx, i, urls[i], ep_texts[i], details, row))
if (i == index+pagelimit): last_index = i
i += 1
column += 1
if index - pagelimit > -1:
self.add_item(nextPageEP(ctx, details, 0, 3, "⏪", urls, ep_texts))
self.add_item(nextPageEP(ctx, details, index - pagelimit, 3, "◀️", urls, ep_texts))
else:
self.add_item(DisabledButton("⏪", 3))
self.add_item(DisabledButton("◀️", 3))
if not last_index == len(urls):
self.add_item(nextPageEP(ctx, details, last_index, 3, "▶️", urls, ep_texts))
max_page = get_max_page(len(urls))
self.add_item(nextPageEP(ctx, details, max_page, 3, "⏩", urls, ep_texts))
else:
self.add_item(DisabledButton("▶️", 3))
self.add_item(DisabledButton("⏩", 3))
self.add_item(CancelButton(ctx, 3))
class ButtonEpisode(discord.ui.Button):
def __init__(self, ctx: commands.Context, index: int, url_session: str, ep_text: str, details: dict, row: int):
super().__init__(label=ep_text.replace("Episode ", ""), style=discord.ButtonStyle.primary, row=row)
self.index, self.url_session, self.ctx, self.details, self.ep_text = index, url_session, ctx, details, ep_text
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()
req = await new_req(f"https://animepahe.ru{self.url_session}", headers, False)
soup = soupify(req)
items = soup.find("div", {"id": "pickDownload"}).findAll("a")
urls = [items[i].get("href") for i in range(len(items))]
texts = [items[i].text for i in range(len(items))]
msg_content = f"{self.details['title']}: {self.ep_text}"
for x in range(len(urls)):
msg_content += f"\n{x+1}. {texts[x]}"
await interaction.followup.send(msg_content, view=DownloadView(self.ctx, urls, self.details, self.index, texts, self.ep_text),
ephemeral=True)
class ButtonDownload(discord.ui.Button):
def __init__(self, ctx: commands.Context, url_fake: str, l: str, details: dict, index: int, text: str, ep_text: str):
super().__init__(label=l+1)
self.url_fake, self.ctx, self.details, self.index, self.text, self.ep_text = url_fake, ctx, details, index, text, ep_text
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()
req = await new_req(self.url_fake, None, False)
soup = soupify(req)
script_tag = soup.find("script")
match = re.search(r"https://kwik\.si/f/\w+", script_tag.string)
if match:
await interaction.followup.send(f"{self.details['title']}: {self.ep_text} [{self.text}]",
view=WatchView([match.group()]), ephemeral=True)
class DownloadView(discord.ui.View):
def __init__(self, ctx: commands.Context, urls: list, details: dict, index: int, texts: list, ep_text: str):
super().__init__(timeout=None)
for x in range(len(urls)):
self.add_item(ButtonDownload(ctx, urls[x], x, details, index, texts[x], ep_text))
class WatchView(discord.ui.View):
def __init__(self, links: list):
super().__init__(timeout=None)
for x in links[:25]:
self.add_item(discord.ui.Button(style=discord.ButtonStyle.link, url=x, emoji="🎞️",
label=f"Watch Full HD Movies & TV Shows"))
class CogPahe(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.hybrid_command(description=f"{description_helper['emojis']['anime']} animepahe")
@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 pahe(self, ctx: commands.Context, *, query:str=None):
await pahe_search(ctx, query)
@commands.hybrid_command(description=f'{description_helper["emojis"]["media"]} {description_helper["media"]["anime"]}')
@app_commands.allowed_installs(guilds=True, users=True)
@app_commands.allowed_contexts(guilds=True, dms=True, private_channels=True)
async def anime(self, ctx: commands.Context):
await help_anime(ctx)
async def setup(bot: commands.Bot):
await bot.add_cog(CogPahe(bot))