TheHamkerCat 5 лет назад
Родитель
Сommit
3cb2424793
3 измененных файлов с 63 добавлено и 21 удалено
  1. 11 13
      wbb/utils/dbfunctions.py
  2. 17 7
      wbb/utils/fetch.py
  3. 35 1
      wbb/utils/functions.py

+ 11 - 13
wbb/utils/dbfunctions.py

@@ -775,16 +775,14 @@ async def get_trust_db(user_id: int) -> list:
 
 
 
 
 async def update_trust_db(user_id: int, new_data: float):
 async def update_trust_db(user_id: int, new_data: float):
-    try:
-        user = await trustdb.find_one({"user_id": user_id})
-        data = user["data"] if user else []
-        if len(data) >= 1000:
-            data.remove(data[0])
-        data.append(new_data)
-        await trustdb.update_one(
-            {"user_id": user_id},
-            {"$set": {"data": data}},
-            upsert=True,
-        )
-    except Exception:
-        pass  # to prevent stack overflow error
+    user = await trustdb.find_one({"user_id": user_id})
+    data = user["data"] if user else []
+    if len(data) >= 100:
+        data = data[:99]
+    data.append(new_data)
+    data = [i for i in data if isinstance(i, float) or isinstance(i, int)]
+    await trustdb.update_one(
+        {"user_id": user_id},
+        {"$set": {"data": data}},
+        upsert=True,
+    )

+ 17 - 7
wbb/utils/fetch.py

@@ -21,18 +21,28 @@ 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.
 """
 """
-from wbb import aiohttpsession
+from asyncio import gather
+from wbb import aiohttpsession as session
 
 
-headers = {
-    "Accept": "application/json",
-    "Content-Type": "application/json",
-}
+async def fetch(url: str, *args, **kwargs):
+    async with session.get(url, *args, **kwargs) as resp:
+        try:
+            data = await resp.json()
+        except Exception:
+            data = await resp.text()
+    return data
 
 
 
 
-async def fetch(url: str):
-    async with aiohttpsession.get(url, headers=headers) as resp:
+async def post(url: str, *args, **kwargs):
+    async with session.get(url, *args, **kwargs) as resp:
         try:
         try:
             data = await resp.json()
             data = await resp.json()
         except Exception:
         except Exception:
             data = await resp.text()
             data = await resp.text()
     return data
     return data
+
+async def multifetch(url: str, times: int, *args, **kwargs):
+    return await gather(*[fetch(url, *args, **kwargs) for _ in range(times)])
+
+async def multipost(url: str, times: int, *args, **kwargs):
+    return await gather(*[post(url, *args, **kwargs) for _ in range(times)])

+ 35 - 1
wbb/utils/functions.py

@@ -28,6 +28,7 @@ from io import BytesIO
 from math import atan2, cos, radians, sin, sqrt
 from math import atan2, cos, radians, sin, sqrt
 from random import randint
 from random import randint
 from re import findall
 from re import findall
+from time import time
 
 
 import aiofiles
 import aiofiles
 import aiohttp
 import aiohttp
@@ -35,7 +36,7 @@ import speedtest
 from PIL import Image, ImageDraw, ImageFilter, ImageFont
 from PIL import Image, ImageDraw, ImageFilter, ImageFont
 from wget import download
 from wget import download
 
 
-from wbb import aiohttpsession as aiosession
+from wbb import aiohttpsession as aiosession, arq
 from wbb.utils import aiodownloader
 from wbb.utils import aiodownloader
 from wbb.utils.fetch import fetch
 from wbb.utils.fetch import fetch
 
 
@@ -233,3 +234,36 @@ async def extract_user_and_reason(message):
 
 
 async def extract_user(message):
 async def extract_user(message):
     return (await extract_user_and_reason(message))[0]
     return (await extract_user_and_reason(message))[0]
+
+async def test_ARQ(message):
+    results = ""
+    funcs ={"deezer": arq.deezer("attention", 1),
+            "image": arq.image("something"),
+            "luna": arq.luna("hello"),
+            "lyrics": arq.lyrics("attention"),
+            "nlp": arq.nlp("bitcoin"),
+            "nsfw_scan": arq.nsfw_scan(url="https://www.pixsy.com/wp-content/uploads/2021/04/ben-sweet-2LowviVHZ-E-unsplash-1.jpeg"),
+            "pornhub": arq.pornhub("incest"),
+            "proxy": arq.proxy(),
+            "pypi": arq.pypi("python-arq"),
+            "reddit": arq.reddit("porn"),
+            "quotly": arq.quotly(message),
+            "saavn": arq.saavn("attention"),
+            "stats": arq.stats(),
+            "tmdb": arq.tmdb("flash"),
+            "torrent": arq.torrent("porn"),
+            "translate": arq.translate("hello"),
+            "urbandict": arq.urbandict("wtf"),
+            "wall": arq.wall("anime"),
+            "wiki": arq.wiki("cat"),
+            "youtube": arq.youtube("never gonna give you up")}
+
+    for key, value in funcs.items():
+        try:
+            t1 = time()
+            await value
+            t2 = time()
+            results += f"**{key.capitalize()}:** `{t2-t1}`\n"
+        except Exception:
+            results += f"**{key.capitalize()}:** `Failed`\n"
+    return results