diff options
| author | Bobby <[email protected]> | 2026-01-15 18:32:06 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2026-01-15 18:32:06 +0530 |
| commit | 78256b3ed08362bc813a92dc0e36d6be3755f808 (patch) | |
| tree | b8f21e799699ba225c3f726da3adb65c60a4d2a8 /database | |
| parent | 8221a9ff2a046c76a64a647fbe1f87069f196673 (diff) | |
| download | metachan-78256b3ed08362bc813a92dc0e36d6be3755f808.tar.xz metachan-78256b3ed08362bc813a92dc0e36d6be3755f808.zip | |
Add episode streaming functionality and caching
Diffstat (limited to 'database')
| -rw-r--r-- | database/anime.go | 88 | ||||
| -rw-r--r-- | database/migrate.go | 4 |
2 files changed, 92 insertions, 0 deletions
diff --git a/database/anime.go b/database/anime.go index 02bac41..e210b62 100644 --- a/database/anime.go +++ b/database/anime.go @@ -314,3 +314,91 @@ func GetAnimeMappingsByTVDBID(tvdbID int) ([]entities.AnimeMapping, error) { } return mappings, nil } + +// GetEpisodeStreaming retrieves cached streaming data for an episode +func GetEpisodeStreaming(episodeID string, animeID uint) (*entities.EpisodeStreaming, error) { + var streaming entities.EpisodeStreaming + result := DB.Preload("SubSources"). + Preload("DubSources"). + Where("episode_id = ? AND anime_id = ?", episodeID, animeID). + First(&streaming) + + if result.Error != nil { + return nil, result.Error + } + + // Check if data is stale (older than 7 days) + if time.Since(streaming.LastFetch) > 7*24*time.Hour { + return nil, fmt.Errorf("streaming data is stale") + } + + return &streaming, nil +} + +// SaveEpisodeStreaming saves streaming data to the database +func SaveEpisodeStreaming(episodeID string, animeID uint, subSources, dubSources []types.AnimeStreamingSource) error { + tx := DB.Begin() + if tx.Error != nil { + return tx.Error + } + + defer func() { + if r := recover(); r != nil { + tx.Rollback() + } + }() + + // Delete existing streaming data for this episode + var existing entities.EpisodeStreaming + if err := tx.Where("episode_id = ? AND anime_id = ?", episodeID, animeID).First(&existing).Error; err == nil { + if err := tx.Delete(&existing).Error; err != nil { + tx.Rollback() + return err + } + } + + // Create new streaming record + streaming := &entities.EpisodeStreaming{ + EpisodeID: episodeID, + AnimeID: animeID, + LastFetch: time.Now(), + } + + // Save the main record first + if err := tx.Create(streaming).Error; err != nil { + tx.Rollback() + return err + } + + // Save sub sources + for _, source := range subSources { + subSource := entities.EpisodeStreamingSource{ + EpisodeStreamingID: streaming.ID, + URL: source.URL, + Server: source.Server, + Type: source.Type, + } + if err := tx.Create(&subSource).Error; err != nil { + tx.Rollback() + return err + } + streaming.SubSources = append(streaming.SubSources, subSource) + } + + // Save dub sources + for _, source := range dubSources { + dubSource := entities.EpisodeStreamingSource{ + EpisodeStreamingID: streaming.ID, + URL: source.URL, + Server: source.Server, + Type: source.Type, + } + if err := tx.Create(&dubSource).Error; err != nil { + tx.Rollback() + return err + } + streaming.DubSources = append(streaming.DubSources, dubSource) + } + + return tx.Commit().Error +} diff --git a/database/migrate.go b/database/migrate.go index 89cdb7d..9e7c1b9 100644 --- a/database/migrate.go +++ b/database/migrate.go @@ -32,6 +32,10 @@ func AutoMigrate() { &entities.AnimeCharacter{}, &entities.AnimeVoiceActor{}, &entities.AnimeSeason{}, + + // Streaming entities + &entities.EpisodeStreaming{}, + &entities.EpisodeStreamingSource{}, ) if err != nil { logger.Log(fmt.Sprintf("Failed to migrate database: %v", err), logger.LogOptions{ |
