ソースを参照

fix: some async fixes

TheHamkerCat 3 ヶ月 前
親
コミット
857788108f
2 ファイル変更、18 行追加、27 行削除
  1. 4 8
      wbb/modules/music.py
  2. 14 19
      wbb/modules/reverse.py

+ 4 - 8
wbb/modules/music.py

@@ -28,9 +28,9 @@ from asyncio import get_running_loop
 from functools import partial
 from io import BytesIO
 
+import httpx
 from pyrogram import filters
 from pytube import YouTube
-from requests import get
 
 from wbb import aiohttpsession as session
 from wbb import app, arq
@@ -54,14 +54,12 @@ def download_youtube_audio(arq_resp):
     performer = r.channel
 
     m, s = r.duration.split(":")
-    duration = int(
-        datetime.timedelta(minutes=int(m), seconds=int(s)).total_seconds()
-    )
+    duration = int(datetime.timedelta(minutes=int(m), seconds=int(s)).total_seconds())
 
     if duration > 1800:
         return
 
-    thumb = get(r.thumbnails[0]).content
+    thumb = httpx.get(r.thumbnails[0]).content
     with open("thumbnail.png", "wb") as f:
         f.write(thumb)
     thumbnail_file = "thumbnail.png"
@@ -91,9 +89,7 @@ async def music(_, message):
             "Another download is in progress, try again after sometime."
         )
     is_downloading = True
-    m = await message.reply_text(
-        f"Downloading {url}", disable_web_page_preview=True
-    )
+    m = await message.reply_text(f"Downloading {url}", disable_web_page_preview=True)
     try:
         loop = get_running_loop()
         arq_resp = await arq.youtube(url)

+ 14 - 19
wbb/modules/reverse.py

@@ -21,14 +21,15 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
 """
+
 import os
-from asyncio import gather, get_running_loop
+from asyncio import gather
 from base64 import b64decode
 from io import BytesIO
 from random import randint
 
 import aiofiles
-import requests
+import httpx
 from bs4 import BeautifulSoup
 from pyrogram import filters
 from pyrogram.types import InputMediaPhoto, Message
@@ -54,9 +55,7 @@ async def get_soup(url: str, headers):
 @capture_err
 async def reverse_image_search(client, message: Message):
     if not message.reply_to_message:
-        return await eor(
-            message, text="Reply to a message to reverse search it."
-        )
+        return await eor(message, text="Reply to a message to reverse search it.")
     reply = message.reply_to_message
     if (
         not reply.document
@@ -77,18 +76,14 @@ async def reverse_image_search(client, message: Message):
     async with aiofiles.open(image, "rb") as f:
         if image:
             search_url = "http://www.google.com/searchbyimage/upload"
-            multipart = {
-                "encoded_image": (image, await f.read()),
-                "image_content": "",
-            }
-
-            def post_non_blocking():
-                return requests.post(
-                    search_url, files=multipart, allow_redirects=False
-                )
+            multipart = {"encoded_image": (image, await f.read())}
 
-            loop = get_running_loop()
-            response = await loop.run_in_executor(None, post_non_blocking)
+            async with httpx.AsyncClient(follow_redirects=False) as client_session:
+                response = await client_session.post(
+                    search_url,
+                    data={"image_content": ""},
+                    files=multipart,
+                )
             location = response.headers.get("Location")
             os.remove(image)
         else:
@@ -110,9 +105,9 @@ async def reverse_image_search(client, message: Message):
 
     # Pass if no images detected
     try:
-        url = "https://google.com" + soup.find_all(
-            "a", {"class": "ekf0x hSQtef"}
-        )[0].get("href")
+        url = "https://google.com" + soup.find_all("a", {"class": "ekf0x hSQtef"})[
+            0
+        ].get("href")
 
         soup = await get_soup(url, headers=headers)