1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
//
// pool-import
//
// Copyright (C) 2016 Christian Pointner <equinox@helsinki.at>
//
// 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 <http://www.gnu.org/licenses/>.
//
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/signal"
"sort"
"syscall"
"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, c <-chan os.Signal) int {
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.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 1
}
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 1
}
s.Run(10 * time.Minute)
var res rhimport.Result
Loop:
for {
select {
case res = <-donechan:
break Loop
case <-c:
s.Cancel()
}
}
if res.ResponseCode == http.StatusNoContent {
stdlog.Printf(">>>>>>> CANCELED!!")
return -1
}
if res.ResponseCode != http.StatusOK {
stdlog.Printf(">>>>>>> ERROR: import failed: %s", res.ErrorString)
return 1
}
stdlog.Printf(">>>>>>> SUCCESS: imported into: cart/cut %d/%d", res.Cart, res.Cut)
return 0
}
func main() {
if len(os.Args) < 3 {
log.Fatal("Usage: pool-import <old pool group> <new pool group>")
}
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)
}
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM)
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
Loop:
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
switch HandleCart(cart.Number, cut, cart.Artist, cart.Album, cart.Title, newGroup, sessions.GetInterface(), conf, stdlog, dbglog, c) {
case 0:
successCnt++
case -1:
break Loop
default:
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("***************************************************************")
time.Sleep(time.Second) // give session some time to cleanup
}
|