diff --git a/magic.json.default b/magic.json.default index acc0f70..855c9e7 100644 --- a/magic.json.default +++ b/magic.json.default @@ -1,3 +1,4 @@ { - "sessionId" : "insert" + "sessionId" : "insert" + "ffmpeg" : "ffmpeg" } diff --git a/platform/other.go b/platform/other.go new file mode 100644 index 0000000..fc89136 --- /dev/null +++ b/platform/other.go @@ -0,0 +1,8 @@ +// +build !windows + +package platform + +func SanitiseFilename(filename string) (newName string, e error) { + newName = filename + return +} diff --git a/platform/windows.go b/platform/windows.go new file mode 100644 index 0000000..2175818 --- /dev/null +++ b/platform/windows.go @@ -0,0 +1,18 @@ +// +build windows +// currently untested as I don't have a windows machine to test on + +package platform + +import( + "regexp" +) + +func SanitiseFilename(filename string) (newName string, e error) { + r, e := regexp.Compile("[\\?<>:\"/\\\\|\\*]") + if e != nil { + return + } + + newName = r.ReplaceAllString(filename, "") + return +} diff --git a/wimp-get.go b/wimp-get.go index bca541f..3cf1e80 100644 --- a/wimp-get.go +++ b/wimp-get.go @@ -6,8 +6,12 @@ import ( "regexp" "encoding/json" "path" + "io" "io/ioutil" "wimp-get/wimp" + "wimp-get/platform" + "os/exec" + "net/http" ) func main() { @@ -45,4 +49,82 @@ func main() { } fmt.Printf("[ %s - %s (%d) ]\n", album.Artist, album.Title, album.Year) + + // Determine whether we have more than one disc + multidisc := false + for _, track := range album.Tracks { + if track.Volume > 1 { + multidisc = true + break + } + } + + dirName := album.Artist+" - "+album.Title+" ("+fmt.Sprintf("%d", album.Year)+") [WEB FLAC]" + + e = os.Mkdir(dirName, os.FileMode(0755)) + if e != nil { + panic(e) + } + + // Time to do the ripping! + for _, track := range album.Tracks { + num := fmt.Sprintf("%d", track.Number) + if len(num) < 2 { + num = "0"+num + } + + fmt.Printf("[%d/%s] %s...", track.Volume, num, track.Title) + + var filename string + if (multidisc) { + filename = fmt.Sprintf("%s/Disc %d/%s - %s.flac", dirName, track.Volume, num, track.Title) + } else { + filename = fmt.Sprintf("%s/%s - %s.flac", dirName, num, track.Title) + } + + filename, e = platform.SanitiseFilename(filename) + if e != nil { + panic(e) + } + + // create disc dir if necessary + if _, e = os.Stat(path.Dir(filename)); e != nil { + if os.IsNotExist(e) { + e = os.Mkdir(path.Dir(filename), os.FileMode(0755)) + if e != nil { + panic(e) + } + } else { + panic(e) + } + } + + resp, e := http.Get(track.Url) + if e != nil { + panic(e) + } + + ffmpeg := exec.Command(magic["ffmpeg"].(string), "-i", "-", "-metadata", "title="+track.Title, "-metadata", "artist="+track.Artist, + "-metadata", "album="+album.Title, "-metadata", "year="+fmt.Sprintf("%d", album.Year), "-metadata", "track="+fmt.Sprintf("%d", track.Number), + "-metadata", "albumartist="+album.Artist, "-metadata", "discnumber="+fmt.Sprintf("%d", track.Volume), filename) + + stdin, e := ffmpeg.StdinPipe() + if e != nil { + panic(e) + } + + e = ffmpeg.Start() + if e != nil { + panic(e) + } + + _, e = io.Copy(stdin, resp.Body) + if e != nil { + panic(e) + } + + resp.Body.Close() + + println(" Done!") + } }