mirror of
https://github.com/UiO-Gaming/ShiteMusicBot.git
synced 2026-11-05 13:56:51 +01:00
Better async performance?
and some pep8
This commit is contained in:
1 parent
f6f0301cf3
commit
dc4838af07
2 files changed
+9
-25
No files matched your search
+8
-24
@@ -1,48 +1,32 @@
|
||||
import aiohttp
|
||||
import asyncio
|
||||
from bs4 import BeautifulSoup as BS
|
||||
from bs4 import BeautifulSoup as bs4
|
||||
|
||||
session = aiohttp.ClientSession()
|
||||
|
||||
|
||||
class ThumbNailer(object):
|
||||
|
||||
def __init__(self):
|
||||
self.url = self
|
||||
|
||||
async def connection(self, url):
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async def get_img(self, url):
|
||||
async with session.get(url, timeout=30) as response:
|
||||
assert response.status == 200
|
||||
html = await response.read()
|
||||
return ThumbNailer.__parse_result(html)
|
||||
return await ThumbNailer.__parse_result(html)
|
||||
|
||||
def __parse_result(html):
|
||||
async def __parse_result(html):
|
||||
try:
|
||||
soup = BS(html, 'html.parser')
|
||||
soup = bs4(html, 'html.parser')
|
||||
img = soup.find("meta", property="twitter:image")["content"]
|
||||
return img
|
||||
except Exception as e:
|
||||
raise e
|
||||
|
||||
async def identify(self, identifier, uri):
|
||||
thumbnail_url = ""
|
||||
if "youtube" in uri:
|
||||
thumbnail_url = f"https://img.youtube.com/vi/{identifier}/0.jpg"
|
||||
return thumbnail_url
|
||||
elif "soundcloud" in uri:
|
||||
thumbnail_url = await ThumbNailer.connection(ThumbNailer, url=uri)
|
||||
thumbnail_url = await ThumbNailer.get_img(ThumbNailer, url=uri)
|
||||
return thumbnail_url
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
|
||||
def thumbnailer(self, identifier, uri):
|
||||
thumbnail_url = ""
|
||||
if "youtube" in uri:
|
||||
thumbnail_url = f"https://img.youtube.com/vi/{identifier}/0.jpg"
|
||||
return thumbnail_url
|
||||
elif "soundcloud" in uri:
|
||||
thumbnail_url = BS(requests.get(uri).text, "html.parser").find("meta", property="twitter:image")["content"]
|
||||
return thumbnail_url
|
||||
else:
|
||||
return None
|
||||
+1
-1
@@ -2,5 +2,5 @@ git+https://github.com/Rapptz/discord.py@rewrite
|
||||
git+https://github.com/Devoxin/Lavalink.py@dev
|
||||
asyncio
|
||||
pyyaml
|
||||
bs4
|
||||
BeautifulSoup4
|
||||
aiohttp<3.5.0,>=3.3.0
|
||||
Reference in new issue
Block a user