Skip to content

Commit

Permalink
Improve RSS diff generator (#7)
Browse files Browse the repository at this point in the history
* Improve RSS diff generator

* update dist

* improve safety

* add test entries

* fix void

* improve void entry processing

* simplify diff

* change pubDate of fifth entry

* clarify pubDate condition

* update comment

---------

Co-authored-by: femshima <[email protected]>
  • Loading branch information
phenylshima and phenylshima authored Dec 26, 2024
1 parent e0e3728 commit b924d69
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 21 deletions.
25 changes: 21 additions & 4 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19730,10 +19730,10 @@ Support boolean input list: \`true | True | TRUE | false | False | FALSE\``);
(0, command_1.issueCommand)("error", (0, utils_1.toCommandProperties)(properties), message instanceof Error ? message.toString() : message);
}
exports2.error = error;
function warning(message, properties = {}) {
function warning2(message, properties = {}) {
(0, command_1.issueCommand)("warning", (0, utils_1.toCommandProperties)(properties), message instanceof Error ? message.toString() : message);
}
exports2.warning = warning;
exports2.warning = warning2;
function notice(message, properties = {}) {
(0, command_1.issueCommand)("notice", (0, utils_1.toCommandProperties)(properties), message instanceof Error ? message.toString() : message);
}
Expand Down Expand Up @@ -33316,8 +33316,25 @@ async function tweetRssDiff(rssPaths2, twitterTokens2, safetyLimits2) {
const parser = new import_rss_parser.default();
const oldRss = await parseRss(parser, rssPaths2.oldRssPath);
const newRss = await parseRss(parser, rssPaths2.newRssPath);
const oldEntries = new Set(oldRss.items.map((item) => item.link));
const posts = newRss.items.filter((item) => !oldEntries.has(item.link)).map((entry) => `${entry.title} ${entry.link}`);
const posts = [];
for (const entry of newRss.items) {
if (!entry.link) {
core2.warning(`Entry with title "${entry.title}" has no link.`);
continue;
}
const isOldEntry = oldRss.items.some((oldEntry) => {
const diffs = [
oldEntry.title !== entry.title,
oldEntry.link !== entry.link,
oldEntry.pubDate !== entry.pubDate
].filter((d) => d).length;
return diffs <= 1;
});
if (isOldEntry) {
break;
}
posts.push(`${entry.title} ${entry.link}`);
}
if (posts.length === 0) {
core2.info("No new entry found.");
return;
Expand Down
31 changes: 26 additions & 5 deletions src/twitter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from "fs";
import { setTimeout } from "timers/promises";
import Parser from "rss-parser";
import Parser, { type Item } from "rss-parser";
import { TwitterApi, type TwitterApiTokens } from "twitter-api-v2";
import * as core from "@actions/core";

Expand All @@ -16,11 +16,32 @@ export async function tweetRssDiff(
const oldRss = await parseRss(parser, rssPaths.oldRssPath);
const newRss = await parseRss(parser, rssPaths.newRssPath);

const oldEntries = new Set(oldRss.items.map((item) => item.link));
const posts = [];
for (const entry of newRss.items) {
if (!entry.link) {
core.warning(`Entry with title "${entry.title}" has no link.`);
continue;
}

const isOldEntry = oldRss.items.some((oldEntry) => {
const diffs = [
oldEntry.title !== entry.title,
oldEntry.link !== entry.link,
oldEntry.pubDate !== entry.pubDate,
].filter((d) => d).length;

// If there are more than 1 difference, we assume it's a different entry.
return diffs <= 1;
});
if (isOldEntry) {
// Since new entries are always at the top of the feed,
// we can stop checking if we reach an old entry.
break;
}

posts.push(`${entry.title} ${entry.link}`);
}

const posts = newRss.items
.filter((item) => !oldEntries.has(item.link))
.map((entry) => `${entry.title} ${entry.link}`);
if (posts.length === 0) {
core.info("No new entry found.");
return;
Expand Down
24 changes: 18 additions & 6 deletions test/new.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
<language>en-us</language>
<atom:link href="https://www.example.com/rss.xml" rel="self" type="application/rss+xml"/>
<item>
<title>First Entry</title>
<link>http://www.example.com/first-entry</link>
<description>This is the first entry in the test RSS feed</description>
<pubDate>Fri, 25 Oct 2024 00:00:00 GMT</pubDate>
<title>Fifth Entry</title>
<link>http://www.example.com/second-entry</link>
<description>New entry that should be tweeted, even though the link is the same as second entry.</description>
<pubDate>Wed, 13 Nov 2024 00:00:00 GMT</pubDate>
</item>
<item>
<title>Fourth Entry</title>
<link>http://www.example.com/fourth-entry</link>
<description>New entry that should be tweeted</description>
<pubDate>Tue, 12 Nov 2024 00:00:00 GMT</pubDate>
</item>
<item>
<title>Second Entry</title>
Expand All @@ -20,8 +26,14 @@
<item>
<title>Third Entry</title>
<link>http://www.example.com/third-entry</link>
<description>This is the third entry in the test RSS feed</description>
<pubDate>Tue, 12 Nov 2024 00:00:00 GMT</pubDate>
<description>Since this is after the old entry, this should not be tweeted.</description>
<pubDate>Tue, 05 Nov 2024 00:00:00 GMT</pubDate>
</item>
<item>
<title>First Entry</title>
<link>http://www.example.com/first-entry</link>
<description>This is the first entry in the test RSS feed</description>
<pubDate>Fri, 25 Oct 2024 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>
12 changes: 6 additions & 6 deletions test/old.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
<link>http://www.example.com</link>
<language>en-us</language>
<atom:link href="https://www.example.com/rss.xml" rel="self" type="application/rss+xml"/>
<item>
<title>First Entry</title>
<link>http://www.example.com/first-entry</link>
<description>This is the first entry in the test RSS feed</description>
<pubDate>Fri, 25 Oct 2024 00:00:00 GMT</pubDate>
</item>
<item>
<title>Second Entry</title>
<link>http://www.example.com/second-entry</link>
<description>This is the second entry in the test RSS feed</description>
<pubDate>Fri, 01 Nov 2024 00:00:00 GMT</pubDate>
</item>
<item>
<title>First Entry</title>
<link>http://www.example.com/first-entry</link>
<description>This is the first entry in the test RSS feed</description>
<pubDate>Fri, 25 Oct 2024 00:00:00 GMT</pubDate>
</item>
</channel>
</rss>

0 comments on commit b924d69

Please sign in to comment.