-
Notifications
You must be signed in to change notification settings - Fork 41
/
repair.go
91 lines (73 loc) · 1.97 KB
/
repair.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
package main
import (
"fmt"
"path/filepath"
"strconv"
"strings"
"github.com/bogem/id3v2"
"github.com/headzoo/surf/errors"
"github.com/zmb3/spotify"
)
func RepairWorker(client spotify.Client, job <-chan string, results chan<- string) {
for filePath := range job {
_, fileName := filepath.Split(filePath)
results <- fmt.Sprintf("Fixing : %v\n", fileName)
if err := Repair(client, filePath); err != nil {
results <- fmt.Sprintf("Error : %v\n", err)
}
}
}
func RevertWorker(job <-chan string, results chan<- string) {
for filePath := range job {
_, fileName := filepath.Split(filePath)
results <- fmt.Sprintf("Reverting : %v\n", fileName)
if err := Revert(filePath); err != nil {
results <- fmt.Sprintf("Error : %v\n", err)
}
}
}
func Revert(path string) error {
tag, err := id3v2.Open(path, id3v2.Options{Parse: true})
if err != nil {
return err
}
defer tag.Close()
tag.DeleteAllFrames()
if err = tag.Save(); err != nil {
return err
}
return nil
}
func Repair(client spotify.Client, path string) error {
tag, err := id3v2.Open(path, id3v2.Options{Parse: true})
if err != nil {
return err
}
if CheckFrames(tag.AllFrames()) {
return errors.New("Already contains tags")
}
_, filename := filepath.Split(path)
metadata, err := GetMetadata(client, filename[0:len(filename)-4])
if err != nil {
return err
}
tag.SetTitle(metadata.Title)
tag.SetAlbum(metadata.Album)
tag.SetArtist(strings.Join(metadata.Artists, ","))
TrackNumber := strconv.Itoa(metadata.TrackNumber)
tag.AddFrame("TRCK", id3v2.TextFrame{id3v2.EncodingUTF8, TrackNumber})
DiscNumber := strconv.Itoa(metadata.DiscNumber)
tag.AddFrame("TPOS", id3v2.TextFrame{id3v2.EncodingUTF8, DiscNumber})
pic := id3v2.PictureFrame{
Encoding: id3v2.EncodingUTF8,
MimeType: "image/jpeg",
PictureType: id3v2.PTFrontCover,
Description: "Front cover",
Picture: metadata.Image,
}
tag.AddAttachedPicture(pic)
if err = tag.Save(); err != nil {
return err
}
return nil
}