// // pool-import // // Copyright (C) 2016 Christian Pointner // // This file is part of pool-import. // // pool-import is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // any later version. // // pool-import is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with pool-import. If not, see . // package main import ( "fmt" "log" "net/http" "os" "sort" "time" "code.helsinki.at/rhrd-go/rddb" "code.helsinki.at/rhrd-go/rhimport" ) const ( NEW_RD_CONF = "/etc/rd.conf" 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) bool { filename := fmt.Sprintf("%06d_%03d.wav", cart, cut) stdlog.Printf("") 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 ctx.FetchConverter = "ffmpeg-bs1770" ctx.ExtraMetaData["ARTIST"] = artist ctx.ExtraMetaData["ALBUM"] = album ctx.ExtraMetaData["TITLE"] = title _, s, code, errstring := sessions.New(ctx, "") if code != http.StatusOK { stdlog.Printf(">>>>>>>> ERROR: creating session: %s", errstring) return false } 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 false } s.Run(10 * time.Minute) res := <-donechan if res.ResponseCode != http.StatusOK { stdlog.Printf(">>>>>>> ERROR: import failed: %s", res.ErrorString) return false } stdlog.Printf(">>>>>>> SUCCESS: imported into: cart/cut %d/%d", res.Cart, res.Cut) return true } func main() { if len(os.Args) < 3 { log.Fatal("Usage: pool-import ") } oldGroup := os.Args[1] newGroup := os.Args[2] 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) } newdb, err := rddb.NewDB(NEW_RD_CONF) if err != nil { log.Fatal("Error initializing NEW Rivdenll DB:", err) } defer newdb.Cleanup() stdlog := log.New(os.Stderr, "[std] ", log.LstdFlags) // dbglog := log.New(os.Stderr, "[dbg] ", log.LstdFlags) var dbglog *log.Logger sessions, err := rhimport.NewSessionStore(conf, newdb.GetInterface(), stdlog, dbglog) if err != nil { log.Fatal("Error initializing Session Store:", err) } defer sessions.Cleanup() olddb, err := NewDB(OLD_RD_CONF) if err != nil { log.Fatal("Error initializing OLD Rivdenll DB:", err) } defer olddb.Cleanup() olddbi := olddb.GetInterface() pool := PoolListEntry{oldGroup, ""} carts, err := olddbi.GetPoolCartList(pool) if err != nil { log.Fatal("Error fetching Pool cart list:", err) } stdlog.Println("***************************************************************") stdlog.Printf("*** will import: %d carts from old:%s -> new:%s", len(carts), oldGroup, newGroup) stdlog.Println("***************************************************************") var keys []int for k := range carts { keys = append(keys, int(k)) } sort.Ints(keys) start := time.Now() successCnt := 0 errCnt := 0 for _, k := range keys { cart := carts[uint(k)] if len(cart.Cuts) == 0 { stdlog.Printf("Warning: Cart %d has no cuts - ingoring it", cart.Number) continue } else if len(cart.Cuts) > 1 { stdlog.Printf("Warning: Cart %d has multiple cuts - will only use the first", cart.Number) } cut := cart.Cuts[0].Number if HandleCart(cart.Number, cut, cart.Artist, cart.Album, cart.Title, newGroup, sessions.GetInterface(), conf, stdlog, dbglog) { successCnt++ } else { errCnt++ } } stdlog.Println("") stdlog.Println("***************************************************************") stdlog.Printf("*** %d files imported successfully, %d errors", successCnt, errCnt) stdlog.Printf("*** process took %v", time.Since(start)) stdlog.Println("***************************************************************") }