Skip to content

Commit

Permalink
Fixed sub channel format & video search tracking for #135
Browse files Browse the repository at this point in the history
  • Loading branch information
Inrixia committed Feb 9, 2023
1 parent 57f8b89 commit 53ea480
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 50 deletions.
22 changes: 12 additions & 10 deletions src/lib/Subscription.ts
Expand Up @@ -25,8 +25,8 @@ export default class Subscription {
constructor(subscription: SubscriptionSettings) {
this.creatorId = subscription.creatorId;

this.channels = Object.values(subscription.channels).map((channel) => new Channel(channel, this));
this.defaultChannel = new Channel(subscription.channels._default, this);
this.channels = subscription.channels.map((channel) => new Channel(channel, this));
this.defaultChannel = this.channels[0];

// Load/Create database
const databaseFilePath = `./db/subscriptions/${subscription.creatorId}.json`;
Expand Down Expand Up @@ -57,7 +57,7 @@ export default class Subscription {
public addVideo(video: BlogPost, stripSubchannelPrefix = true): ReturnType<Channel["addVideo"]> | null {
for (const channel of this.channels) {
// Check if the video belongs to this channel
if (channel.identifiers === false) continue;
if (!channel.identifiers) continue;
for (const identifier of channel.identifiers) {
if (typeof identifier.type !== "string")
throw new Error(
Expand Down Expand Up @@ -96,16 +96,18 @@ export default class Subscription {

process.stdout.write(`> Fetching latest videos from [${coloredTitle}]... Fetched ${videos.length} videos!`);

let videosSearched = 0;
for await (const blogPost of fApi.creator.blogPostsIterable(this.creatorId, { hasVideo: true })) {
const video = this.addVideo(blogPost, stripSubchannelPrefix);
if (video === null) continue;
// If we have found the last seen video, check if its downloaded.
// If it is then break here and return the videos we have found.
// Otherwise continue to fetch new videos up to the videosToSearch limit to ensure partially or non downloaded videos are returned.
if (!forceFullSearch && video.guid === this.lastSeenVideo.guid && (await video.isDownloaded())) break;
videos.push(video);
if (video !== null) {
// If we have found the last seen video, check if its downloaded.
// If it is then break here and return the videos we have found.
// Otherwise continue to fetch new videos up to the videosToSearch limit to ensure partially or non downloaded videos are returned.
if (!forceFullSearch && video.guid === this.lastSeenVideo.guid && (await video.isDownloaded())) break;
videos.push(video);
}
// Stop searching if we have looked through videosToSearch
if (videos.length >= videosToSearch) break;
if (videosSearched++ >= videosToSearch) break;
process.stdout.write(`\r> Fetching latest videos from [${coloredTitle}]... Fetched ${videos.length} videos!`);
}
process.stdout.write(` Skipped ${videos.length - videos.length}.\n`);
Expand Down
43 changes: 20 additions & 23 deletions src/lib/defaults.ts
Expand Up @@ -2,33 +2,30 @@ import { Resolutions, SubChannels, Settings, Args } from "./types.js";

export const defaultResolutions: Resolutions = ["360", "720", "1080", "2160"];
export const defaultSubChannels: { [key: string]: SubChannels } = {
"Tech Deals": {
_default: {
"Tech Deals": [
{
title: "Teach Deals",
skip: false,
identifiers: false,
consoleColor: "\u001b[38;5;10m",
daysToKeepVideos: -1,
},
},
"BitWit Ultra": {
_default: {
],
"BitWit Ultra": [
{
title: "BitWit Ultra",
skip: false,
identifiers: false,
consoleColor: "\u001b[38;5;105m",
daysToKeepVideos: -1,
},
},
"Linus Tech Tips": {
_default: {
],
"Linus Tech Tips": [
{
title: "Linus Tech Tips",
skip: false,
identifiers: false,
consoleColor: "\u001b[38;5;208m",
daysToKeepVideos: -1,
},
"Mac Address": {
{
title: "Mac Address",
skip: false,
identifiers: [
Expand All @@ -40,7 +37,7 @@ export const defaultSubChannels: { [key: string]: SubChannels } = {
consoleColor: "\u001b[38;5;189m",
daysToKeepVideos: -1,
},
"Floatplane Exclusive": {
{
title: "Floatplane Exclusive",
skip: false,
identifiers: [
Expand All @@ -52,7 +49,7 @@ export const defaultSubChannels: { [key: string]: SubChannels } = {
consoleColor: "\u001b[38;5;200m",
daysToKeepVideos: -1,
},
TalkLinked: {
{
title: "TalkLinked",
skip: false,
identifiers: [
Expand All @@ -64,7 +61,7 @@ export const defaultSubChannels: { [key: string]: SubChannels } = {
consoleColor: "\u001b[36m",
daysToKeepVideos: -1,
},
TechLinked: {
{
title: "TechLinked",
skip: false,
identifiers: [
Expand All @@ -76,7 +73,7 @@ export const defaultSubChannels: { [key: string]: SubChannels } = {
consoleColor: "\u001b[38;5;14m",
daysToKeepVideos: -1,
},
"TechLinked Shorts": {
{
title: "TechLinked Shorts",
skip: false,
identifiers: [
Expand All @@ -88,7 +85,7 @@ export const defaultSubChannels: { [key: string]: SubChannels } = {
consoleColor: "\u001b[38;5;14m",
daysToKeepVideos: -1,
},
TechQuickie: {
{
title: "TechQuickie",
skip: false,
identifiers: [
Expand All @@ -100,7 +97,7 @@ export const defaultSubChannels: { [key: string]: SubChannels } = {
consoleColor: "\u001b[38;5;153m",
daysToKeepVideos: -1,
},
"Theyre Just Movies": {
{
title: "Theyre Just Movies",
skip: false,
identifiers: [
Expand All @@ -127,7 +124,7 @@ export const defaultSubChannels: { [key: string]: SubChannels } = {
],
daysToKeepVideos: -1,
},
ShortCircuit: {
{
title: "ShortCircuit",
skip: false,
identifiers: [
Expand All @@ -138,7 +135,7 @@ export const defaultSubChannels: { [key: string]: SubChannels } = {
],
daysToKeepVideos: -1,
},
ChannelSuperFun: {
{
title: "ChannelSuperFun",
skip: false,
identifiers: [
Expand All @@ -150,7 +147,7 @@ export const defaultSubChannels: { [key: string]: SubChannels } = {
consoleColor: "\u001b[38;5;220m",
daysToKeepVideos: -1,
},
"The WAN Show": {
{
title: "The WAN Show",
skip: false,
identifiers: [
Expand All @@ -162,7 +159,7 @@ export const defaultSubChannels: { [key: string]: SubChannels } = {
consoleColor: "\u001b[38;5;208m",
daysToKeepVideos: -1,
},
"LMG Livestream VODs": {
{
title: "LMG Livestream VODs",
skip: false,
identifiers: [
Expand All @@ -174,7 +171,7 @@ export const defaultSubChannels: { [key: string]: SubChannels } = {
consoleColor: "\u001b[38;5;208m",
daysToKeepVideos: -1,
},
},
],
};

export const defaultArgs: Args = {
Expand Down
7 changes: 2 additions & 5 deletions src/lib/types.ts
Expand Up @@ -10,15 +10,12 @@ type ChannelIdentifier = {
export type ChannelOptions = {
title: string;
skip: boolean;
identifiers: ChannelIdentifier[] | false;
identifiers?: ChannelIdentifier[];
consoleColor?: string;
daysToKeepVideos: number;
};

export type SubChannels = {
_default: ChannelOptions;
[key: string]: ChannelOptions;
};
export type SubChannels = ChannelOptions[];

export type PlexSections = Array<{ server: string; section: string }>;
export type PlexSettings = {
Expand Down
32 changes: 20 additions & 12 deletions src/subscriptionFetching.ts
Expand Up @@ -13,19 +13,27 @@ export const fetchSubscriptions = async (): Promise<Subscription[]> =>
skip: false,
channels: defaultSubChannels[titleAlias],
};

const sub = settings.subscriptions[subscription.creator];

// Translate old configs
if (sub.channels !== undefined && !Array.isArray(sub.channels)) sub.channels = Object.values(sub.channels);

// Make sure that new subchannels from defaults are added to settings
settings.subscriptions[subscription.creator].channels = {
...defaultSubChannels[titleAlias],
...settings.subscriptions[subscription.creator].channels,
};
// If no defaultSubChannels have been created for this subscription make sure the _default channel exists regardless
if (settings.subscriptions[subscription.creator].channels._default === undefined)
settings.subscriptions[subscription.creator].channels._default = {
title: titleAlias,
skip: false,
identifiers: false,
daysToKeepVideos: -1,
};
const channelsToAdd = defaultSubChannels[titleAlias].filter((channel) => sub.channels.findIndex((chan) => chan.title === channel.title) === -1);
if (channelsToAdd.length > 0) {
sub.channels = [...sub.channels, ...channelsToAdd];
}

// If a default channel isnt specified for this in defaultSubChannels then create a default
if (settings.subscriptions[subscription.creator].channels.length === 0)
settings.subscriptions[subscription.creator].channels = [
{
title: titleAlias,
skip: false,
daysToKeepVideos: -1,
},
];
return new Subscription(settings.subscriptions[subscription.creator]);
})
.filter((subscription) => settings.subscriptions[subscription.creatorId].skip !== true);

0 comments on commit 53ea480

Please sign in to comment.