Add image test
This commit is contained in:
parent
8f971a5c89
commit
9fd3a845e7
4 changed files with 65 additions and 6 deletions
|
|
@ -6,3 +6,9 @@ class RuleTypeChoices(TextChoices):
|
||||||
feed = "feed", _("Feed")
|
feed = "feed", _("Feed")
|
||||||
subreddit = "subreddit", _("Subreddit")
|
subreddit = "subreddit", _("Subreddit")
|
||||||
twitter = "twitter", _("Twitter")
|
twitter = "twitter", _("Twitter")
|
||||||
|
|
||||||
|
|
||||||
|
class TwitterPostTypeChoices(TextChoices):
|
||||||
|
photo = "photo", _("Poto")
|
||||||
|
video = "video", _("Video")
|
||||||
|
animated_gif = "animated_gif", _("GIF")
|
||||||
|
|
|
||||||
|
|
@ -286,7 +286,7 @@ image_mock = [
|
||||||
"retweet_count": 427,
|
"retweet_count": 427,
|
||||||
"retweeted": False,
|
"retweeted": False,
|
||||||
"source": '<a href="http://twitter.com/download/iphone" rel="nofollow">Twitter for iPhone</a>',
|
"source": '<a href="http://twitter.com/download/iphone" rel="nofollow">Twitter for iPhone</a>',
|
||||||
"text": "_ https://t.co/VjEeDrL1iA",
|
"full_text": "_ https://t.co/VjEeDrL1iA",
|
||||||
"truncated": False,
|
"truncated": False,
|
||||||
"user": {
|
"user": {
|
||||||
"contributors_enabled": False,
|
"contributors_enabled": False,
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,10 @@ from django.utils.html import format_html
|
||||||
import pytz
|
import pytz
|
||||||
|
|
||||||
from newsreader.news.collection.tests.factories import TwitterProfileFactory
|
from newsreader.news.collection.tests.factories import TwitterProfileFactory
|
||||||
from newsreader.news.collection.tests.twitter.builder.mocks import simple_mock
|
from newsreader.news.collection.tests.twitter.builder.mocks import (
|
||||||
|
image_mock,
|
||||||
|
simple_mock,
|
||||||
|
)
|
||||||
from newsreader.news.collection.twitter import TWITTER_URL, TwitterBuilder
|
from newsreader.news.collection.twitter import TWITTER_URL, TwitterBuilder
|
||||||
from newsreader.news.core.models import Post
|
from newsreader.news.core.models import Post
|
||||||
|
|
||||||
|
|
@ -67,9 +70,43 @@ class TwitterBuilderTestCase(TestCase):
|
||||||
|
|
||||||
# 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
|
# see https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/extended-entities-object
|
||||||
@skip("Not implemented")
|
|
||||||
def test_images_in_post(self):
|
def test_images_in_post(self):
|
||||||
pass
|
builder = TwitterBuilder
|
||||||
|
|
||||||
|
profile = TwitterProfileFactory(screen_name="RobertsSpaceInd")
|
||||||
|
mock_stream = MagicMock(rule=profile)
|
||||||
|
|
||||||
|
with builder((image_mock, mock_stream)) as builder:
|
||||||
|
builder.save()
|
||||||
|
|
||||||
|
posts = {post.remote_identifier: post for post in Post.objects.all()}
|
||||||
|
|
||||||
|
self.assertCountEqual(("1269039237166321664",), posts.keys())
|
||||||
|
|
||||||
|
post = posts["1269039237166321664"]
|
||||||
|
|
||||||
|
full_text = "_ https://t.co/VjEeDrL1iA"
|
||||||
|
|
||||||
|
self.assertEquals(post.rule, profile)
|
||||||
|
self.assertEquals(post.title, full_text)
|
||||||
|
|
||||||
|
self.assertEquals(post.author, "RobertsSpaceInd")
|
||||||
|
self.assertEquals(
|
||||||
|
post.url, f"{TWITTER_URL}/RobertsSpaceInd/1269039237166321664"
|
||||||
|
)
|
||||||
|
self.assertEquals(
|
||||||
|
post.publication_date, pytz.utc.localize(datetime(2020, 6, 5, 22, 51, 46))
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertIn(full_text, post.body)
|
||||||
|
self.assertIn(
|
||||||
|
f"<div><img alt='1269039233072689152' src='https://pbs.twimg.com/media/EZyIdXVU8AACPCz.jpg' loading='lazy' /></div>",
|
||||||
|
post.body,
|
||||||
|
)
|
||||||
|
self.assertIn(
|
||||||
|
f"<div><img alt='1269039233068527618' src='https://pbs.twimg.com/media/EZyIdXUVcAI3Cju.jpg' loading='lazy' /></div>",
|
||||||
|
post.body,
|
||||||
|
)
|
||||||
|
|
||||||
@skip("Not implemented")
|
@skip("Not implemented")
|
||||||
def test_videos_in_post(self):
|
def test_videos_in_post(self):
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ from django.utils.html import format_html
|
||||||
import pytz
|
import pytz
|
||||||
|
|
||||||
from newsreader.news.collection.base import Builder, Client, Collector, Stream
|
from newsreader.news.collection.base import Builder, Client, Collector, Stream
|
||||||
from newsreader.news.collection.choices import RuleTypeChoices
|
from newsreader.news.collection.choices import RuleTypeChoices, TwitterPostTypeChoices
|
||||||
from newsreader.news.core.models import Post
|
from newsreader.news.core.models import Post
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -37,11 +37,27 @@ class TwitterBuilder(Builder):
|
||||||
publication_date = pytz.utc.localize(
|
publication_date = pytz.utc.localize(
|
||||||
datetime.strptime(post["created_at"], "%a %b %d %H:%M:%S +0000 %Y")
|
datetime.strptime(post["created_at"], "%a %b %d %H:%M:%S +0000 %Y")
|
||||||
)
|
)
|
||||||
|
body = ""
|
||||||
|
|
||||||
|
if "extended_entities" in post:
|
||||||
|
media_entities = post["extended_entities"]["media"]
|
||||||
|
|
||||||
|
for media_entity in media_entities:
|
||||||
|
media_type = media_entity["type"]
|
||||||
|
media_url = media_entity["media_url_https"]
|
||||||
|
title = media_entity["id_str"]
|
||||||
|
|
||||||
|
if media_type == TwitterPostTypeChoices.photo:
|
||||||
|
html_fragment = f"<div><img alt='{title}' src='{media_url}' loading='lazy' /></div>"
|
||||||
|
|
||||||
|
body += format_html(html_fragment)
|
||||||
|
|
||||||
|
body += format_html(post["full_text"])
|
||||||
|
|
||||||
data = {
|
data = {
|
||||||
"remote_identifier": remote_identifier,
|
"remote_identifier": remote_identifier,
|
||||||
"title": truncatechars(post["full_text"], 40),
|
"title": truncatechars(post["full_text"], 40),
|
||||||
"body": format_html(post["full_text"]),
|
"body": body,
|
||||||
"author": rule.screen_name,
|
"author": rule.screen_name,
|
||||||
"publication_date": publication_date,
|
"publication_date": publication_date,
|
||||||
"url": (f"{TWITTER_URL}/{rule.screen_name}/{remote_identifier}"),
|
"url": (f"{TWITTER_URL}/{rule.screen_name}/{remote_identifier}"),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue