diff options
-rw-r--r-- | src/pool-import/main.go | 62 |
1 files changed, 52 insertions, 10 deletions
diff --git a/src/pool-import/main.go b/src/pool-import/main.go index b184ef3..7dd7851 100644 --- a/src/pool-import/main.go +++ b/src/pool-import/main.go @@ -22,7 +22,9 @@ package main import ( + "fmt" "log" + "net/http" "os" "sort" @@ -35,14 +37,55 @@ const ( OLD_RD_CONF = "rd.conf" ) +func Done(res rhimport.Result, userdata interface{}) bool { + c := userdata.(chan<- rhimport.Result) + c <- res + return true +} + +func HandleCart(cart uint, cut uint, artist, album, title, dstgroup string, sessions *rhimport.SessionStore, conf *rhimport.Config, stdlog, dbglog *log.Logger) { + filename := fmt.Sprintf("%06d_%03d.wav", cart, cut) + + stdlog.Printf("********* %s: '%s' / '%s' / '%s'", filename, artist, album, title) + + ctx := rhimport.NewContext(conf, nil, stdlog, dbglog) + ctx.UserName = "importer" + ctx.Trusted = true + ctx.GroupName = dstgroup + ctx.SourceUri = "local:///" + filename + ctx.SourceFilePolicy = rhimport.Keep + + id, s, code, errstring := sessions.New(ctx, "") + if code != http.StatusOK { + stdlog.Printf(">>>>>>>> ERROR: creating session: %s", errstring) + return + } + stdlog.Printf(">>>>>>>> got session: %s", id) + + donechan := make(chan rhimport.Result, 1) + if err := s.AddDoneHandler((chan<- rhimport.Result)(donechan), Done); err != nil { + stdlog.Printf(">>>>>>> ERROR: adding done handler: %s", err.Error()) + return + } + + s.Run(0) + res := <-donechan + + if res.ResponseCode != http.StatusOK { + stdlog.Printf(">>>>>>> ERROR: adding done handler: %s", res.ErrorString) + } else { + stdlog.Printf(">>>>>>> SUCCESS: imported into: cart/cut %d/%d", res.Cart, res.Cut) + } +} + func main() { if len(os.Args) < 3 { log.Fatal("Usage: pool-import <old pool group> <new pool group>") } - old_group := os.Args[1] - new_group := os.Args[2] + oldGroup := os.Args[1] + newGroup := os.Args[2] - impconf, err := rhimport.NewConfig(NEW_RD_CONF, "http://localhost/rd-bin/rdxport.cgi", "/tmp", "snd/") + conf, err := rhimport.NewConfig(NEW_RD_CONF, "http://localhost/rd-bin/rdxport.cgi", "/tmp", "snd/") if err != nil { log.Fatal("Error parsing configuration file:", err) } @@ -55,7 +98,7 @@ func main() { stdlog := log.New(os.Stderr, "[std] ", log.LstdFlags) dbglog := log.New(os.Stderr, "[dbg] ", log.LstdFlags) - sessions, err := rhimport.NewSessionStore(impconf, newdb.GetInterface(), stdlog, dbglog) + sessions, err := rhimport.NewSessionStore(conf, newdb.GetInterface(), stdlog, dbglog) if err != nil { log.Fatal("Error initializing Session Store:", err) } @@ -68,13 +111,13 @@ func main() { defer olddb.Cleanup() olddbi := olddb.GetInterface() - pool := PoolListEntry{old_group, ""} + pool := PoolListEntry{oldGroup, ""} carts, err := olddbi.GetPoolCartList(pool) if err != nil { log.Fatal("Error fetching Pool cart list:", err) } - log.Printf("will import: %d carts from old:%s -> new:%s", len(carts), old_group, new_group) + stdlog.Printf("will import: %d carts from old:%s -> new:%s", len(carts), oldGroup, newGroup) var keys []int for k := range carts { @@ -84,13 +127,12 @@ func main() { for _, k := range keys { cart := carts[uint(k)] if len(cart.Cuts) == 0 { - log.Printf("Warning: Cart %d has no cuts - ingoring it", cart.Number) + stdlog.Printf("Warning: Cart %d has no cuts - ingoring it", cart.Number) continue } else if len(cart.Cuts) > 1 { - log.Printf("Warning: Cart %d has multiple cuts - will only use the first", cart.Number) + stdlog.Printf("Warning: Cart %d has multiple cuts - will only use the first", cart.Number) } cut := cart.Cuts[0].Number - log.Printf(" %06d_%03d: '%s' / '%s' / '%s'", cart.Number, cut, cart.Artist, cart.Album, cart.Title) - + HandleCart(cart.Number, cut, cart.Artist, cart.Album, cart.Title, newGroup, sessions.GetInterface(), conf, stdlog, dbglog) } } |