stupid async

This commit is contained in:
Roy Olav Purser 2021-05-27 21:34:48 +02:00
parent f96a58c141
commit 2cbf94f1ce
Signed by: roypur
GPG Key ID: E14D26A036F21656

View File

@ -199,46 +199,61 @@ class UpstreamHandler():
try: try:
embed_url = f'https://noembed.com/embed?url={self.upstream_safe}' embed_url = f'https://noembed.com/embed?url={self.upstream_safe}'
async with self.proxy.session() as session: async with self.proxy.session() as session:
resp_embed_future = session.get(embed_url)
resp_upstream_future = session.get(self.upstream)
try: try:
resp_embed_future = session.get(embed_url)
resp_upstream_future = session.get(self.upstream)
resp_embed = await resp_embed_future resp_embed = await resp_embed_future
except Exception:
resp_embed = None
try:
resp_upstream = await resp_upstream_future resp_upstream = await resp_upstream_future
text_embed_future = resp_embed.text() except Exception:
text_upstream_future = resp_upstream.text() resp_upstream = None
text_embed_future = resp_embed.text()
text_upstream_future = resp_upstream.text()
try:
text_embed = await text_embed_future text_embed = await text_embed_future
except Exception:
text_embed = None
try:
text_upstream = await text_upstream_future text_upstream = await text_upstream_future
parser = MetaParser() except Exception:
parser.feed(text_upstream) text_upstream = None
data_raw = json.loads(text_embed) parser = MetaParser()
if isinstance(data_raw, dict): parser.feed(text_upstream)
data_new = {} data_raw = json.loads(text_embed)
data_valid = True if isinstance(data_raw, dict):
data_new["og:title"] = data_raw.get("title") data_new = {}
data_new["og:description"] = data_raw.get("author_name") data_valid = True
data_new["og:image"] = data_raw.get("thumbnail_url") data_new["og:title"] = data_raw.get("title")
data_filtered = {} data_new["og:description"] = data_raw.get("author_name")
for key in data_new: data_new["og:image"] = data_raw.get("thumbnail_url")
value = data_new.get(key) data_filtered = {}
if isinstance(value, str): for key in data_new:
data_filtered[key] = value value = data_new.get(key)
data_filtered.update(parser.meta_data) if isinstance(value, str):
data = data_filtered data_filtered[key] = value
image = data.get("og:image") data_filtered.update(parser.meta_data)
if isinstance(image, str): data = data_filtered
if self.provider == "youtube": image = data.get("og:image")
full_image = re.sub(r'\/[a-zA-Z0-9]+\.([a-zA-Z0-9]+)$', r'/maxresdefault.\1', image) if isinstance(image, str):
standard_image = re.sub(r'\/[a-zA-Z0-9]+\.([a-zA-Z0-9]+)$', r'/sddefault.\1', image) if self.provider == "youtube":
image_status_full_future = session.head(full_image) full_image = re.sub(r'\/[a-zA-Z0-9]+\.([a-zA-Z0-9]+)$', r'/maxresdefault.\1', image)
image_status_standard_future = session.head(standard_image) standard_image = re.sub(r'\/[a-zA-Z0-9]+\.([a-zA-Z0-9]+)$', r'/sddefault.\1', image)
image_status_full_future = session.head(full_image)
image_status_standard_future = session.head(standard_image)
try:
image_status_full = await image_status_full_future image_status_full = await image_status_full_future
except Exception:
image_status_full = None
try:
image_status_standard = await image_status_standard_future image_status_standard = await image_status_standard_future
if hasattr(image_status_full, "status") and (image_status_full.status < 400): except Exception:
data["og:image"] = full_image image_status_standard = None
elif hasattr(image_status_standard, "status") and (image_status_standard.status < 400): if hasattr(image_status_full, "status") and (image_status_full.status < 400):
data["og:image"] = standard_image data["og:image"] = full_image
except Exception as e: elif hasattr(image_status_standard, "status") and (image_status_standard.status < 400):
logger.info(e) data["og:image"] = standard_image
except Exception as e: except Exception as e:
logger.info(e) logger.info(e)
return data return data