-
Notifications
You must be signed in to change notification settings - Fork 1
/
id3.go
99 lines (79 loc) · 1.92 KB
/
id3.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
// Package id3 provides the interfacing methods to working with a file on the filesystem and pushing
// content into necessary processors for tag discovery and usage
package id3
import (
"encoding/json"
"fmt"
"io"
"github.com/cloudcloud/go-id3/frames"
"gopkg.in/yaml.v2"
)
// File provides the data container for an individual File
type File struct {
Filename string `json:"filename"`
V1 *V1 `json:"id3v1"`
V2 *V2 `json:"id3v2"`
Debug bool `json:"-"`
fileHandle frames.FrameFile
}
// Version is an interface for individual version implementations
type Version interface {
Parse(frames.FrameFile) error
}
const (
readFromEnd = 2
readFromStart = 0
)
// Process will begin the opening and loading of File content
func (f *File) Process(h frames.FrameFile) *File {
f.fileHandle = h
// run through v1
f.V1 = &V1{Debug: f.Debug}
f.V1.Parse(f.fileHandle)
// run through v2
f.V2 = &V2{Debug: f.Debug}
f.V2.Parse(f.fileHandle)
return f
}
// PrettyPrint draws a nice representation of the file for the command line
func (f *File) PrettyPrint(o io.Writer, format string) {
switch format {
case "text":
fmt.Fprintf(o, "Artist: %s\n", f.GetArtist())
fmt.Fprintf(o, "Album: %s\n", f.GetAlbum())
fmt.Fprintf(o, "Title: %s\n", f.GetTitle())
case "yaml":
out, _ := yaml.Marshal(f)
fmt.Fprintf(o, string(out))
case "raw":
case "json":
fallthrough
default:
e := json.NewEncoder(o)
e.Encode(f)
}
}
// GetArtist will determine the ideal Artist string for use.
func (f *File) GetArtist() string {
a := f.V2.GetArtist()
if len(a) < 1 {
a = f.V1.Artist
}
return a
}
// GetAlbum will determine the ideal Album string for use.
func (f *File) GetAlbum() string {
a := f.V2.GetAlbum()
if len(a) < 1 {
a = f.V1.Album
}
return a
}
// GetTitle will provide the title found for the song.
func (f *File) GetTitle() string {
a := f.V2.GetTitle()
if len(a) < 1 {
a = f.V1.Title
}
return a
}