in lambda.js [93:207]
async function app(feed1, feed2=[]){
let list_of_stories = []
let already_posted = []
let cursor = "";
// ### Login and validate
const agent = new BskyAgent({ service: "https://bsky.social" });
await agent.login({
identifier: accounty,
password: passy,
});
// ### Grab the already posted stories
let recent = await agent.getAuthorFeed({
actor: accounty,
limit: 50,
cursor: cursor,
})
for (const feed of recent.data.feed) {
already_posted.push(feed.post.record.embed.external.uri)
}
const listicle = recent.data.feed.map(d => d.post.indexedAt)
const latest = new Date(Math.max.apply(null, listicle.map((e) => new Date(e))))
const diffInMs = new Date().getTime() - latest.getTime();
if ( diffInMs > lastPost) { // lastPost The last post was more than five minutes ago
// Only post world stories between 1am and 6am, min of 5 minutes between posts all the time
const getStories = async (rssfeeds) => {
const feedlist = await Promise.allSettled(rssfeeds.map(url => parser.parseURL(url)))
const shortlist = feedlist.filter(d => d.status == 'fulfilled')
const rss = shortlist.map(d => d.value)
for await (const feed of rss) {
for (const item of feed.items) {
const age = new Date().getTime() - new Date(item.date).getTime();
if (age < 28800000) { // Less than eight hours ago
if (!already_posted.includes(item.link + '?CMP=aus_bsky') && !containsMatch(["ntwnfb", "nfbntw"], item.link)) {
list_of_stories.push({
title: item.title,
link: item.link,
description: item.contentSnippet,
published : new Date(item.date).getTime()
})
}
}
}
}
}
await getStories(feed1)
if (list_of_stories.length == 0 && feed2.length > 0) {
await getStories(feed2)
}
if (list_of_stories.length > 0) {
let posting = `<p>Number of stories: ${list_of_stories.length }</p>`
let ordered = list_of_stories.sort((a, b) => a.published - b.published);
for (var i = 0; i < ordered.length; i++) {
let message = await post(agent, ordered[i]);
posting += `<p>Posted: ${ordered[i].title}</p>`
if (i >= (numberOfPosts - 1)) {
break
}
}
return posting
} else {
return "<p>No stories to post</p>"
}
} else {
return `<p>The last post was less than ${millisecondsToMinutes(lastPost)} minutes ago</p>`
}
}