"os"
"path"
"regexp"
+ "strconv"
+ "strings"
"./paths"
)
// Parse the filename into its components.
_, fileName := path.Split(*safePath)
- parseEpisodeName(fileName)
+ info := parseEpisodeName(fileName)
+ if info == nil {
+ return nil, os.NewError("Could not parse file name")
+ }
+ fmt.Print("info = ", *info)
return safePath, nil
}
}
// Parses the last path component into a the component structure.
-func parseEpisodeName(name string) episodeInfo {
- regex := regexp.MustCompile("^([0-9]+_)?(.+)( |\\.)(S|s)?([0-9]+)[xeXE]([0-9]+)")
- matches := regex.FindAllString(name, 0)
- fmt.Printf("matches = %s\n", matches)
- return episodeInfo{ "", 0, 0 }
+func parseEpisodeName(name string) *episodeInfo {
+ regex := regexp.MustCompile("(.+)( |\\.)[sS]?([0-9]+)[xeXE]([0-9]+)")
+ matches := regex.FindAllStringSubmatch(name, -1)
+ if len(matches) < 1 || len(matches[0]) < 4 {
+ return nil
+ }
+
+ // Convert the season and episode numbers to integers.
+ season, err := strconv.Atoi(matches[0][3])
+ if err != nil {
+ return nil
+ }
+ episode, err := strconv.Atoi(matches[0][4])
+ if err != nil {
+ return nil
+ }
+
+ // If the separator between the show title and episode is a period, then
+ // it's likely of the form "some.show.name.s03e06.720p.blah.mkv", so strip the
+ // periods in the title.
+ var showName string = matches[0][1]
+ if matches[0][2] == "." {
+ showName = strings.Replace(matches[0][1], ".", " ", -1)
+ }
+
+ return &episodeInfo {
+ showName,
+ season,
+ episode,
+ }
}