func (self *SeriesIndex) AddEpisode(episode *renamer.Episode) (bool, error) { // test for all possible series names if they exist in index and take the // first matching ExtractorLoop: for _, extractor := range self.nameExtractors { names, err := extractor.Names(episode) if err != nil { fmt.Printf("!!! Error asking extractor for series names: %s", err) continue } for _, possible_series := range names { series_name := self.SeriesNameInIndex(possible_series) if series_name != "" { episode.Series = series_name break ExtractorLoop } } } series, existing := self.seriesMap[episode.Series] if !existing { return false, errors.New("Series does not exist in index") } // Handle episodes where no language is set if episode.Language == "" { self.GuessEpisodeLanguage(episode, series) } _, language_exist := series.languageMap[episode.Language] if !language_exist { return false, errors.New("Series is not watched in this language") } if self.IsEpisodeInIndex(*episode) { return false, errors.New("Episode already exists in Index") } episode_entry := Episode{Name: episode.CleanedFileName()} // find the right EpisodeSet so we can add our new episode to it set, exist := series.languageMap[episode.Language] if exist { set.EpisodeList = append(set.EpisodeList, episode_entry) set.BuildUpEpisodeMap() return true, nil } return false, errors.New("Episode couldn't be added to index. This shouldn't occur!") }
func (self *SeriesIndex) GuessEpisodeLanguage(episode *renamer.Episode, series *Series) { // This methods tries to find the right language for the supplied episode // based on several heuristics // When there is no language set and the series is only watched in one // language we can take this series if len(series.languageMap) == 1 { for k, _ := range series.languageMap { episode.Language = k break } } // Find the language which is most likely the right language if len(series.languageMap) > 1 { possible_languages := []string{} // when episode has not been watched in only one of the languages for lang, _ := range series.languageMap { episode.Language = lang if !self.IsEpisodeInIndex(*episode) { possible_languages = append(possible_languages, lang) } episode.Language = "" } if len(possible_languages) == 1 { episode.Language = possible_languages[0] } else if len(possible_languages) > 1 { // take the language where the previous episode exist previous_existing := []string{} for _, lang := range possible_languages { epi := *episode epi.Language = lang if (epi.Episode - 1) > 0 { epi.Episode -= 1 } if self.IsEpisodeInIndex(epi) { previous_existing = append(previous_existing, lang) } } if len(previous_existing) == 1 { episode.Language = previous_existing[0] } } } }
func (s *MySuite) TestEpisodeExistanceCheckWithExactSeriesName(c *C) { episode := renamer.Episode{Series: "Shameless US", Season: 1, Episode: 1, Language: "de"} c.Assert(s.index.IsEpisodeInIndex(episode), Equals, true) episode.Language = "en" c.Assert(s.index.IsEpisodeInIndex(episode), Equals, true) episode.Language = "fr" c.Assert(s.index.IsEpisodeInIndex(episode), Equals, false) episode.Season = 100 c.Assert(s.index.IsEpisodeInIndex(episode), Equals, false) }