Skip to content

Commit

Permalink
Fix bug in parse_news() - entries without link not being filtered out
Browse files Browse the repository at this point in the history
I don't know what I was doing, but code in question doesn't make sense.
Broken by commit 1d52e66.
  • Loading branch information
zstyblik committed Jun 6, 2024
1 parent 76cd11e commit 8a908df
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 5 deletions.
7 changes: 4 additions & 3 deletions phpbb2slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,15 @@ def parse_news(data: str, authors: List[str]) -> Dict:
feed = feedparser.parse(data)
for entry in feed["entries"]:
link = entry.pop("link", None)
title = entry.pop("title", None)
author_detail = entry.pop("author_detail", {"name": None})
if not "link" and not "title":
if not link:
# If we don't have a link, there is nothing we can do.
continue

author_detail = entry.pop("author_detail", {"name": None})
if authors and author_detail["name"] not in authors:
continue

title = entry.pop("title", "No title")
category = entry.pop("category", None)
comments_cnt = entry.pop("slash_comments", 0)
try:
Expand Down
5 changes: 3 additions & 2 deletions rss2irc.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,11 @@ def parse_news(data: str) -> Dict[str, Tuple[str, str]]:
feed = feedparser.parse(data)
for entry in feed["entries"]:
link = entry.pop("link", "")
title = entry.pop("title", "")
if not "link" and not "title":
if not link:
# If we don't have a link, there is nothing we can do.
continue

title = entry.pop("title", "No title")
category = entry.pop("category", "")
news[link] = (title, category)

Expand Down
8 changes: 8 additions & 0 deletions tests/files/phpbb-rss.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,13 @@ Does anyone else have the same situation?]]></content:encoded>
<content:encoded><![CDATA[I ordered a new something yesterday and still have not received an email. i have emailed twice somewhere and still nothing....so now trying something.]]></content:encoded>
<slash:comments>1</slash:comments>
</item>
<item>
<title>Broken entry</title>
<pubDate>Fri, 14 Feb 2020 20:50:36 +0000</pubDate>
<author>[email protected] (otherUser)</author>
<dc:creator>otherUser</dc:creator>
<content:encoded><![CDATA[I ordered a new something yesterday and still have not received an email. i have emailed twice somewhere and still nothing....so now trying something.]]></content:encoded>
<slash:comments>1</slash:comments>
</item>
</channel>
</rss>
4 changes: 4 additions & 0 deletions tests/files/rss.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@
<description>Item2 description</description>
<pubDate>Sun, 02 Feb 2020 11:26:26 -0500</pubDate>
</item>
<item>
<description>Item3 description - purposefully broken</description>
<pubDate>Sun, 02 Feb 2020 11:25:26 -0500</pubDate>
</item>
</channel>
</rss>
23 changes: 23 additions & 0 deletions tests/test_phpbb2slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,29 @@ def test_main_cache_hit(
assert len(fixture_http_server.requests) == 0


def test_parse_news():
"""Test parse_news()."""
expected_news = {
"https://phpbb.example.com/threads/something-of-something.424837/": {
"title": "Some other problem",
"category": None,
"comments_cnt": 0,
},
"https://phpbb.example.com/threads/something-not-received.424836/": {
"title": "Something not received",
"category": None,
"comments_cnt": 1,
},
}

rss_fname = os.path.join(SCRIPT_PATH, "files", "phpbb-rss.xml")
with open(rss_fname, "rb") as fhandle:
rss_data = fhandle.read().decode("utf-8")

result = phpbb2slack.parse_news(rss_data, [])
assert result == expected_news


@pytest.mark.parametrize(
"cache,expected_cache",
[
Expand Down
21 changes: 21 additions & 0 deletions tests/test_rss2irc.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,27 @@ def test_main_cache_hit(
assert sorted(output) == sorted(expected_output)


def test_parse_news():
"""Test parse_news()."""
expected_news = {
"http://www.example.com/scan.php?page=news_item&px=item1": (
"Item1",
"",
),
"http://www.example.com/scan.php?page=news_item&px=item2": (
"Item2",
"",
),
}

rss_fname = os.path.join(SCRIPT_PATH, "files", "rss.xml")
with open(rss_fname, "rb") as fhandle:
rss_data = fhandle.read().decode("utf-8")

result = rss2irc.parse_news(rss_data)
assert result == expected_news


def test_scrub_items():
"""Test scrub_items()."""
logging.basicConfig(level=logging.CRITICAL)
Expand Down

0 comments on commit 8a908df

Please sign in to comment.