Skip to content

Commit

Permalink
Add feed image and title overrides, fix image finder
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Sieradski <[email protected]>
  • Loading branch information
selfagency committed Apr 1, 2022
1 parent eb62746 commit 74f1dfe
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 50 deletions.
31 changes: 3 additions & 28 deletions .github/workflows/feedbot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ jobs:
uses: 'selfagency/slackfeedbot@dev'
with:
rss: 'https://feeds.washingtonpost.com/rss/homepage'
feed_name: 'Washington Post'
feed_image: 'https://www.washingtonpost.com/wp-stat/assets/favicons/touch-icon-iphone.png'
slack_webhook: ${{ secrets.SLACK_WEBHOOK }}
cache_dir: './slackfeedbot-cache'
continue-on-error: true
Expand Down Expand Up @@ -68,31 +70,4 @@ jobs:
slack_webhook: ${{ secrets.SLACK_WEBHOOK }}
cache_dir: './slackfeedbot-cache'
continue-on-error: true
- name: New Relic
uses: 'selfagency/slackfeedbot@dev'
with:
rss: 'https://status.newrelic.com/history.rss'
slack_webhook: ${{ secrets.SLACK_WEBHOOK }}
cache_dir: './slackfeedbot-cache'
continue-on-error: true
- name: Fastly
uses: 'selfagency/slackfeedbot@dev'
with:
rss: 'https://status.fastly.com/history.rss'
slack_webhook: ${{ secrets.SLACK_WEBHOOK }}
cache_dir: './slackfeedbot-cache'
continue-on-error: true
- name: 1Password
uses: 'selfagency/slackfeedbot@dev'
with:
rss: 'https://1password.statuspage.io/history.rss'
slack_webhook: ${{ secrets.SLACK_WEBHOOK }}
cache_dir: './slackfeedbot-cache'
continue-on-error: true
- name: DataDog
uses: 'selfagency/slackfeedbot@dev'
with:
rss: 'https://status.datadoghq.com/history.rss'
slack_webhook: ${{ secrets.SLACK_WEBHOOK }}
cache_dir: './slackfeedbot-cache'
continue-on-error: true

14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: NYT
uses: 'selfagency/[email protected].8'
uses: 'selfagency/[email protected].9'
with:
rss: 'https://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml'
slack_webhook: ${{ secrets.SLACK_WEBHOOK }}
Expand All @@ -35,7 +35,9 @@ jobs:

Required fields denoted with `*`. Must specify `cache_dir` (which requires separate use of [actions/cache](https://github.com/actions/cache) or a similar solution) _or_ `interval`.

- `rss`*: The RSS feed URL.
- `rss`\*: The RSS feed URL.
- `feed_name`: A title to override the RSS feed's own title.
- `feed_image`: An image to override the RSS feed's default feed image.
- `slack_webhook`\*: The Slack webhook URL (this can and probably should be a repository or organization secret).
- `cache_dir`\*: The folder in which to cache feed data, which prevents publishing duplicates, or _alternately_...
- `interval`\*: The number of minutes between runs of the parent workflow, as specified in the `cron` section of the `schedule` workflow trigger (may publish duplicates due to post pinning).
Expand Down Expand Up @@ -73,7 +75,7 @@ jobs:
key: feed-cache-${{ steps.generate-key.outputs.cache-key }}
restore-keys: feed-cache-
- name: NYT
uses: 'selfagency/[email protected].8'
uses: 'selfagency/[email protected].9'
with:
rss: 'https://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml'
slack_webhook: ${{ secrets.SLACK_WEBHOOK }}
Expand All @@ -94,7 +96,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: NYT
uses: 'selfagency/[email protected].8'
uses: 'selfagency/[email protected].9'
with:
rss: 'https://rss.nytimes.com/services/xml/rss/nyt/HomePage.xml'
slack_webhook: ${{ secrets.SLACK_WEBHOOK }}
Expand All @@ -113,13 +115,13 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: LAT
uses: 'selfagency/[email protected].8'
uses: 'selfagency/[email protected].9'
with:
rss: 'https://www.latimes.com/rss2.0.xml'
slack_webhook: ${{ secrets.SLACK_WEBHOOK }}
interval: 15
- name: WaPo
uses: 'selfagency/[email protected].8'
uses: 'selfagency/[email protected].9'
with:
rss: 'https://feeds.washingtonpost.com/rss/homepage'
slack_webhook: ${{ secrets.SLACK_WEBHOOK }}
Expand Down
10 changes: 8 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ branding:
color: 'orange'
inputs:
rss:
description: 'RSS Feed URL'
description: 'RSS feed URL'
required: true
feed_name:
description: 'Feed name'
required: false
feed_image:
description: 'Feed image URL'
required: false
slack_webhook:
description: 'Slack Webhook URL'
description: 'Slack webhook URL'
required: true
cache_dir:
description: 'Cache folder'
Expand Down
16 changes: 10 additions & 6 deletions dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -30101,7 +30101,7 @@ function fixResponseChunkedTransferBadEnding(request, errorCallback) {
var getFeedImg = async (rssFeed) => {
var _a, _b;
const url = new URL(rssFeed);
const host = url.hostname.replace("//status.", "//").replace("//feed.", "//").replace("//feeds.", "//").replace("//rss.", "//");
const host = url.hostname.replace(/^status\./, "").replace(/^feed\./, "").replace(/^feeds\./, "").replace(/^rss\./, "");
import_core3.default.debug(`Getting favicons for ${host}`);
let favicon;
try {
Expand All @@ -30123,7 +30123,7 @@ var converter = new import_showdown.default.Converter();
var html2txt = (0, import_html_to_text.compile)({
wordwrap: 255
});
var genPayload = async (filtered, unfiltered, rssFeed, unfurl, showDesc, showImg, showDate, showLink) => {
var genPayload = async (filtered, unfiltered, rssFeed, feedName, feedImg, unfurl, showDesc, showImg, showDate, showLink) => {
try {
const blocks = [];
filtered.forEach((item) => {
Expand All @@ -30134,7 +30134,7 @@ var genPayload = async (filtered, unfiltered, rssFeed, unfurl, showDesc, showImg
const { document: document2 } = parseHTML("<div></div>");
const desc = (0, import_striptags.default)(item.description.replace(/&gt;/g, ">").replace(/&lt;/g, "<"), ["p", "strong", "b", "em", "i", "a", "ul", "ol", "li"], " ");
const markdown = converter.makeMarkdown(desc, document2);
text += `${markdown.replace(/\\-/g, "-").replace(/\\\|/g, "|").replace(/\*{2}/g, "*").replace(/\[(.+)\]\((.+)\)/g, "<$2|$1>")}`;
text += `${markdown.replace(/\\-/g, "-").replace(/\\\|/g, "|").replace(/\*{2,}/g, "*").replace(/\[(.+)\]\((.+)\)/g, "<$2|$1>")}`;
}
}
if (item == null ? void 0 : item.title) {
Expand Down Expand Up @@ -30181,8 +30181,8 @@ var genPayload = async (filtered, unfiltered, rssFeed, unfurl, showDesc, showImg
});
const payload = {
as_user: false,
username: unfiltered.title ? html2txt(unfiltered.title) : "FeedBot",
icon_url: await getFeedImg(rssFeed),
username: feedName.length ? feedName : unfiltered.title ? html2txt(unfiltered.title) : "FeedBot",
icon_url: feedImg.length ? feedImg : await getFeedImg(rssFeed),
unfurl_links: unfurl,
unfurl_media: unfurl,
blocks
Expand Down Expand Up @@ -30247,6 +30247,8 @@ var run = async () => {
validate();
const slackWebhook = import_core7.default.getInput("slack_webhook");
const rssFeed = import_core7.default.getInput("rss");
const feedName = import_core7.default.getInput("feed_name");
const feedImg = import_core7.default.getInput("feed_image");
const cacheDir = import_core7.default.getInput("cache_dir");
const interval = import_core7.default.getInput("interval").length > 0 ? parseInt(import_core7.default.getInput("interval")) : void 0;
const unfurl = import_core7.default.getInput("unfurl").length > 0 ? import_core7.default.getBooleanInput("unfurl") : false;
Expand All @@ -30257,6 +30259,8 @@ var run = async () => {
import_core7.default.debug(`Processed inputs: ${JSON.stringify({
slackWebhook,
rssFeed,
feedName,
feedImg,
cacheDir,
interval,
unfurl,
Expand All @@ -30266,7 +30270,7 @@ var run = async () => {
})}`);
const { filtered, unfiltered, cached } = await getFeed(rssFeed, cacheDir, interval);
if (filtered.length) {
const payload = await genPayload(filtered, unfiltered, rssFeed, unfurl, showDesc, showImg, showDate, showLink);
const payload = await genPayload(filtered, unfiltered, rssFeed, feedName, feedImg, unfurl, showDesc, showImg, showDate, showLink);
await slack(payload, slackWebhook);
if (cacheDir)
await writeCache((unfiltered == null ? void 0 : unfiltered.title) || "", rssFeed, cacheDir, filtered, cached);
Expand Down
17 changes: 16 additions & 1 deletion src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const run = async () => {
// Parse inputs
const slackWebhook = core.getInput('slack_webhook');
const rssFeed = core.getInput('rss');
const feedName = core.getInput('feed_name');
const feedImg = core.getInput('feed_image');
const cacheDir = core.getInput('cache_dir');
const interval = core.getInput('interval').length > 0 ? parseInt(core.getInput('interval')) : undefined;
const unfurl = core.getInput('unfurl').length > 0 ? core.getBooleanInput('unfurl') : false;
Expand All @@ -25,6 +27,8 @@ const run = async () => {
`Processed inputs: ${JSON.stringify({
slackWebhook,
rssFeed,
feedName,
feedImg,
cacheDir,
interval,
unfurl,
Expand All @@ -39,7 +43,18 @@ const run = async () => {

if (filtered.length) {
// Generate payload
const payload = await genPayload(filtered, unfiltered, rssFeed, unfurl, showDesc, showImg, showDate, showLink);
const payload = await genPayload(
filtered,
unfiltered,
rssFeed,
feedName,
feedImg,
unfurl,
showDesc,
showImg,
showDate,
showLink
);

// Send payload to Slack
await slack(payload, slackWebhook);
Expand Down
8 changes: 4 additions & 4 deletions src/lib/feedimg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import type { Icons } from '../types.d';
const getFeedImg = async (rssFeed: string): Promise<string | undefined> => {
const url = new URL(rssFeed);
const host = url.hostname
.replace('//status.', '//')
.replace('//feed.', '//')
.replace('//feeds.', '//')
.replace('//rss.', '//');
.replace(/^status\./, '')
.replace(/^feed\./, '')
.replace(/^feeds\./, '')
.replace(/^rss\./, '');
core.debug(`Getting favicons for ${host}`);

let favicon;
Expand Down
8 changes: 5 additions & 3 deletions src/lib/payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const genPayload = async (
filtered: RssFeedItem[],
unfiltered: RssFeed,
rssFeed: string,
feedName: string,
feedImg: string,
unfurl: boolean,
showDesc: boolean,
showImg: boolean,
Expand All @@ -42,7 +44,7 @@ const genPayload = async (
text += `${markdown
.replace(/\\-/g, '-')
.replace(/\\\|/g, '|')
.replace(/\*{2}/g, '*')
.replace(/\*{2,}/g, '*')
.replace(/\[(.+)\]\((.+)\)/g, '<$2|$1>')}`;
}
}
Expand Down Expand Up @@ -102,8 +104,8 @@ const genPayload = async (

const payload = {
as_user: false,
username: unfiltered.title ? html2txt(unfiltered.title) : 'FeedBot',
icon_url: await getFeedImg(rssFeed),
username: feedName.length ? feedName : unfiltered.title ? html2txt(unfiltered.title) : 'FeedBot',
icon_url: feedImg.length ? feedImg : await getFeedImg(rssFeed),
unfurl_links: unfurl,
unfurl_media: unfurl,
blocks
Expand Down

0 comments on commit 74f1dfe

Please sign in to comment.