import { fApi } from "./helpers/index"; import chalk from "chalk-template"; import type { ContentPost, VideoContent } from "floatplane/content"; import type { ChannelOptions, SubscriptionSettings } from "./types"; import { Attachment } from "./Attachment"; import { settings } from "./helpers/index"; import { Video } from "./Video"; const removeRepeatedSentences = (postTitle: string, attachmentTitle: string) => { const separators = /(?:\s+|^)((?:[^.,;:!?-]+[\s]*[.,;:!?-]+)+)(?:\s+|$)/g; const postTitleSentences = postTitle.match(separators)?.map((sentence) => sentence.trim()); const attachmentTitleSentences = attachmentTitle.match(separators)?.map((sentence) => sentence.trim()); const uniqueAttachmentTitleSentences = attachmentTitleSentences?.filter((sentence) => !postTitleSentences?.includes(sentence)); if (uniqueAttachmentTitleSentences === undefined) return `${postTitle.trim()} - ${attachmentTitle.trim()}`.trim(); // Remove trailing separator return `${postTitle.trim()} - ${uniqueAttachmentTitleSentences.join("").trim()}`.trim().replace(/[\s]*[.,;:!?-]+[\s]*$/, ""); }; type isChannel = (post: ContentPost, video?: VideoContent) => boolean; export default class Subscription { public readonly creatorId: string; public readonly channels: SubscriptionSettings["channels"]; public readonly plan: string; constructor(subscription: SubscriptionSettings) { this.creatorId = subscription.creatorId; this.channels = subscription.channels; this.plan = subscription.plan; } public deleteOldVideos = async () => { for (const channel of this.channels) { if (channel.daysToKeepVideos !== undefined) { const ignoreBeforeTimestamp = Subscription.getIgnoreBeforeTimestamp(channel); let deletedVideos = 0; for (const video of Video.find((video) => { return video.releaseDate < ignoreBeforeTimestamp && video.channelTitle === channel.title; })) { deletedVideos++; await new Attachment({ ...video, releaseDate: new Date(video.releaseDate), }).delete(); } console.log(chalk`[{yellow ${channel.title}}] deleted {redBright ${deletedVideos}} videos older than {cyanBright ${channel.daysToKeepVideos}} days`); } } }; private static getIgnoreBeforeTimestamp = (channel: ChannelOptions) => Date.now() - (channel.daysToKeepVideos ?? 0) * 24 * 60 * 60 * 1000; private static isChannelCache: Record = {}; private static isChannelHelper = `const isChannel = (post, channelId) => (typeof post.channel !== 'string' ? post.channel.id : post.channel) === channelId`; private async *matchChannel(blogPost: ContentPost): AsyncGenerator