Initial tests & video/image mockup

This commit is contained in:
Sonny 2020-07-18 22:13:27 +02:00
parent 6b7ac32dfa
commit e0744d3a68
3 changed files with 37 additions and 10 deletions

View file

@ -42,6 +42,8 @@ REDDIT_API_URL = "https://oauth.reddit.com"
RATE_LIMIT = 60 RATE_LIMIT = 60
RATE_LIMIT_DURATION = timedelta(seconds=60) RATE_LIMIT_DURATION = timedelta(seconds=60)
REDDIT_IMAGE_EXTENSIONS = (".jpg", ".png", ".gif")
def get_reddit_authorization_url(user): def get_reddit_authorization_url(user):
state = str(uuid4()) state = str(uuid4())
@ -120,7 +122,9 @@ class RedditBuilder(Builder):
remote_identifier = post["data"]["id"] remote_identifier = post["data"]["id"]
title = truncate_text(Post, "title", post["data"]["title"]) title = truncate_text(Post, "title", post["data"]["title"])
author = truncate_text(Post, "author", post["data"]["author"]) author = truncate_text(Post, "author", post["data"]["author"])
url_fragment = f"{post['data']['permalink']}" post_url_fragment = post["data"]["permalink"]
direct_url = post["data"]["url"]
is_text_post = post["data"]["is_self"]
if remote_identifier in results: if remote_identifier in results:
continue continue
@ -139,6 +143,13 @@ class RedditBuilder(Builder):
else "" else ""
) )
if not is_text_post and direct_url.endswith(REDDIT_IMAGE_EXTENSIONS):
body = f"<div><img alt='{title}' src='{direct_url}' loading='lazy' /></div>"
elif not is_text_post and post["data"]["is_video"]:
video_info = post["data"]["secure_media"]["reddit_video"]
body = f"<div><video controls muted><source src='{video_info['fallback_url']}' type='video/mp4' /></video></div>"
try: try:
parsed_date = datetime.fromtimestamp(post["data"]["created_utc"]) parsed_date = datetime.fromtimestamp(post["data"]["created_utc"])
created_date = pytz.utc.localize(parsed_date) created_date = pytz.utc.localize(parsed_date)
@ -151,7 +162,7 @@ class RedditBuilder(Builder):
"title": title, "title": title,
"body": body, "body": body,
"author": author, "author": author,
"url": f"{REDDIT_URL}{url_fragment}", "url": f"{REDDIT_URL}{post_url_fragment}",
"publication_date": created_date, "publication_date": created_date,
"rule": rule, "rule": rule,
} }

View file

@ -1,4 +1,5 @@
from datetime import datetime from datetime import datetime
from unittest import skip
from unittest.mock import MagicMock from unittest.mock import MagicMock
from django.test import TestCase from django.test import TestCase
@ -219,3 +220,23 @@ class RedditBuilderTestCase(TestCase):
duplicate_post.title, duplicate_post.title,
"Linux Experiences/Rants or Education/Certifications thread - July 06, 2020", "Linux Experiences/Rants or Education/Certifications thread - July 06, 2020",
) )
@skip("Not implemented")
def test_image_post(self):
pass
@skip("Not implemented")
def test_external_image_post(self):
pass
@skip("Not implemented")
def test_video_post(self):
pass
@skip("Not implemented")
def test_external_video_post(self):
pass
@skip("Not implemented")
def test_link_only_post(self):
pass

View file

@ -68,14 +68,9 @@
margin: 20px 0 5px 0; margin: 20px 0 5px 0;
} }
& img { & img, video {
padding: 10px 10px 30px 10px; padding: 10px 0;
max-width: 100%;
max-width: 70%;
width: inherit;
height: 100%;
align-self: center;
} }
} }