func (self FilesystemExtractor) Names(epi *renamer.Episode) ([]string, error) { possibilities := []string{epi.Series} if util.IsDirectory(epi.Path) { // Check all subdirectories and the episodefile itepi for a suitable // series name episodepath := epi.Path // get the diff between epi.Path and epi.Episodefile so that we can // add one path element a time and extract the series subpath := epi.Episodefile[len(episodepath):] splits := strings.Split(subpath, "/") for _, part := range splits { if part == "" { continue } episodepath = path.Join(episodepath, part) subepisode, suberr := renamer.CreateEpisodeFromPath(episodepath) if suberr == nil { possibilities = append(possibilities, subepisode.Series) } } } return possibilities, nil }
func FindBiggestVideoFile(dir string) (string, error) { if !util.IsDirectory(dir) { return "", errors.New("The supplied directory does not exist") } var videofile string var videofile_size int64 walker := func(entry_path string, info os.FileInfo, err error) error { if info.IsDir() || !HasVideoFileEnding(entry_path) { return nil } if info.Size() > videofile_size { videofile = entry_path videofile_size = info.Size() } return nil } err := filepath.Walk(dir, walker) if err != nil { panic(err) } if videofile == "" { return "", errors.New("No videofile available") } return videofile, nil }
func CreateEpisodeFromPath(path string) (*Episode, error) { episode := new(Episode) if !util.PathExists(path) { return episode, errors.New("Supplied episode does not exist") } basename := filepath.Base(path) if !IsInterestingDirEntry(basename) { return episode, errors.New("Supplied episode has no series information") } episode.Path = path episode.Episodefile = path if util.IsDirectory(path) { episodefile, err := FindBiggestVideoFile(path) if err != nil { return episode, err } episode.Episodefile = episodefile } if !HasVideoFileEnding(episode.Episodefile) { return episode, errors.New("No videofile available") } information := ExtractEpisodeInformation(basename) episode.Season, _ = strconv.Atoi(information["season"]) episode.Episode, _ = strconv.Atoi(information["episode"]) episode.Series = CleanEpisodeInformation(information["series"]) episode.Extension = GlobalPath.Ext(episode.Episodefile) name := information["episodename"] if util.IsFile(path) { name = name[:len(name)-len(episode.Extension)] } episode.Name = CleanEpisodeInformation(name) episode.ExtractLanguage() return episode, nil }
func (self *Episode) Rename(dest_path string) error { if !self.CanBeRenamed() { return errors.New( "This episode couldn't be renamed as it has some problems") } need_cleanup := false if util.IsDirectory(self.Path) { need_cleanup = true } dest := GlobalPath.Join(dest_path, self.CleanedFileName()) err := os.Rename(self.Episodefile, dest) if err != nil { return err } if need_cleanup { return os.RemoveAll(self.Path) } return nil }