Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions twootfeed/mastodon/generate_toots_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': '<strong>@</strong>', # 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:
Expand All @@ -28,7 +39,7 @@ def format_toot(toot: Dict, text_length_limit: int) -> Dict:
f"<blockquote>{html_boosted}<div><img src=\""
f"{toot['account']['avatar_static']}\" "
f"alt=\"{toot['account']['display_name']}\""
f" width= 100px\"/> "
f" width= 100px\" style=\"border-radius: 50%;\"/> "
f"<strong>{toot['account']['display_name']} </strong>"
f"{toot['content']}"
),
Expand All @@ -50,10 +61,11 @@ def format_toot(toot: Dict, text_length_limit: int) -> Dict:
f"{media.get('preview_url')}\"></a>"
)

visibility_icon = get_visibility_icon(toot)
rss_toot['htmltext'] += (
f"<br>♻ : {toot['reblogs_count']}, "
f"✰ : {toot['favourites_count']}"
f"</div></blockquote>"
f"✰ : {toot['favourites_count']}, "
f"{visibility_icon}</div></blockquote>"
)

rss_toot['text'] = BeautifulSoup(
Expand Down Expand Up @@ -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:
Expand Down
106 changes: 74 additions & 32 deletions twootfeed/tests/data.py

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions twootfeed/tests/test_mastodon_feed_generation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re

import pytest
from twootfeed.utils.feed_generation import add_noindex

from ..mastodon.generate_toots_feed import (
format_toot,
Expand Down Expand Up @@ -96,7 +97,7 @@ def test_generate_xml_query_ok() -> None:
'<lastBuildDate></lastBuildDate>',
val,
)
assert val == toot_1_feed
assert val == add_noindex(toot_1_feed)
assert code == 200


Expand All @@ -108,7 +109,7 @@ def test_generate_xml_query_limit_ok() -> None:
'<lastBuildDate></lastBuildDate>',
val,
)
assert val == toot_20_feed
assert val == add_noindex(toot_20_feed)
assert code == 200


Expand Down
4 changes: 3 additions & 1 deletion twootfeed/tests/test_twitter_feed_generation.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -100,7 +102,7 @@ def test_generate_xml_ok(get_mock: Mock, fake_tweepy_ok: Mock) -> None:
'<lastBuildDate></lastBuildDate>',
val,
)
assert val == tweet_1_feed
assert val == add_noindex(tweet_1_feed)
assert code == 200


Expand Down
8 changes: 8 additions & 0 deletions twootfeed/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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
13 changes: 8 additions & 5 deletions twootfeed/twitter/generate_tweets_feed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -19,9 +19,10 @@ def format_tweet(tweet: tweepy.tweet.Tweet) -> Dict:
'<blockquote><div><img src="{}" alt="{}'.format(
tweet.user.profile_image_url_https, tweet.user.screen_name
)
+ ' profile image"/> <strong>{} </strong>{}<br><i>'.format(
tweet.user.name, tweet.full_text
)
+ (
' profile image" style=\"border-radius: 50%;\"/> <strong>{} '
'</strong>{}<br><i>'
).format(tweet.user.name, tweet.full_text)
+ 'Source: {}</i>'.format(tweet.source)
),
}
Expand Down Expand Up @@ -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'
Expand Down
9 changes: 9 additions & 0 deletions twootfeed/utils/feed_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<channel>',
'<channel>'
'<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" '
'name="robots" content="noindex" />',
)