Get file name parsing to work
authorRobert Sesek <rsesek@bluestatic.org>
Sat, 16 Apr 2011 18:31:12 +0000 (14:31 -0400)
committerRobert Sesek <rsesek@bluestatic.org>
Sat, 16 Apr 2011 18:31:12 +0000 (14:31 -0400)
src/tv_rename.go

index 36739333180e3b82919f34f347cf5dbbfd5abd6c..8420511f9c8e9efc41fb9a8f01842b1e3a927173 100644 (file)
@@ -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,
+  }
 }