Skip to content

Commit

Permalink
RSS: Add support for $content/$summary_detail/$title_detail
Browse files Browse the repository at this point in the history
  • Loading branch information
progval committed Oct 17, 2023
1 parent edb13f6 commit 04f0d70
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 1 deletion.
29 changes: 29 additions & 0 deletions plugins/RSS/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,35 @@ def format_entry(self, network, channel, feed, entry, is_announce):
isinstance(v, str)}
kwargs["feed_name"] = feed.name
kwargs.update(entry)
for (key, value) in list(kwargs.items()):
# First look for plain text
if isinstance(value, list):
for item in value:
if isinstance(item, dict) and 'value' in item and \
item.get('type') == 'text/plain':
value = item['value']
break
# Then look for HTML text or URL
if isinstance(value, list):
for item in value:
if isinstance(item, dict) and item.get('type') in \
('text/html', 'application/xhtml+xml'):
if 'value' in item:
value = utils.web.htmlToText(item['value'])
elif 'href' in item:
value = item['href']
# Then fall back to any URL
if isinstance(value, list):
for item in value:
if isinstance(item, dict) and 'href' in item:
value = item['href']
break
# Finally, as a last resort, use the value as-is
if isinstance(value, list):
for item in value:
if isinstance(item, dict) and 'value' in item:
value = item['value']
kwargs[key] = value
s = string.Template(template).safe_substitute(entry, **kwargs, date=date)
return self._normalize_entry(s)

Expand Down
93 changes: 92 additions & 1 deletion plugins/RSS/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
</rss>
"""


class MockResponse:
headers = {}
url = ''
Expand Down Expand Up @@ -359,6 +358,98 @@ def testDescription(self, mock):
self.assertRegexp('rss http://xkcd.com/rss.xml',
'On the other hand, the refractor\'s')

@mock_urllib
def testContentHtmlOnly(self, mock):
timeFastForward(1.1)
with conf.supybot.plugins.RSS.format.context('$content'):
mock._data = """
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xml:lang="en-US">
<title>Recent Commits to anope:2.0</title>
<updated>2023-10-04T16:14:39Z</updated>
<entry>
<title>title with &lt;pre&gt;HTML&lt;pre&gt;</title>
<updated>2023-10-04T16:14:39Z</updated>
<content type="html">
content with &lt;pre&gt;HTML&lt;pre&gt;
</content>
</entry>
</feed>"""
self.assertRegexp('rss https://example.org',
'content with HTML')

@mock_urllib
def testContentXhtmlOnly(self, mock):
timeFastForward(1.1)
with conf.supybot.plugins.RSS.format.context('$content'):
mock._data = """
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xml:lang="en-US">
<title>Recent Commits to anope:2.0</title>
<updated>2023-10-04T16:14:39Z</updated>
<entry>
<title>title with &lt;pre&gt;HTML&lt;pre&gt;</title>
<updated>2023-10-04T16:14:39Z</updated>
<content type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
content with <pre>XHTML<pre>
</div>
</content>
</entry>
</feed>"""
self.assertRegexp('rss https://example.org',
'content with XHTML')

@mock_urllib
def testContentHtmlAndPlaintext(self, mock):
timeFastForward(1.1)
with conf.supybot.plugins.RSS.format.context('$content'):
mock._data = """
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xml:lang="en-US">
<title>Recent Commits to anope:2.0</title>
<updated>2023-10-04T16:14:39Z</updated>
<entry>
<title>title with &lt;pre&gt;HTML&lt;pre&gt;</title>
<updated>2023-10-04T16:14:39Z</updated>
<!-- Atom spec says multiple contents is invalid, feedparser says it's not.
I like having the option, so let's make sure we support it. -->
<content type="html">
content with &lt;pre&gt;HTML&lt;pre&gt;
</content>
<content type="text">
content with plaintext
</content>
</entry>
</feed>"""
self.assertRegexp('rss https://example.org',
'content with plaintext')

@mock_urllib
def testContentPlaintextAndHtml(self, mock):
timeFastForward(1.1)
with conf.supybot.plugins.RSS.format.context('$content'):
mock._data = """
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xml:lang="en-US">
<title>Recent Commits to anope:2.0</title>
<updated>2023-10-04T16:14:39Z</updated>
<entry>
<title>title with &lt;pre&gt;HTML&lt;pre&gt;</title>
<updated>2023-10-04T16:14:39Z</updated>
<!-- Atom spec says multiple contents is invalid, feedparser says it's not.
I like having the option, so let's make sure we support it. -->
<content type="text">
content with plaintext
</content>
<content type="html">
content with &lt;pre&gt;HTML&lt;pre&gt;
</content>
</entry>
</feed>"""
self.assertRegexp('rss https://example.org',
'content with plaintext')

@mock_urllib
def testFeedAttribute(self, mock):
timeFastForward(1.1)
Expand Down

0 comments on commit 04f0d70

Please sign in to comment.