Skip to content

Commit

Permalink
Refactor movieku scraper v2
Browse files Browse the repository at this point in the history
  • Loading branch information
yasirarism authored Sep 24, 2024
1 parent 9fb073c commit 5136b96
Showing 1 changed file with 50 additions and 23 deletions.
73 changes: 50 additions & 23 deletions misskaty/plugins/web_scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ async def getDataKuso(msg, kueri, CurrentPage, user, strings):


# Movieku GetData
async def getDataMovieku(msg, kueri, CurrentPage, strings):
async def getDataMovieku(msg, kueri, CurrentPage, user, strings):
if not SCRAP_DICT.get(msg.id):
moviekudata = []
with contextlib.redirect_stdout(sys.stderr):
Expand Down Expand Up @@ -264,15 +264,21 @@ async def getDataMovieku(msg, kueri, CurrentPage, strings):
SCRAP_DICT.add(msg.id, [split_arr(moviekudata, 6), kueri], timeout=1800)
index = int(CurrentPage - 1)
PageLen = len(SCRAP_DICT[msg.id][0])
extractbtn = []

moviekuResult = (
strings("header_no_query").format(web="Movieku", cmd="movieku")
if kueri == ""
else strings("header_with_query").format(web="Movieku", kueri=kueri)
)
for c, i in enumerate(SCRAP_DICT[msg.id][0][index], start=1):
moviekuResult += f"<b>{index*6+c}. <a href='{i['link']}'>{i['judul']}</a></b>\n<b>{strings('quality')}/Status:</b> {i['type']}\n<b>Extract:</b> <code>/movieku_scrap {i['link']}</code>\n\n"
return moviekuResult, PageLen
moviekuResult += f"<b>{index*6+c}. <a href='{i['link']}'>{i['judul']}</a></b>\n<b>{strings('quality')}/Status:</b> {i['type']}\n\n"
extractbtn.append(
InlineButton(
index * 6 + c, f"moviekuextract#{CurrentPage}#{c}#{user}#{msg.id}"
)
)
return moviekuResult, PageLen, extractbtn


# NoDrakor GetData
Expand Down Expand Up @@ -846,7 +852,7 @@ async def movieku_s(self, ctx: Message, strings):
kueri = ""
pesan = await ctx.reply_msg(strings("get_data"), quote=True)
CurrentPage = 1
moviekures, PageLen = await getDataMovieku(pesan, kueri, CurrentPage, strings)
moviekures, PageLen, btn = await getDataMovieku(pesan, kueri, CurrentPage, ctx.from_user.id, strings)
if not moviekures:
return
keyboard = InlineKeyboard()
Expand All @@ -855,6 +861,8 @@ async def movieku_s(self, ctx: Message, strings):
CurrentPage,
"page_movieku#{number}" + f"#{pesan.id}#{ctx.from_user.id}",
)
keyboard.row(InlineButton(strings("ex_data"), user_id=self.me.id))
keyboard.row(*btn)
keyboard.row(InlineButton(strings("cl_btn"), f"close#{ctx.from_user.id}"))
await pesan.edit_msg(
moviekures, disable_web_page_preview=True, reply_markup=keyboard
Expand Down Expand Up @@ -1038,7 +1046,7 @@ async def lendrivepage_callback(self, callback_query, strings):
# Movieku Page Callback
@app.on_cb("page_movieku#")
@use_chat_lang()
async def moviekupage_callback(_, callback_query, strings):
async def moviekupage_callback(self, callback_query, strings):
try:
if callback_query.from_user.id != int(callback_query.data.split("#")[3]):
return await callback_query.answer(strings("unauth"), True)
Expand All @@ -1051,8 +1059,8 @@ async def moviekupage_callback(_, callback_query, strings):
return await callback_query.message.edit_msg(strings("invalid_cb"), True)

try:
moviekures, PageLen = await getDataMovieku(
callback_query.message, kueri, CurrentPage, strings
moviekures, PageLen, btn = await getDataMovieku(
callback_query.message, kueri, CurrentPage, callback_query.from_user.id, strings
)
except TypeError:
return
Expand All @@ -1063,6 +1071,8 @@ async def moviekupage_callback(_, callback_query, strings):
CurrentPage,
"page_movieku#{number}" + f"#{message_id}#{callback_query.from_user.id}",
)
keyboard.row(InlineButton(strings("ex_data"), user_id=self.me.id))
keyboard.row(*btn)
keyboard.row(
InlineButton(strings("cl_btn"), f"close#{callback_query.from_user.id}")
)
Expand Down Expand Up @@ -1440,13 +1450,32 @@ async def nodrakorddl_scrap(_, callback_query, strings):
)


# Scrape Link Download Movieku.CC
@app.on_cmd("movieku_scrap")
# Scrape DDL Link Melongmovie
@app.on_cb("moviekuextract#")
@use_chat_lang()
async def muviku_scrap(_, message, strings):
async def movieku_scrap(_, callback_query, strings):
try:
if callback_query.from_user.id != int(callback_query.data.split("#")[3]):
return await callback_query.answer(strings("unauth"), True)
idlink = int(callback_query.data.split("#")[2])
message_id = int(callback_query.data.split("#")[4])
CurrentPage = int(callback_query.data.split("#")[1])
link = SCRAP_DICT[message_id][0][CurrentPage - 1][idlink - 1].get("link")
except QueryIdInvalid:
return
except KeyError:
return await callback_query.message.edit_msg(strings("invalid_cb"))

keyboard = InlineKeyboard()
keyboard.row(
InlineButton(
strings("back_btn"),
f"page_movieku#{CurrentPage}#{message_id}#{callback_query.from_user.id}",
),
InlineButton(strings("cl_btn"), f"close#{callback_query.from_user.id}"),
)
with contextlib.redirect_stdout(sys.stderr):
try:
link = message.text.split(maxsplit=1)[1]
html = await fetch.get(link)
html.raise_for_status()
soup = BeautifulSoup(html.text, "lxml")
Expand All @@ -1466,31 +1495,29 @@ async def muviku_scrap(_, message, strings):
if strong_tag:
resolution = strong_tag.text.strip()
if resolution in valid_resolutions:
total_links += 1
links = ', '.join([f'<a href="{a["href"]}">{a.text.strip()}</a>' for a in element.find_all('a')])
data[current_title].append(f"{resolution} {links}")

for title, resolutions in data.items():
output.append(title)
output.extend(resolutions)
output.append('')
for res in resolutions:
total_links += res.count('<a href=')
if not data:
return await message.reply(strings("no_result"))
if total_links > 70:
url = await post_to_telegraph(False, link, "<br>".join(output))
return await message.reply_msg(f"Your result is too long, i have pasted your result on Telegraph:\n{url}")
await message.reply_msg("\n".join(output))
except IndexError:
return await message.reply(
strings("invalid_cmd_scrape").format(cmd=message.command[0])
)
return await callback_query.message.edit_msg(strings("res_scrape").format(link=link, kl=f"Your result is too long, i have pasted your result on Telegraph:\n{url})", reply_markup=keyboard)
await callback_query.message.edit_msg(strings("res_scrape").format(link=link, kl="\n".join(output)), reply_markup=keyboard)
except httpx.HTTPError as exc:
await message.reply(
f"HTTP Exception for {exc.request.url} - <code>{exc}</code>"
await callback_query.message.edit_msg(
f"HTTP Exception for {exc.request.url} - <code>{exc}</code>",
reply_markup=keyboard,
)
except Exception as err:
await callback_query.message.edit_msg(
f"ERROR: {err}", reply_markup=keyboard
)
except Exception as e:
await message.reply(f"ERROR: {str(e)}")


# Scrape DDL Link Melongmovie
Expand Down

0 comments on commit 5136b96

Please sign in to comment.