func populateSampleEpisodes(t *testing.T, name string, count int) (*Series, *Season, []*Episode) { series := NewSeries(name) if err := database.SaveSeries(series); err != nil { t.Errorf("unexpected error: %v", err) } season, err := NewSeason(series.Id, 1) if err != nil { t.Errorf("unexpected error: %v", err) } if err := database.SaveSeason(season); err != nil { t.Errorf("unexpected error: %v", err) } episodes := make([]*Episode, count) for i := 0; i < count; i++ { e := &Episode{ Title: fmt.Sprintf("Episode %d", i), SeasonId: season.Id, SeasonNumber: uint64(season.SeasonNumber), SeriesId: series.Id, } if err := database.SaveEpisode(e); err != nil { t.Errorf("unexpected error: %v", err) } episodes[i] = e } return series, season, episodes }
func FetchMetadataForSeries(queue *Queue, series *Series) error { log.Printf("Fetching metadata for series: %q", series.Title) ss, err := tvdbconn.GetSeries(series.Title, "") if err != nil { log.Printf("Error querying TVDB for %s: %v", series.Title, err) return err } if len(ss) < 1 { log.Printf("No Series found for name: %s", series.Title) return nil } // Now that we have basic info, get hte extended info. s, err := tvdbconn.GetSeriesById(ss[0].Id, "") if err != nil { log.Printf("Error querying TVDB for %s: %v", series.Title, err) return err } // Use the top matching series for now series.Title = s.SeriesName series.Overview = s.Overview series.SeriesId = int64(s.Id) series.Language = s.Language series.ImdbId = s.ImdbId series.Banner = tvdbconn.GetImageUrl(s.Banner) series.FanArt = tvdbconn.GetImageUrl(s.FanArt) series.Poster = tvdbconn.GetImageUrl(s.Poster) log.Printf("Writing series: %+v", series) // Persist the series err = database.SaveSeries(series) if err != nil { return err } eps, err := database.ListEpisodesForSeries(series.SeriesId) if err != nil && err != sql.ErrNoRows { log.Printf("Failed to fetch episodes for season: %v", err) } if eps != nil { for _, ep := range eps { ep.TvdbSeriesId = series.SeriesId err = database.SaveEpisode(ep) if err != nil { return err } queue.Add(&FetchMetadataTask{Episode: ep}) } } return nil }
func FetchMetadataForEpisode(e *Episode) error { log.Printf("Fetching metadata for episode: %q", e.Title) if e.TvdbSeriesId == 0 { s, err := database.GetSeries(e.SeriesId) if err != nil { return err } e.TvdbSeriesId = s.SeriesId log.Printf("Adding series id to episode: %d", e.TvdbSeriesId) } b, err := tvdbconn.GetEpisodeBySeasonEp(int(e.TvdbSeriesId), int(e.SeasonNumber), int(e.EpisodeNumber), "en") if err != nil { log.Printf("Error querying TheTVDB for %s: %v", e.Title, err) return err } singleEpisode, err := tvdb.ParseSingleEpisode(b) if err != nil { log.Printf("Error parsing single episode: %v", err) return err } episode := singleEpisode.Episode e.Title = episode.EpisodeName e.Overview = episode.Overview e.ImdbId = episode.ImdbId e.Director = episode.Director e.Writer = episode.Writer e.SeasonId = int64(episode.SeasonId) e.GuestStars = episode.GuestStars e.AbsoluteNumber = episode.AbsoluteNumber // IMDB info title, err := imdb.NewTitle(http.DefaultClient, e.ImdbId) if err != nil { log.Printf("Failed to get extended info from imdb: %v", err) } else { e.Poster = title.Poster.ContentURL } err = database.SaveEpisode(e) if err != nil { return err } return nil }
// PUT /api/episodes // Creates a new episodes func EpisodeCreate(w http.ResponseWriter, r *http.Request) (interface{}, error) { var s Episode err := json.NewDecoder(r.Body).Decode(&s) if err != nil { return nil, err } if s.Id != 0 { return nil, badRequest{errors.New("Episode already has an Id")} } err = database.SaveEpisode(&s) if err != nil { return nil, err } return s, nil }
func processEpisode(path string, meta *guessit.GuessResult, queue *Queue) error { episode, _ := database.GetEpisodeByFilename(path) var fetchMetadataTask *FetchMetadataTask if episode == nil || episode.Filename == "" { s, err := database.GetSeriesByTitle(meta.Series) if err != nil { // Create and save a placeholder series s = &Series{ Title: meta.Series, } err = database.SaveSeries(s) if err != nil { return err } // Queue a metadata refresh of the series data fetchMetadataTask = &FetchMetadataTask{Series: s} } episode = &Episode{ Filename: path, Title: meta.Title, EpisodeNumber: uint64(meta.EpisodeNumber), SeasonNumber: uint64(meta.Season), SeriesId: s.Id, TvdbSeriesId: s.SeriesId, } err = database.SaveEpisode(episode) if err != nil { log.Println("Error saving movie") } } if fetchMetadataTask != nil { queue.Add(fetchMetadataTask) } else { queue.Add(&FetchMetadataTask{Episode: episode}) } return nil }