From 59fc7ae8a39ab10455edc0bf4af1115da34b3146 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 16 Apr 2011 14:31:12 -0400 Subject: [PATCH] Get file name parsing to work --- src/tv_rename.go | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/src/tv_rename.go b/src/tv_rename.go index 3673933..8420511 100644 --- a/src/tv_rename.go +++ b/src/tv_rename.go @@ -14,6 +14,8 @@ import ( "os" "path" "regexp" + "strconv" + "strings" "./paths" ) @@ -37,7 +39,11 @@ func RenameEpisode(inPath string) (*string, os.Error) { // 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 } @@ -49,9 +55,34 @@ type episodeInfo struct { } // 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, + } } -- 2.43.5