Parcourir la source

fix: some async fixes

TheHamkerCat il y a 3 mois
Parent
commit
857788108f
2 fichiers modifiés avec 18 ajouts et 27 suppressions
  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 functools import partial
 from io import BytesIO
 from io import BytesIO
 
 
+import httpx
 from pyrogram import filters
 from pyrogram import filters
 from pytube import YouTube
 from pytube import YouTube
-from requests import get
 
 
 from wbb import aiohttpsession as session
 from wbb import aiohttpsession as session
 from wbb import app, arq
 from wbb import app, arq
@@ -54,14 +54,12 @@ def download_youtube_audio(arq_resp):
     performer = r.channel
     performer = r.channel
 
 
     m, s = r.duration.split(":")
     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:
     if duration > 1800:
         return
         return
 
 
-    thumb = get(r.thumbnails[0]).content
+    thumb = httpx.get(r.thumbnails[0]).content
     with open("thumbnail.png", "wb") as f:
     with open("thumbnail.png", "wb") as f:
         f.write(thumb)
         f.write(thumb)
     thumbnail_file = "thumbnail.png"
     thumbnail_file = "thumbnail.png"
@@ -91,9 +89,7 @@ async def music(_, message):
             "Another download is in progress, try again after sometime."
             "Another download is in progress, try again after sometime."
         )
         )
     is_downloading = True
     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:
     try:
         loop = get_running_loop()
         loop = get_running_loop()
         arq_resp = await arq.youtube(url)
         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
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
 SOFTWARE.
 """
 """
+
 import os
 import os
-from asyncio import gather, get_running_loop
+from asyncio import gather
 from base64 import b64decode
 from base64 import b64decode
 from io import BytesIO
 from io import BytesIO
 from random import randint
 from random import randint
 
 
 import aiofiles
 import aiofiles
-import requests
+import httpx
 from bs4 import BeautifulSoup
 from bs4 import BeautifulSoup
 from pyrogram import filters
 from pyrogram import filters
 from pyrogram.types import InputMediaPhoto, Message
 from pyrogram.types import InputMediaPhoto, Message
@@ -54,9 +55,7 @@ async def get_soup(url: str, headers):
 @capture_err
 @capture_err
 async def reverse_image_search(client, message: Message):
 async def reverse_image_search(client, message: Message):
     if not message.reply_to_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
     reply = message.reply_to_message
     if (
     if (
         not reply.document
         not reply.document
@@ -77,18 +76,14 @@ async def reverse_image_search(client, message: Message):
     async with aiofiles.open(image, "rb") as f:
     async with aiofiles.open(image, "rb") as f:
         if image:
         if image:
             search_url = "http://www.google.com/searchbyimage/upload"
             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")
             location = response.headers.get("Location")
             os.remove(image)
             os.remove(image)
         else:
         else:
@@ -110,9 +105,9 @@ async def reverse_image_search(client, message: Message):
 
 
     # Pass if no images detected
     # Pass if no images detected
     try:
     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)
         soup = await get_soup(url, headers=headers)