Fix httpx exception and use contextlib to handle some exception

Signed-off-by: Yasir Aris M <git@yasirdev.my.id>
This commit is contained in:
Yasir Aris M 2023-10-18 11:26:06 +07:00
parent 6bff71a082
commit 298a076362
3 changed files with 701 additions and 676 deletions

View file

@ -2,10 +2,12 @@
# * @date 2023-06-21 22:12:27
# * @projectName MissKatyPyro
# * Copyright ©YasirPedia All rights reserved
import contextlib
import httpx
import json
import logging
import re
import sys
from urllib.parse import quote_plus
from bs4 import BeautifulSoup
@ -135,6 +137,7 @@ async def imdb_search_id(kueri, message):
)
msg = ""
buttons = InlineKeyboard(row_width=4)
with contextlib.redirect_stdout(sys.stderr):
try:
r = await fetch.get(
f"https://v3.sg.media-imdb.com/suggestion/titles/x/{quote_plus(kueri)}.json"
@ -196,6 +199,7 @@ async def imdb_search_en(kueri, message):
)
msg = ""
buttons = InlineKeyboard(row_width=4)
with contextlib.redirect_stdout(sys.stderr):
try:
r = await fetch.get(
f"https://v3.sg.media-imdb.com/suggestion/titles/x/{quote_plus(kueri)}.json"
@ -268,6 +272,7 @@ async def imdbcari(_, query: CallbackQuery):
pass
msg = ""
buttons = InlineKeyboard(row_width=4)
with contextlib.redirect_stdout(sys.stderr):
try:
r = await fetch.get(
f"https://v3.sg.media-imdb.com/suggestion/titles/x/{quote_plus(kueri)}.json"
@ -324,6 +329,7 @@ async def imdbcari(_, query: CallbackQuery):
await query.message.edit_caption("<i>🔎 Looking in the IMDB Database..</i>")
msg = ""
buttons = InlineKeyboard(row_width=4)
with contextlib.redirect_stdout(sys.stderr):
try:
r = await fetch.get(
f"https://v3.sg.media-imdb.com/suggestion/titles/x/{quote_plus(kueri)}.json"
@ -376,6 +382,7 @@ async def imdb_id_callback(self: Client, query: CallbackQuery):
i, userid, movie = query.data.split("#")
if query.from_user.id != int(userid):
return await query.answer("⚠️ Akses Ditolak!", True)
with contextlib.redirect_stdout(sys.stderr):
try:
await query.message.edit_caption("⏳ Permintaan kamu sedang diproses.. ")
imdb_url = f"https://www.imdb.com/title/tt{movie}/"
@ -537,6 +544,7 @@ async def imdb_en_callback(self: Client, query: CallbackQuery):
i, userid, movie = query.data.split("#")
if query.from_user.id != int(userid):
return await query.answer("⚠️ Access Denied!", True)
with contextlib.redirect_stdout(sys.stderr):
try:
await query.message.edit_caption("<i>⏳ Getting IMDb source..</i>")
imdb_url = f"https://www.imdb.com/title/tt{movie}/"

View file

@ -6,10 +6,12 @@
"""
import asyncio
import contextlib
import html
import json
import os
import re
import sys
import traceback
from logging import getLogger
from urllib.parse import quote
@ -207,6 +209,7 @@ async def carbon_make(self: Client, ctx: Message):
"code": text,
"backgroundColor": "#1F816D",
}
with contextlib.redirect_stdout(sys.stderr):
try:
response = await fetch.post(
"https://carbon.yasirapi.eu.org/api/cook", json=json_data, timeout=20
@ -561,17 +564,13 @@ async def who_is(client, message):
async def close_callback(_, query: CallbackQuery):
_, userid = query.data.split("#")
if query.from_user.id != int(userid):
try:
with contextlib.suppress(QueryIdInvalid):
return await query.answer("⚠️ Access Denied!", True)
except QueryIdInvalid:
return
try:
with contextlib.redirect_stdout(Exception):
await query.answer("Deleting this message in 5 seconds.")
await asyncio.sleep(5)
await query.message.delete()
await query.message.reply_to_message.delete()
except:
pass
async def mdlapi(title):

View file

@ -4,9 +4,11 @@
* @projectName MissKatyPyro
* Copyright @YasirPedia All rights reserved
"""
import contextlib
import httpx
import logging
import re
import sys
import traceback
import cloudscraper
@ -71,6 +73,7 @@ def split_arr(arr, size: 5):
# Terbit21 GetData
async def getDataTerbit21(msg, kueri, CurrentPage, strings):
if not SCRAP_DICT.get(msg.id):
with contextlib.redirect_stdout(sys.stderr):
try:
if kueri:
terbitjson = await fetch.get(f"{web['yasirapi']}/terbit21?q={kueri}")
@ -108,6 +111,7 @@ async def getDataTerbit21(msg, kueri, CurrentPage, strings):
# LK21 GetData
async def getDatalk21(msg, kueri, CurrentPage, strings):
if not SCRAP_DICT.get(msg.id):
with contextlib.redirect_stdout(sys.stderr):
try:
if kueri:
lk21json = await fetch.get(f"{web['yasirapi']}/lk21?q={kueri}")
@ -143,6 +147,7 @@ async def getDatalk21(msg, kueri, CurrentPage, strings):
# Pahe GetData
async def getDataPahe(msg, kueri, CurrentPage, strings):
if not SCRAP_DICT.get(msg.id):
with contextlib.redirect_stdout(sys.stderr):
try:
if kueri:
pahejson = await fetch.get(f"{web['yasirapi']}/pahe?q={kueri}")
@ -175,6 +180,7 @@ async def getDataPahe(msg, kueri, CurrentPage, strings):
async def getDataKuso(msg, kueri, CurrentPage, user, strings):
if not SCRAP_DICT.get(msg.id):
kusodata = []
with contextlib.redirect_stdout(sys.stderr):
try:
data = await fetch.get(
f"{web['kusonime']}/?s={kueri}", follow_redirects=True
@ -224,6 +230,7 @@ async def getDataKuso(msg, kueri, CurrentPage, user, strings):
async def getDataMovieku(msg, kueri, CurrentPage, strings):
if not SCRAP_DICT.get(msg.id):
moviekudata = []
with contextlib.redirect_stdout(sys.stderr):
try:
data = await fetch.get(
f"{web['movieku']}/?s={kueri}", follow_redirects=True
@ -261,6 +268,7 @@ async def getDataMovieku(msg, kueri, CurrentPage, strings):
async def getDataNodrakor(msg, kueri, CurrentPage, user, strings):
if not SCRAP_DICT.get(msg.id):
nodrakordata = []
with contextlib.redirect_stdout(sys.stderr):
try:
data = await fetch.get(
f"{web['nodrakor']}/?s={kueri}", follow_redirects=True,
@ -308,6 +316,7 @@ async def getDataNodrakor(msg, kueri, CurrentPage, user, strings):
async def getDataSavefilm21(msg, kueri, CurrentPage, user, strings):
if not SCRAP_DICT.get(msg.id):
sfdata = []
with contextlib.redirect_stdout(sys.stderr):
try:
data = await fetch.get(
f"{web['savefilm21']}/?s={kueri}", follow_redirects=True,
@ -354,6 +363,7 @@ async def getDataSavefilm21(msg, kueri, CurrentPage, user, strings):
# Lendrive GetData
async def getDataLendrive(msg, kueri, CurrentPage, user, strings):
if not SCRAP_DICT.get(msg.id):
with contextlib.redirect_stdout(sys.stderr):
try:
if kueri:
data = await fetch.get(
@ -409,6 +419,7 @@ async def getDataLendrive(msg, kueri, CurrentPage, user, strings):
# MelongMovie GetData
async def getDataMelong(msg, kueri, CurrentPage, user, strings):
if not SCRAP_DICT.get(msg.id):
with contextlib.redirect_stdout(sys.stderr):
try:
data = await fetch.get(
f"{web['melongmovie']}/?s={kueri}", follow_redirects=True,
@ -454,6 +465,7 @@ async def getDataMelong(msg, kueri, CurrentPage, user, strings):
# GoMov GetData
async def getDataGomov(msg, kueri, CurrentPage, user, strings):
if not SCRAP_DICT.get(msg.id):
with contextlib.redirect_stdout(sys.stderr):
try:
gomovv = await fetch.get(
f"{web['gomov']}/?s={kueri}", follow_redirects=True
@ -1310,6 +1322,7 @@ async def savefilm21_scrap(_, callback_query, strings):
),
InlineButton(strings("cl_btn"), f"close#{callback_query.from_user.id}"),
)
with contextlib.redirect_stdout(sys.stderr):
try:
html = await fetch.get(link)
html.raise_for_status()
@ -1349,6 +1362,7 @@ async def nodrakorddl_scrap(_, callback_query, strings):
),
InlineButton(strings("cl_btn"), f"close#{callback_query.from_user.id}"),
)
with contextlib.redirect_stdout(sys.stderr):
try:
html = await fetch.get(link)
html.raise_for_status()
@ -1382,6 +1396,7 @@ async def nodrakorddl_scrap(_, callback_query, strings):
@app.on_cmd("movieku_scrap")
@use_chat_lang()
async def muviku_scrap(_, message, strings):
with contextlib.redirect_stdout(sys.stderr):
try:
link = message.text.split(maxsplit=1)[1]
html = await fetch.get(link)
@ -1433,6 +1448,7 @@ async def melong_scrap(_, callback_query, strings):
),
InlineButton(strings("cl_btn"), f"close#{callback_query.from_user.id}"),
)
with contextlib.redirect_stdout(sys.stderr):
try:
html = await fetch.get(link)
html.raise_for_status()
@ -1475,6 +1491,7 @@ async def gomov_dl(_, callback_query, strings):
),
InlineButton(strings("cl_btn"), f"close#{callback_query.from_user.id}"),
)
with contextlib.redirect_stdout(sys.stderr):
try:
html = await fetch.get(link)
html.raise_for_status()
@ -1515,6 +1532,7 @@ async def lendrive_dl(_, callback_query, strings):
),
InlineButton(strings("cl_btn"), f"close#{callback_query.from_user.id}"),
)
with contextlib.redirect_stdout(sys.stderr):
try:
hmm = await fetch.get(link)
hmm.raise_for_status()