-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcmd_index.go
160 lines (144 loc) · 3.7 KB
/
cmd_index.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
package main
import (
"bytes"
"context"
"fmt"
"regexp"
"time"
"github.com/blugelabs/bluge"
"github.com/briandowns/spinner"
"github.com/dhowden/tag"
"github.com/muesli/reflow/padding"
"github.com/muesli/reflow/truncate"
"github.com/rubiojr/rapi/repository"
"github.com/rubiojr/rapi/restic"
"github.com/rubiojr/rindex"
"github.com/urfave/cli/v2"
)
var tStart = time.Now()
const statusStrLen = 30
var audioRegexp *regexp.Regexp
type AudioFilter struct{}
type MP3DocumentBuilder struct{}
func init() {
cmd := &cli.Command{
Name: "index",
Usage: "Index the repository",
Action: indexRepo,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "log-errors",
Usage: "Log errors",
Required: false,
},
&cli.BoolFlag{
Name: "reindex",
Usage: "Re-index files",
Required: false,
},
},
}
appCommands = append(appCommands, cmd)
}
func (m *AudioFilter) ShouldIndex(path string) bool {
return audioRegexp.Match([]byte(path))
}
func indexRepo(cli *cli.Context) error {
var err error
audioRegexp, err = regexp.Compile(`\.(flac|ogg|mp3)$`)
if err != nil {
return err
}
progress := make(chan rindex.IndexStats, 10)
idxOpts := rindex.IndexOptions{
Filter: &AudioFilter{},
AppendFileMeta: true,
DocumentBuilder: &MP3DocumentBuilder{},
}
if cli.Bool("reindex") {
idxOpts.Reindex = true
fmt.Println("Re-indexing all snapshots and files")
}
go progressMonitor(cli.Bool("log-errors"), progress)
idx, err := rindex.New(indexPath, globalOptions.Repo, globalOptions.Password)
if err != nil {
return err
}
stats, err := idx.Index(context.Background(), idxOpts, progress)
if err != nil {
panic(err)
}
fmt.Printf(
"\n💥 %d indexed, %d already present. Took %d seconds.\n",
stats.IndexedFiles,
stats.AlreadyIndexed,
int(time.Since(tStart).Seconds()),
)
return nil
}
func (i MP3DocumentBuilder) BuildDocument(fileID string, node *restic.Node, repo *repository.Repository) *bluge.Document {
buf, err := repo.LoadBlob(context.Background(), restic.DataBlob, node.Content[0], nil)
var id3Info tag.Metadata
if err == nil {
// ignore errors when reading tags, we still want to index them
id3Info, _ = tag.ReadFrom(bytes.NewReader(buf))
}
artist := ""
title := ""
album := ""
genre := ""
year := 0
if id3Info != nil {
artist = id3Info.Artist()
title = id3Info.Title()
album = id3Info.Album()
genre = id3Info.Genre()
year = id3Info.Year()
}
doc := bluge.NewDocument(fileID).
AddField(bluge.NewTextField("artist", artist).StoreValue()).
AddField(bluge.NewTextField("title", title).StoreValue()).
AddField(bluge.NewTextField("album", album).StoreValue()).
AddField(bluge.NewTextField("genre", genre).StoreValue()).
AddField(bluge.NewNumericField("year", float64(year)).StoreValue())
return doc
}
func progressMonitor(logErrors bool, progress chan rindex.IndexStats) {
s := spinner.New(spinner.CharSets[11], 100*time.Millisecond)
s.Color("fgGreen")
s.Suffix = " Analyzing the repository..."
lastError := ""
for {
select {
case p := <-progress:
if logErrors {
if len(p.Errors) > 0 {
e := p.Errors[len(p.Errors)-1].Error()
if e != lastError {
panic(e)
fmt.Println("\n", e)
lastError = e
}
}
}
lm := p.LastMatch
if lm == "" {
lm = "Searching for MP3 files..."
}
ls := truncate.StringWithTail(lm, statusStrLen, "...")
rate := float64(p.ScannedNodes*1000000000) / float64(time.Since(tStart))
s.Suffix = fmt.Sprintf(
" %s 🎯 %d new, %d skipped, %d err, %.0f f/s, %d scanned",
padding.String(ls, statusStrLen),
p.IndexedFiles,
p.AlreadyIndexed,
len(p.Errors),
rate,
p.ScannedFiles,
)
default:
time.Sleep(100 * time.Millisecond)
}
}
s.Stop()
}