WIP: video posts

This commit is contained in:
Sonny Bakker 2020-08-09 22:43:39 +02:00
parent 9fd3a845e7
commit bafb188636
2 changed files with 75 additions and 5 deletions

View file

@ -12,6 +12,7 @@ from newsreader.news.collection.tests.factories import TwitterProfileFactory
from newsreader.news.collection.tests.twitter.builder.mocks import (
image_mock,
simple_mock,
video_mock,
)
from newsreader.news.collection.twitter import TWITTER_URL, TwitterBuilder
from newsreader.news.core.models import Post
@ -68,7 +69,7 @@ class TwitterBuilderTestCase(TestCase):
post.publication_date, pytz.utc.localize(datetime(2020, 7, 29, 19, 1, 47))
)
# Note that only one media type can be uploaded to an Tweet
# note that only one media type can be uploaded to an Tweet
# see https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/extended-entities-object
def test_images_in_post(self):
builder = TwitterBuilder
@ -108,9 +109,46 @@ class TwitterBuilderTestCase(TestCase):
post.body,
)
@skip("Not implemented")
def test_videos_in_post(self):
pass
builder = TwitterBuilder
profile = TwitterProfileFactory(screen_name="RobertsSpaceInd")
mock_stream = MagicMock(rule=profile)
with builder((video_mock, mock_stream)) as builder:
builder.save()
posts = {post.remote_identifier: post for post in Post.objects.all()}
self.assertCountEqual(
("1291080532361527296", "1291079386821582849"), posts.keys()
)
post = posts["1291080532361527296"]
full_text = (
"Small enough to access hard-to-reach ore deposits, but with enough"
"power to get through the tough jobs, Greycat\u2019s ROC perfectly"
"complements any mining operation. \n\nDetails:"
"https://t.co/2aH7qdOfSk https://t.co/mZ8CAuq3SH"
)
self.assertEquals(post.rule, profile)
self.assertEquals(post.title, truncatechars(full_text, 40))
self.assertEquals(post.author, "RobertsSpaceInd")
self.assertEquals(
post.url, f"{TWITTER_URL}/RobertsSpaceInd/1291080532361527296"
)
self.assertEquals(
post.publication_date, pytz.utc.localize(datetime(2020, 8, 5, 18, 36, 0))
)
self.assertIn(full_text, post.body)
self.assertIn(
"""<div><video controls muted><source src="https://video.twimg.com/amplify_video/1291074294747770880/vid/1280x720/J05_p6q74ZUN4csg.mp4?tag=13" type="video/mp4" /></video></div>""",
post.body,
)
@skip("Not implemented")
def test_GIFs_in_post(self):

View file

@ -48,9 +48,41 @@ class TwitterBuilder(Builder):
title = media_entity["id_str"]
if media_type == TwitterPostTypeChoices.photo:
html_fragment = f"<div><img alt='{title}' src='{media_url}' loading='lazy' /></div>"
html_fragment = format_html(
"<div><img alt='{title}' src='{media_url}' loading='lazy' /></div>",
title=title,
media_url=media_url,
)
body += format_html(html_fragment)
body += html_fragment
elif media_type == TwitterPostTypeChoices.video:
meta_data = media_entity["video_info"]
# TODO catch case where bitrates are not defined or no videos
video = next(
iter(
sorted(
(
video
for video in meta_data["variants"]
if "bitrate" in video
),
reverse=True,
key=lambda video: video["bitrate"],
)
)
)
content_type = video["content_type"]
url = video["url"]
html_fragment = format_html(
"""<div><video controls muted><source src="{url}" type="{content_type}" /></video></div>""",
url=url,
content_type=content_type,
)
body += html_fragment
body += format_html(post["full_text"])