@@ -4,10 +4,16 @@ import {
44 dbYouTubeTable ,
55} from "../../db/schema" ;
66import { env } from "../../config" ;
7- import { dbYouTubeGetAllChannelsToTrack } from "../../db/youtube" ;
7+ import {
8+ dbYouTubeGetAllChannelsToTrack ,
9+ youtubeUpdateVideoId ,
10+ } from "../../db/youtube" ;
811import { discordGetAllGuildsTrackingChannel } from "../../db/discord" ;
912
1013import getChannelDetails from "./getChannelDetails" ;
14+ import getSinglePlaylistAndReturnVideoData , {
15+ PlaylistType ,
16+ } from "./getSinglePlaylistAndReturnVideoData" ;
1117
1218export const updates = new Map <
1319 string ,
@@ -30,10 +36,8 @@ export default async function fetchLatestUploads() {
3036 }
3137
3238 channels . data . forEach ( ( channel ) => {
33- if ( ! channel . youtubeChannelId || ! channel . latestAllId ) {
34- console . error (
35- "Channel ID or latest video ID is missing in fetchLatestUploads" ,
36- ) ;
39+ if ( ! channel . youtubeChannelId ) {
40+ console . error ( "Channel ID is missing in fetchLatestUploads" ) ;
3741
3842 return ;
3943 }
@@ -69,12 +73,14 @@ export default async function fetchLatestUploads() {
6973
7074 const data = await res . json ( ) ;
7175
76+ // TODO: Upload time (https://github.com/GalvinPython/feedr/issues/136)
7277 for ( const playlist of data . items ) {
7378 const channelId = playlist . snippet . channelId ;
7479 const videoId =
7580 playlist . snippet . thumbnails . default . url . split ( "/" ) [ 4 ] ;
7681
77- const requiresUpdate = channelDict [ channelId ] !== videoId ;
82+ const requiresUpdate =
83+ channelDict [ channelId ] . latestAllId !== videoId ;
7884
7985 console . log (
8086 "Channel ID:" ,
@@ -86,7 +92,68 @@ export default async function fetchLatestUploads() {
8692 ) ;
8793
8894 if ( requiresUpdate ) {
89- if ( ! ( await updateVideoId ( channelId , videoId ) ) ) {
95+ const [ longVideoId , shortVideoId , streamVideoId ] =
96+ await Promise . all ( [
97+ getSinglePlaylistAndReturnVideoData (
98+ channelId ,
99+ PlaylistType . Video ,
100+ ) ,
101+ getSinglePlaylistAndReturnVideoData (
102+ channelId ,
103+ PlaylistType . Short ,
104+ ) ,
105+ getSinglePlaylistAndReturnVideoData (
106+ channelId ,
107+ PlaylistType . Stream ,
108+ ) ,
109+ ] ) ;
110+
111+ if ( ! longVideoId && ! shortVideoId && ! streamVideoId ) {
112+ console . error (
113+ "No video IDs found for channel in fetchLatestUploads" ,
114+ ) ;
115+ continue ;
116+ }
117+
118+ let contentType : PlaylistType | null = null ;
119+
120+ const videoIdMap = {
121+ [ PlaylistType . Video ] : longVideoId ,
122+ [ PlaylistType . Short ] : shortVideoId ,
123+ [ PlaylistType . Stream ] : streamVideoId ,
124+ } ;
125+
126+ contentType = Object . entries ( videoIdMap ) . find (
127+ ( [ , id ] ) => id ,
128+ ) ?. [ 0 ] as PlaylistType | null ;
129+
130+ if ( contentType ) {
131+ console . log (
132+ `Updating ${ contentType } video ID for channel` ,
133+ channelId ,
134+ "to" ,
135+ videoIdMap [ contentType as keyof typeof videoIdMap ] ,
136+ ) ;
137+ } else {
138+ console . error (
139+ "No valid video ID found for channel" ,
140+ channelId ,
141+ "with video ID" ,
142+ videoId ,
143+ ) ;
144+ continue ;
145+ }
146+
147+ const updateSuccess = await youtubeUpdateVideoId (
148+ channelId ,
149+ videoId ,
150+ contentType ,
151+
152+ // Temporarily using current date for update time
153+ new Date ( ) ,
154+ ) ;
155+
156+ if ( ! updateSuccess ) {
90157 console . error (
91158 "Error updating video ID in fetchLatestUploads" ,
92159 ) ;
@@ -110,9 +177,29 @@ export default async function fetchLatestUploads() {
110177
111178 const channelInfo = await getChannelDetails ( channelId ) ;
112179
180+ console . log (
181+ discordGuildsToUpdate . data . filter (
182+ (
183+ guild ,
184+ ) : guild is typeof dbGuildYouTubeSubscriptionsTable . $inferSelect =>
185+ "youtubeChannelId" in guild &&
186+ "trackVideos" in guild &&
187+ "trackShorts" in guild &&
188+ "trackStreams" in guild ,
189+ ) ,
190+ ) ;
191+
113192 updates . set ( videoId , {
114193 channelInfo,
115- discordGuildsToUpdate,
194+ discordGuildsToUpdate : discordGuildsToUpdate . data . filter (
195+ (
196+ guild ,
197+ ) : guild is typeof dbGuildYouTubeSubscriptionsTable . $inferSelect =>
198+ "youtubeChannelId" in guild &&
199+ "trackVideos" in guild &&
200+ "trackShorts" in guild &&
201+ "trackStreams" in guild ,
202+ ) ,
116203 } ) ;
117204
118205 // console.log("Discord guilds to update:", discordGuildsToUpdate);
0 commit comments