embed.sanitizer: add fetch_metadata function

- pipfile: remove mmh3
This commit is contained in:
Luna 2018-12-05 02:42:00 -03:00
parent 13dc541fec
commit e7d3559acc
3 changed files with 29 additions and 12 deletions

View File

@ -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"

9
Pipfile.lock generated
View File

@ -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",

View File

@ -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