ソースを参照

feat: Extract YouTube video IDs and generate thumbnails

This commit modifies the `fetch-rss` function to extract YouTube video IDs from article URLs. It then generates thumbnail URLs using the format `https://img.youtube.com/vi/{VIDEO_ID}/hqdefault.jpg` and saves them in the `image_url` field for YouTube articles. This addresses the issue of missing thumbnails for YouTube feeds.
gpt-engineer-app[bot] 4 ヶ月 前
コミット
f2edf89837
1 ファイル変更31 行追加0 行削除
  1. 31 0
      supabase/functions/fetch-rss/index.ts

+ 31 - 0
supabase/functions/fetch-rss/index.ts

@@ -43,6 +43,29 @@ function extractAttribute(xml: string, tagName: string, attributeName: string):
   return match ? match[1] : '';
 }
 
+// Helper function to extract YouTube video ID from URL
+function getYouTubeVideoId(url: string): string | null {
+  if (!url) return null;
+  
+  const patterns = [
+    /(?:youtube\.com\/watch\?v=|youtu\.be\/|youtube\.com\/embed\/)([a-zA-Z0-9_-]{11})/,
+    /youtube\.com\/.*[?&]v=([a-zA-Z0-9_-]{11})/
+  ];
+  
+  for (const pattern of patterns) {
+    const match = url.match(pattern);
+    if (match) return match[1];
+  }
+  
+  return null;
+}
+
+// Helper function to generate YouTube thumbnail URL
+function generateYouTubeThumbnail(videoUrl: string): string | null {
+  const videoId = getYouTubeVideoId(videoUrl);
+  return videoId ? `https://img.youtube.com/vi/${videoId}/hqdefault.jpg` : null;
+}
+
 serve(async (req) => {
   if (req.method === 'OPTIONS') {
     return new Response('ok', { headers: corsHeaders })
@@ -104,6 +127,14 @@ serve(async (req) => {
           image = imgMatch[1];
         }
       }
+      
+      // Generate YouTube thumbnail if this is a YouTube link and no image found
+      if (!image && link) {
+        const youtubeImage = generateYouTubeThumbnail(link);
+        if (youtubeImage) {
+          image = youtubeImage;
+        }
+      }
 
       if (title && guid) {
         items.push({