This repository has been archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpcf.go
286 lines (271 loc) · 6.88 KB
/
mpcf.go
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package main
import (
"crypto/md5"
"database/sql"
"flag"
"fmt"
_ "github.com/mattn/go-sqlite3"
"io"
"io/ioutil"
"log"
"os"
"strings"
)
var (
verp = flag.Bool("version", false, "Show version info")
facetp = flag.Bool("facets", false, "List facets")
scanp = flag.Bool("scan", false, "Check for new/modified files and sweep db for orphan records")
cleanp = flag.Bool("cleandb", false, "Just clean db (no file scan)")
tagp = flag.Bool("tag", false, "Tag [dir] with [facet]")
getp = flag.Bool("get", false, "Get filenames for tracks tagged with [facet]")
mdflag = flag.String("musicdir", "", "Set location of your mpd music directory")
musicdir = ""
seen int64
touched int64
)
func init() {
flag.Parse()
config := os.Getenv("HOME") + "/.mpcf"
if *mdflag != "" {
err := ioutil.WriteFile(config, []byte(*mdflag), 0644)
if err != nil {
log.Fatal(err)
}
musicdir = *mdflag
} else {
mdbytes, err := ioutil.ReadFile(config)
if err != nil {
log.Fatal("Please run 'mpcf -musicdir /path/to/music' to set your musicdir path.")
}
musicdir = string(mdbytes)
}
}
func main() {
db, err := sql.Open("sqlite3", musicdir+"/.mpcf.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// create db if needed
var tracks int
res := db.QueryRow("select count(id) from tracks")
err = res.Scan(&tracks)
if err != nil {
db.Exec("PRAGMA synchronous=0")
log.Println("Creating db")
createdb(db)
log.Println("Updating track list")
scandir("", db)
}
if *verp {
fmt.Println("This is mpcf v0.5.3")
os.Exit(0)
}
if *scanp {
db.Exec("PRAGMA synchronous=0")
scandir("", db)
cleandb(db)
os.Exit(0)
}
if *cleanp {
cleandb(db)
os.Exit(0)
}
if *tagp {
tagdir(flag.Args(), db)
os.Exit(0)
}
if *getp {
getfacettracks(flag.Args(), db)
os.Exit(0)
}
if *facetp {
lsfacets(db)
os.Exit(0)
}
var taggedtracks, tags, facets int
db.QueryRow("select count(tid) from t2f").Scan(&tags)
db.QueryRow("select count(distinct tid) from t2f").Scan(&taggedtracks)
db.QueryRow("select count(id) from facets").Scan(&facets)
fmt.Printf("%v tracks (%v tagged)\n%v tags\n%v facets\n", tracks, taggedtracks, tags, facets)
}
func lsfacets(db *sql.DB) {
rows, err := db.Query("SELECT facet FROM facets ORDER BY facet")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var f string
for rows.Next() {
if err := rows.Scan(&f); err != nil {
log.Fatal(err)
}
fmt.Println(f)
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
}
func getfacettracks(args []string, db *sql.DB) {
if len(args) != 1 {
log.Fatal("Too many/few arguments to -get; need a facet name")
}
var fid int
db.QueryRow("select id from facets where facet = ?", args[0]).Scan(&fid)
if fid == 0 {
return
}
rows, err := db.Query("SELECT filename FROM tracks WHERE id IN (SELECT DISTINCT tid FROM t2f WHERE fid = ?)", fid)
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var name string
for rows.Next() {
if err := rows.Scan(&name); err != nil {
log.Fatal(err)
}
fmt.Println(name)
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
}
func tagdir(args []string, db *sql.DB) {
if len(args) != 2 {
log.Fatal("Too many/few arguments to -tag; need a directory and a facet")
}
// create the tag if it doesn't exist
var fid int
db.QueryRow("select id from facets where facet = ?", args[1]).Scan(&fid)
if fid == 0 {
db.Exec("insert into facets (facet) values (?)", args[1])
db.QueryRow("select id from facets where facet = ?", args[1]).Scan(&fid)
}
// now actually tag tracks under this dir
args[0] = strings.TrimRight(args[0], "/")
args[0] = strings.TrimLeft(args[0], "./")
args[0] = strings.TrimLeft(args[0], musicdir)
tagdir2(args[0], fid, db)
}
func tagdir2(dir string, fid int, db *sql.DB) {
err := os.Chdir(musicdir + "/" + dir)
if err != nil {
log.Fatalf("Can't chdir to %v", dir)
}
ls, err := ioutil.ReadDir(".")
for _, direntry := range ls {
name := dir + "/" + direntry.Name()
if direntry.IsDir() {
tagdir2(name, fid, db)
} else {
var tid, fcnt int
db.QueryRow("select id from tracks where filename = ?", name).Scan(&tid)
db.QueryRow("select count(tid) from t2f where tid = ? and fid = ?", tid, fid).Scan(&fcnt)
if fcnt > 0 {
continue
}
db.Exec("insert into t2f (tid, fid) values (?, ?)", tid, fid)
}
}
}
func createdb(db *sql.DB) {
var err error
var stmts = []string{
"create table tracks (id integer primary key, filename text unique, hash text unique)",
"create table facets (id integer primary key, facet text)",
"create table t2f (tid integer, fid integer)",
"create index fididx on t2f(fid)",
"create table config (key text, value text)",
"insert into config (key, value) values('mpdconf', '/etc/mpd.conf')",
}
for _, stmt := range stmts {
if err != nil {
break
}
_, err = db.Exec(stmt)
}
if err != nil {
log.Fatal(err)
}
}
func scandir(dir string, db *sql.DB) {
os.Chdir(musicdir + "/" + dir)
ls, err := ioutil.ReadDir(".")
if err != nil {
log.Fatal(err, dir)
}
for _, direntry := range ls {
if direntry.IsDir() {
if dir == "" {
scandir(direntry.Name(), db)
} else {
scandir(dir+"/"+direntry.Name(), db)
}
} else {
seen++
if seen%100 == 0 {
log.Printf("Processed %v tracks; updated %v\n", seen, touched)
}
name := dir + "/" + direntry.Name()
md5 := fmt.Sprintf("%x", calcMD5(direntry.Name()))
// _, err := db.Exec("INSERT OR REPLACE INTO tracks (filename, hash) VALUES(COALESCE((SELECT filename FROM tracks WHERE filename = ?),?), COALESCE((SELECT hash FROM tracks WHERE hash = ?), ?))", name, name, md5, md5)
r, err := db.Exec("INSERT OR IGNORE INTO tracks (filename, hash) VALUES(?, ?)", name, md5)
if err != nil {
log.Fatal(err)
}
touch, _ := r.RowsAffected()
touched += touch
//r, err = db.Exec("UPDATE tracks SET filename = ?, hash = ? WHERE filename = ?", name, md5, name)
//if err != nil {
// log.Fatal(err)
//}
}
}
}
func cleandb(db *sql.DB) {
log.Printf("Scanning db for orphaned records")
rows, err := db.Query("SELECT id, filename FROM tracks")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
var id int64
var name string
for rows.Next() {
if err := rows.Scan(&id, &name); err != nil {
log.Fatal(err)
}
_, err = os.Stat(musicdir + "/" + name)
if err == nil {
continue
}
// remove track entry
_, err = db.Exec("delete from tracks where id = ?", id)
if err != nil {
log.Fatal(err)
}
// remove tag links
_, err = db.Exec("delete from t2f where tid = ?", id)
if err != nil {
log.Fatal(err)
}
log.Printf("Removed orphan record for %v\n", name)
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
_, err = db.Exec("vacuum")
}
func calcMD5(filename string) []byte {
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
hash := md5.New()
if _, err := io.CopyN(hash, file, 524288); err != nil && err != io.EOF {
log.Fatal(err)
}
return hash.Sum(nil)
}