diff --git a/config/settings.py b/config/settings.py index 12aa5108..44186867 100755 --- a/config/settings.py +++ b/config/settings.py @@ -394,6 +394,11 @@ STATIC_CONTENT_AWS_S3_ENDPOINT_URL = env( "STATIC_CONTENT_AWS_S3_ENDPOINT_URL", default="https://s3.us-east-2.amazonaws.com" ) +# LinkPreview API Key +LINK_PREVIEW_API_KEY = env( + "LINK_PREVIEW_API_KEY", default="changeme" +) + # JSON configuration of how we map static content in the S3 buckets to URL paths STATIC_CONTENT_MAPPING = env( "STATIC_CONTENT_MAPPING", default="stage_static_config.json" diff --git a/news/forms.py b/news/forms.py index 2a163222..0fe0f1d1 100644 --- a/news/forms.py +++ b/news/forms.py @@ -1,4 +1,5 @@ from django import forms +from .helpers import get_link_preview_data from .models import BlogPost, Entry, Link, News, Poll, Video @@ -30,6 +31,15 @@ class LinkForm(EntryForm): model = Link fields = ["title", "publish_at", "external_url", "image"] + # Holding on this as it's a new feature Issue #437 + # def save(self, *args, commit=True, **kwargs): + # instance = super().save(*args, commit=False, **kwargs) + # if self.cleaned_data["external_url"] and not self.cleaned_data["image"]: + # link_data = get_link_preview_data(self.cleaned_data["external_url"]) + # print(link_data) + # instance.save() + # return instance + class NewsForm(EntryForm): class Meta: diff --git a/news/helpers.py b/news/helpers.py new file mode 100644 index 00000000..62159b62 --- /dev/null +++ b/news/helpers.py @@ -0,0 +1,18 @@ +import requests + +from django.conf import settings + + +def get_link_preview_data(link): + """gets the link preview json response from LinkPreview api""" + api_url = "https://api.linkpreview.net" + api_key = settings.LINK_PREVIEW_API_KEY + target = link + + # TODO: Add additional field `image_size` to help validate image https://docs.linkpreview.net/#image-processing-and-validation + response = requests.get( + api_url, + headers={'X-Linkpreview-Api-Key': api_key}, + params={'q': target}, + ) + return response.json()