diff --git a/twootfeed/mastodon/generate_toots_feed.py b/twootfeed/mastodon/generate_toots_feed.py index 84eb248..547ddfd 100644 --- a/twootfeed/mastodon/generate_toots_feed.py +++ b/twootfeed/mastodon/generate_toots_feed.py @@ -4,7 +4,18 @@ import pytz from bs4 import BeautifulSoup from mastodon import Mastodon -from twootfeed.utils.feed_generation import generate_feed +from twootfeed.utils.feed_generation import add_noindex, generate_feed + +TOOT_VISIBILITY = { + 'public': '🌐', # Visible to everyone, shown in public timelines. + 'unlisted': '🔓', # Visible to public, but not included in public timelines. # noqa + 'private': '🔒', # Visible to followers only, and to any mentioned users. + 'direct': '@', # Visible only to mentioned users. +} + + +def get_visibility_icon(toot: Dict) -> str: + return f"{TOOT_VISIBILITY[toot['visibility']]}" def format_toot(toot: Dict, text_length_limit: int) -> Dict: @@ -28,7 +39,7 @@ def format_toot(toot: Dict, text_length_limit: int) -> Dict: f"
{html_boosted}
" + f" width= 100px\" style=\"border-radius: 50%;\"/> " f"{toot['account']['display_name']} " f"{toot['content']}" ), @@ -50,10 +61,11 @@ def format_toot(toot: Dict, text_length_limit: int) -> Dict: f"{media.get('preview_url')}\">" ) + visibility_icon = get_visibility_icon(toot) rss_toot['htmltext'] += ( f"
♻ : {toot['reblogs_count']}, " - f"✰ : {toot['favourites_count']}" - f"
" + f"✰ : {toot['favourites_count']}, " + f"{visibility_icon}" ) rss_toot['text'] = BeautifulSoup( @@ -173,8 +185,10 @@ def generate_xml( feed_desc = param['feed']['author_name'] + ' home timeline.' else: raise Exception('Invalid target') - xml = generate_mastodon_feed( - result, param, feed_title, feed_link, feed_desc + xml = add_noindex( + generate_mastodon_feed( + result, param, feed_title, feed_link, feed_desc + ) ) code = 200 else: diff --git a/twootfeed/tests/data.py b/twootfeed/tests/data.py index 314a544..85d69c9 100644 --- a/twootfeed/tests/data.py +++ b/twootfeed/tests/data.py @@ -80,7 +80,7 @@ 'created_at': datetime(2017, 4, 6, 15, 24, 15, tzinfo=pytz.UTC), 'tweet_url': 'https://twitter.com/TwitterDev/status/850006245121695744', 'htmltext': '
' + 'alt="TwitterDev profile image" style="border-radius: 50%;"/> ' 'Twitter Dev We are sharing our vision for ' 'the future of the Twitter API
' 'Source:
Location: None
' @@ -100,7 +100,8 @@ 'future of the Twitter API' 'https://twitter.com/TwitterDev/status/850006245121695744' '<blockquote><div><img src="" ' - 'alt="TwitterDev profile image"/> <strong>Twitter Dev ' + 'alt="TwitterDev profile image" style="border-radius: 50%;"/> ' + '<strong>Twitter Dev ' '</strong>We are sharing our vision for the future of the Twitter ' 'API<br><i>Source: </i><br><i>Location: ' 'None</i><br> ♻ : 0, ♥ : 0</div></blockquote>' @@ -126,7 +127,8 @@ 'future of the Twitter API' 'https://twitter.com/TwitterDev/status/850006245121695744' '<blockquote><div><img src="" ' - 'alt="TwitterDev profile image"/> <strong>Twitter Dev ' + 'alt="TwitterDev profile image" style="border-radius: 50%;"/> ' + '<strong>Twitter Dev ' '</strong>We are sharing our vision for the future of the ' 'Twitter ' 'API<br><i>Source: </i><br><i>Location: ' @@ -336,7 +338,7 @@ 'tweet_url': 'https://twitter.com/UserA/status/1111111111111111111', 'htmltext': '
' '
' - 'UserA profile image ' # noqa + 'UserA profile image ' # noqa 'User A ' 'tweet #test ' # noqa 'cc ' @@ -358,7 +360,20 @@ '' 'Twitter Search Feed: "test"' 'https://twitter.com/search?q=test' - 'Twitter search results.' # noqa + 'Twitter search results.' + 'fr' + '' +) + +empty_feed_with_no_index = ( + '\n' + '' + '' + '' + 'Twitter Search Feed: "test"' + 'https://twitter.com/search?q=test' + 'Twitter search results.' 'fr' '' ) @@ -421,10 +436,10 @@ 'url': 'https://mastodon.social/@UserD/111111111111111111', 'htmltext': '
' '
' - 'User ' # noqa + 'User ' # noqa 'User ' '

What\'s New in #python today?

' # noqa - '
♻ : 0, ✰ : 0
', + '
♻ : 0, ✰ : 0, 🌐
', 'text': 'What\'s New in #python today?', } toot_1_feed = ( @@ -441,11 +456,12 @@ 'https://mastodon.social/@UserD/111111111111111111' '<blockquote><div><img src="https://files.' 'mastodon.social/accounts/avatars/000/000/000/original/DxDxDxDxDxDxDxDx' - '.jpg" alt="User" width= 100px"/> <strong>User </strong>' + '.jpg" alt="User" width= 100px" style="border-radius: 50%;"/> ' + '<strong>User </strong>' ';<p>What\'s New in <a href="https://linuxjobs.social/tags/' 'python" class="mention hashtag" rel="nofollow noopener" target="_blank' '">#<span>python</span></a> today?</p><br' - '>♻ : 0, ✰ : 0</div></blockquote>' + '>♻ : 0, ✰ : 0, 🌐</div></blockquote>
' 'Thu, 25 Oct 2018 16:16:42 +0200' '' '' @@ -466,11 +482,12 @@ 'https://mastodon.social/@UserD/111111111111111111' '<blockquote><div><img src="https://files' '.mastodon.social/accounts/avatars/000/000/000/original/' - 'DxDxDxDxDxDxDxDx.jpg" alt="User" width= 100px"/> <strong>' + 'DxDxDxDxDxDxDxDx.jpg" alt="User" width= 100px" style="border-radius: ' + '50%;"/> <strong>' 'User </strong><p>What\'s New in <a href="https://' 'linuxjobs.social/tags/python" class="mention hashtag" rel="nofollow' ' noopener" target="_blank">#<span>python</span></a' - '> today?</p><br>♻ : 0, ✰ : 0</div></' + '> today?</p><br>♻ : 0, ✰ : 0, 🌐</div></' 'blockquote>' 'Thu, 25 Oct 2018 16:16:42 +0200' '' @@ -483,6 +500,8 @@ '\n' '' '' + '' 'Mastodon Feed: Bookmarks' 'https://mastodon.social/web/bookmarks<' 'description> bookmarks toots.
' @@ -493,11 +512,12 @@ 'https://mastodon.social/@UserD/111111111111111111' '<blockquote><div><img src="https://files.' 'mastodon.social/accounts/avatars/000/000/000/original/DxDxDxDxDxDxDxDx' - '.jpg" alt="User" width= 100px"/> <strong>User </strong>' + '.jpg" alt="User" width= 100px" style="border-radius: 50%;"/> ' + '<strong>User </strong>' ';<p>What\'s New in <a href="https://linuxjobs.social/tags/' 'python" class="mention hashtag" rel="nofollow noopener" target="_blank' '">#<span>python</span></a> today?</p><br' - '>♻ : 0, ✰ : 0</div></blockquote>' + '>♻ : 0, ✰ : 0, 🌐</div></blockquote>' 'Thu, 25 Oct 2018 16:16:42 +0200' '' '' @@ -507,6 +527,8 @@ '\n' '' '' + '' 'Mastodon Feed: Bookmarks' 'https://mastodon.social/web/bookmarks<' 'description> bookmarks toots.' @@ -518,12 +540,13 @@ 'https://mastodon.social/@UserD/111111111111111111' '<blockquote><div><img src="https://files.' 'mastodon.social/accounts/avatars/000/000/000/original/' - 'DxDxDxDxDxDxDxDx.jpg" alt="User" width= 100px"/> <strong>' + 'DxDxDxDxDxDxDxDx.jpg" alt="User" width= 100px" style="border-radius: ' + '50%;"/> <strong>' 'User </strong><p>What\'s New in <a href="https://' 'linuxjobs.social/tags/python" class="mention hashtag" rel="nofollow ' 'noopener" target="_blank">#<span>python</span></a&' - 'gt; today?</p><br>♻ : 0, ✰ : 0</div></blockquote' - '>' + 'gt; today?</p><br>♻ : 0, ✰ : 0, 🌐</div></' + 'blockquote>' 'Thu, 25 Oct 2018 16:16:42 +0200' '' ) @@ -535,6 +558,8 @@ '\n' '' '' + '' 'Mastodon Feed: Favourites' 'https://mastodon.social/web/favourites<' 'description> favourites toots.' @@ -545,11 +570,12 @@ 'https://mastodon.social/@UserD/111111111111111111' '<blockquote><div><img src="https://files.' 'mastodon.social/accounts/avatars/000/000/000/original/DxDxDxDxDxDxDxDx' - '.jpg" alt="User" width= 100px"/> <strong>User </strong>' + '.jpg" alt="User" width= 100px" style="border-radius: 50%;"/> ' + '<strong>User </strong>' ';<p>What\'s New in <a href="https://linuxjobs.social/tags/' 'python" class="mention hashtag" rel="nofollow noopener" target="_blank' '">#<span>python</span></a> today?</p><br' - '>♻ : 0, ✰ : 0</div></blockquote>' + '>♻ : 0, ✰ : 0, 🌐</div></blockquote>' 'Thu, 25 Oct 2018 16:16:42 +0200' '' '' @@ -559,6 +585,8 @@ '\n' '' '' + '' 'Mastodon Feed: Favourites' 'https://mastodon.social/web/favourites<' 'description> favourites toots.' @@ -570,12 +598,13 @@ 'https://mastodon.social/@UserD/111111111111111111' '<blockquote><div><img src="https://files.' 'mastodon.social/accounts/avatars/000/000/000/original/' - 'DxDxDxDxDxDxDxDx.jpg" alt="User" width= 100px"/> <strong>' + 'DxDxDxDxDxDxDxDx.jpg" alt="User" width= 100px" style="border-radius: ' + '50%;"/> <strong>' 'User </strong><p>What\'s New in <a href="https://' 'linuxjobs.social/tags/python" class="mention hashtag" rel="nofollow ' 'noopener" target="_blank">#<span>python</span></a&' - 'gt; today?</p><br>♻ : 0, ✰ : 0</div></blockquote' - '>' + 'gt; today?</p><br>♻ : 0, ✰ : 0, 🌐</div></' + 'blockquote>' 'Thu, 25 Oct 2018 16:16:42 +0200' '' ) @@ -675,12 +704,12 @@ 'url': 'https://mastodon.social/@UserD/111111111111111111', 'htmltext': '
' '
' - 'User ' # noqa + 'User ' # noqa 'User ' '

This is a

' # noqa 'Source: Twidere for Android
' '' # noqa - '
♻ : 0, ✰ : 0
', + '
♻ : 0, ✰ : 0, 🌐
', 'text': 'This is a ... ', } @@ -688,6 +717,8 @@ '\n' '' '' + '' 'Mastodon Feed: search "test"' 'https://mastodon.social/web/timelines/tag/test<' 'description>Mastodon generated feed from search.' @@ -701,6 +732,8 @@ '\n' '' '' + '' 'Mastodon Feed: search "test"' 'https://mastodon.social/web/search/<' 'description>Mastodon generated feed from search.' @@ -713,6 +746,8 @@ '\n' '' '' + '' 'Mastodon Feed: search "test"' 'https://mastodon.social/web/search/<' 'description>Mastodon generated feed from search.' @@ -723,11 +758,12 @@ 'https://mastodon.social/@UserD/111111111111111111' '<blockquote><div><img src="https://files.' 'mastodon.social/accounts/avatars/000/000/000/original/DxDxDxDxDxDxDxDx' - '.jpg" alt="User" width= 100px"/> <strong>User </strong>' + '.jpg" alt="User" width= 100px" style="border-radius: 50%;"/> ' + '<strong>User </strong>' ';<p>What\'s New in <a href="https://linuxjobs.social/tags/' 'python" class="mention hashtag" rel="nofollow noopener" target="_blank' '">#<span>python</span></a> today?</p><br' - '>♻ : 0, ✰ : 0</div></blockquote>' + '>♻ : 0, ✰ : 0, 🌐</div></blockquote>' 'Thu, 25 Oct 2018 16:16:42 +0200' '' '' @@ -839,10 +875,10 @@ 'htmltext': '
' '
Boosted by User A:
' '
' - 'User ' # noqa + 'User ' # noqa 'User ' '

What\'s New in #python today?

' # noqa - '
♻ : 0, ✰ : 0
', + '
♻ : 0, ✰ : 0, 🌐', 'text': 'What\'s New in #python today?', } @@ -850,6 +886,8 @@ '\n' '' '' + '' 'Mastodon Feed: Home Timeline' 'https://mastodon.social<' 'description> home timeline.' @@ -860,11 +898,12 @@ 'https://mastodon.social/@UserD/111111111111111111' '<blockquote><div><img src="https://files.' 'mastodon.social/accounts/avatars/000/000/000/original/DxDxDxDxDxDxDxDx' - '.jpg" alt="User" width= 100px"/> <strong>User </strong>' + '.jpg" alt="User" width= 100px" style="border-radius: 50%;"/> ' + '<strong>User </strong>' ';<p>What\'s New in <a href="https://linuxjobs.social/tags/' 'python" class="mention hashtag" rel="nofollow noopener" target="_blank' '">#<span>python</span></a> today?</p><br' - '>♻ : 0, ✰ : 0</div></blockquote>' + '>♻ : 0, ✰ : 0, 🌐</div></blockquote>' 'Thu, 25 Oct 2018 16:16:42 +0200' '' '' @@ -875,6 +914,8 @@ '\n' '' '' + '' 'Mastodon Feed: Home Timeline' 'https://mastodon.social<' 'description> home timeline.' @@ -886,12 +927,13 @@ 'https://mastodon.social/@UserD/111111111111111111' '<blockquote><div><img src="https://files.' 'mastodon.social/accounts/avatars/000/000/000/original/' - 'DxDxDxDxDxDxDxDx.jpg" alt="User" width= 100px"/> <strong>' + 'DxDxDxDxDxDxDxDx.jpg" alt="User" width= 100px" style="border-radius: ' + '50%;"/> <strong>' 'User </strong><p>What\'s New in <a href="https://' 'linuxjobs.social/tags/python" class="mention hashtag" rel="nofollow ' 'noopener" target="_blank">#<span>python</span></a&' - 'gt; today?</p><br>♻ : 0, ✰ : 0</div></blockquote' - '>' + 'gt; today?</p><br>♻ : 0, ✰ : 0, 🌐</div></' + 'blockquote>' 'Thu, 25 Oct 2018 16:16:42 +0200' '' ) diff --git a/twootfeed/tests/test_mastodon_feed_generation.py b/twootfeed/tests/test_mastodon_feed_generation.py index 2fd1a21..b92709e 100644 --- a/twootfeed/tests/test_mastodon_feed_generation.py +++ b/twootfeed/tests/test_mastodon_feed_generation.py @@ -1,6 +1,7 @@ import re import pytest +from twootfeed.utils.feed_generation import add_noindex from ..mastodon.generate_toots_feed import ( format_toot, @@ -96,7 +97,7 @@ def test_generate_xml_query_ok() -> None: '', val, ) - assert val == toot_1_feed + assert val == add_noindex(toot_1_feed) assert code == 200 @@ -108,7 +109,7 @@ def test_generate_xml_query_limit_ok() -> None: '', val, ) - assert val == toot_20_feed + assert val == add_noindex(toot_20_feed) assert code == 200 diff --git a/twootfeed/tests/test_twitter_feed_generation.py b/twootfeed/tests/test_twitter_feed_generation.py index 4262c55..0d5f7e4 100644 --- a/twootfeed/tests/test_twitter_feed_generation.py +++ b/twootfeed/tests/test_twitter_feed_generation.py @@ -1,6 +1,8 @@ import re from unittest.mock import Mock, patch +from twootfeed.utils.feed_generation import add_noindex + from ..twitter.generate_tweets_feed import ( format_tweet, generate_twitter_feed, @@ -100,7 +102,7 @@ def test_generate_xml_ok(get_mock: Mock, fake_tweepy_ok: Mock) -> None: '', val, ) - assert val == tweet_1_feed + assert val == add_noindex(tweet_1_feed) assert code == 200 diff --git a/twootfeed/tests/test_utils.py b/twootfeed/tests/test_utils.py new file mode 100644 index 0000000..df1c28f --- /dev/null +++ b/twootfeed/tests/test_utils.py @@ -0,0 +1,8 @@ +from twootfeed.utils.feed_generation import add_noindex + +from .data import empty_feed, empty_feed_with_no_index + + +class TestAddNoIndexMeta: + def test_it_returns_feed_with_no_index_meta(self) -> None: + assert add_noindex(empty_feed) == empty_feed_with_no_index diff --git a/twootfeed/twitter/generate_tweets_feed.py b/twootfeed/twitter/generate_tweets_feed.py index 1ad4321..05c84e5 100644 --- a/twootfeed/twitter/generate_tweets_feed.py +++ b/twootfeed/twitter/generate_tweets_feed.py @@ -3,7 +3,7 @@ import pytz import tweepy -from twootfeed.utils.feed_generation import generate_feed +from twootfeed.utils.feed_generation import add_noindex, generate_feed def format_tweet(tweet: tweepy.tweet.Tweet) -> Dict: @@ -19,9 +19,10 @@ def format_tweet(tweet: tweepy.tweet.Tweet) -> Dict: '
{}'.format(
                 tweet.user.profile_image_url_https, tweet.user.screen_name
             )
-            + ' profile image {} {}
'.format( - tweet.user.name, tweet.full_text - ) + + ( + ' profile image" style=\"border-radius: 50%;\"/> {} ' + '{}
' + ).format(tweet.user.name, tweet.full_text) + 'Source: {}'.format(tweet.source) ), } @@ -147,7 +148,9 @@ def generate_xml( api: tweepy.API, query_feed: str, param: Dict ) -> Tuple[str, int]: if api: - xml = generate_twitter_feed(api.search_tweets, query_feed, param) + xml = add_noindex( + generate_twitter_feed(api.search_tweets, query_feed, param) + ) code = 200 else: xml = 'error - Twitter parameters not defined' diff --git a/twootfeed/utils/feed_generation.py b/twootfeed/utils/feed_generation.py index dad642e..46a0395 100644 --- a/twootfeed/utils/feed_generation.py +++ b/twootfeed/utils/feed_generation.py @@ -17,3 +17,12 @@ def generate_feed( author_name=param['feed']['author_name'], feed_url=param['feed']['feed_url'], ) + + +def add_noindex(xml_str: str) -> str: + return xml_str.replace( + '', + '' + '', + )