From e7d3559acc4a721b4b4c2fccc940e1cf08fb23ca Mon Sep 17 00:00:00 2001 From: Luna Date: Wed, 5 Dec 2018 02:42:00 -0300 Subject: [PATCH] embed.sanitizer: add fetch_metadata function - pipfile: remove mmh3 --- Pipfile | 1 - Pipfile.lock | 9 +-------- litecord/embed/sanitizer.py | 31 ++++++++++++++++++++++++++++--- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/Pipfile b/Pipfile index fa74768..9c1a0f5 100644 --- a/Pipfile +++ b/Pipfile @@ -14,7 +14,6 @@ Cerberus = "==1.2" quart = {editable = true,ref = "e23714d5",git = "https://gitlab.com/pgjones/quart"} pillow = "*" aiohttp = "==3.4.4" -mmh3 = "*" [dev-packages] pytest = "==3.10.1" diff --git a/Pipfile.lock b/Pipfile.lock index 27b6b49..29c671a 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "7e33ae70b481ecfe3b866d7ad431d532bf94644be3c65b832976b5efd6a269a8" + "sha256": "b546ad1edfe79457cb4da95e19fd17506b7adabe6a43acbc0906fb12cfda68b2" }, "pipfile-spec": 6, "requires": { @@ -304,13 +304,6 @@ ], "version": "==1.1.0" }, - "mmh3": { - "hashes": [ - "sha256:185209a217c52afe43e079e5b232d0ef0f3a262601eaaf4371326ab6dcbec508" - ], - "index": "pypi", - "version": "==2.5.1" - }, "multidict": { "hashes": [ "sha256:024b8129695a952ebd93373e45b5d341dbb87c17ce49637b34000093f243dd4f", diff --git a/litecord/embed/sanitizer.py b/litecord/embed/sanitizer.py index b69eecf..973811c 100644 --- a/litecord/embed/sanitizer.py +++ b/litecord/embed/sanitizer.py @@ -4,6 +4,8 @@ litecord.embed.sanitizer such as type: rich """ from typing import Dict, Any + +from mmh3 import hash128 from logbook import Logger from quart import current_app as app @@ -68,6 +70,24 @@ def proxify(url) -> str: ) +async def fetch_metadata(url) -> dict: + """Fetch metadata for a url.""" + parsed = url.parsed + + md_path = f'{parsed.scheme}/{parsed.netloc}/{parsed.path}' + md_base_url = app.config.MEDIA_PROXY + + proto = 'https' if app.config.IS_SSL else 'http' + + request_url = f'{proto}://{md_base_url}/meta/{md_path}' + + async with app.session.get(request_url) as resp: + if resp.status != 200: + return + + return await resp.json() + + async def fill_embed(embed: Embed) -> Embed: """Fill an embed with more information.""" embed = sanitize_embed(embed) @@ -81,8 +101,13 @@ async def fill_embed(embed: Embed) -> Embed: proxify(embed['author']['icon_url']) if path_exists(embed, 'image.url'): - # TODO: width, height - embed['image']['proxy_url'] = \ - proxify(embed['image']['url']) + image_url = embed['image']['url'] + + meta = await fetch_metadata(image_url) + embed['image']['proxy_url'] = proxify(image_url) + + if meta and meta['image']: + embed['image']['width'] = meta['width'] + embed['image']['height'] = meta['height'] return embed