feat: add dedicated YouTube transcript API endpoint

## CHANGES

- Add new YouTube handler for transcript requests
- Create `/youtube/transcript` POST endpoint route
- Add request/response types for YouTube API
- Support language and timestamp options
- Update frontend to use new endpoint
- Remove chat endpoint dependency for transcripts
- Validate video vs playlist URLs properly
This commit is contained in:
Kayvan Sylvan
2025-06-26 01:21:27 -07:00
parent e29ed908e6
commit 0dbe1bbb4e
3 changed files with 75 additions and 4 deletions

View File

@@ -26,6 +26,7 @@ func Serve(registry *core.PluginRegistry, address string, apiKey string) (err er
NewContextsHandler(r, fabricDb.Contexts)
NewSessionsHandler(r, fabricDb.Sessions)
NewChatHandler(r, registry, fabricDb)
NewYouTubeHandler(r, registry)
NewConfigHandler(r, fabricDb)
NewModelsHandler(r, registry.VendorManager)
NewStrategiesHandler(r)

70
restapi/youtube.go Normal file
View File

@@ -0,0 +1,70 @@
package restapi
import (
"net/http"
"github.com/danielmiessler/fabric/core"
"github.com/danielmiessler/fabric/plugins/tools/youtube"
"github.com/gin-gonic/gin"
)
type YouTubeHandler struct {
yt *youtube.YouTube
}
type YouTubeRequest struct {
URL string `json:"url"`
Language string `json:"language"`
Timestamps bool `json:"timestamps"`
}
type YouTubeResponse struct {
Transcript string `json:"transcript"`
Title string `json:"title"`
}
func NewYouTubeHandler(r *gin.Engine, registry *core.PluginRegistry) *YouTubeHandler {
handler := &YouTubeHandler{yt: registry.YouTube}
r.POST("/youtube/transcript", handler.Transcript)
return handler
}
func (h *YouTubeHandler) Transcript(c *gin.Context) {
var req YouTubeRequest
if err := c.BindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request"})
return
}
if req.URL == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "url is required"})
return
}
language := req.Language
if language == "" {
language = "en"
}
var videoID, playlistID string
var err error
if videoID, playlistID, err = h.yt.GetVideoOrPlaylistId(req.URL); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
if videoID == "" && playlistID != "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "URL is a playlist, not a video"})
return
}
var transcript string
if req.Timestamps {
transcript, err = h.yt.GrabTranscriptWithTimestamps(videoID, language)
} else {
transcript, err = h.yt.GrabTranscript(videoID, language)
}
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, YouTubeResponse{Transcript: transcript, Title: videoID})
}

View File

@@ -1,5 +1,5 @@
import { get } from 'svelte/store';
import { languageStore } from '$lib/store/language-store';
import { get } from 'svelte/store';
export interface TranscriptResponse {
transcript: string;
@@ -18,18 +18,18 @@ export async function getTranscript(url: string): Promise<TranscriptResponse> {
console.log('\n=== YouTube Transcript Service Start ===');
console.log('1. Request details:', {
url,
endpoint: '/chat',
endpoint: '/youtube/transcript',
method: 'POST',
isYouTubeURL: url.includes('youtube.com') || url.includes('youtu.be'),
originalLanguage
});
const response = await fetch('/chat', {
const response = await fetch('/youtube/transcript', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
body: JSON.stringify({
url,
language: originalLanguage // Pass original language to server
})