-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.go
97 lines (74 loc) · 1.82 KB
/
cli.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
"github.com/bogem/id3v2"
"github.com/kalbhor/MSeize/spotify"
"github.com/kalbhor/MSeize/youtube"
)
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
var query string
var choice int
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter song name : ")
query, _ = reader.ReadString('\n')
query = strings.TrimSuffix(query, "\n")
videos, err := youtube.Search(query)
checkErr(err)
for i, video := range videos {
fmt.Printf("[%v] %v\n", i+1, video.Title)
}
fmt.Print("Enter song number : ")
fmt.Scan(&choice)
if choice > len(videos) {
log.Fatal("Wrong input")
}
fmt.Println("--------------")
path, err := youtube.Download(videos[choice-1], "./")
checkErr(err)
id := os.Getenv("SPOTIFY_ID")
secret := os.Getenv("SPOTIFY_SECRET")
client, err := spotify.Auth(id, secret)
checkErr(err)
metadata, err := spotify.GetMetadata(client, query)
checkErr(err)
tag, err := id3v2.Open(path, id3v2.Options{Parse: true})
checkErr(err)
defer tag.Close()
tag.SetTitle(metadata.Title)
tag.SetAlbum(metadata.Album)
artists := strings.Join(metadata.Artists, ",")
tag.SetArtist(artists)
pic := id3v2.PictureFrame{
Encoding: id3v2.EncodingUTF8,
MimeType: "image/jpeg",
PictureType: id3v2.PTFrontCover,
Description: "Front cover",
Picture: metadata.Image,
}
tag.AddAttachedPicture(pic)
TrackNumber := strconv.Itoa(metadata.TrackNumber)
trackFrame := id3v2.TextFrame{
Encoding: id3v2.EncodingUTF8,
Text: TrackNumber,
}
tag.AddFrame("TRCK", trackFrame)
DiscNumber := strconv.Itoa(metadata.DiscNumber)
discFrame := id3v2.TextFrame{
Encoding: id3v2.EncodingUTF8,
Text: DiscNumber,
}
tag.AddFrame("TPOS", discFrame)
if err = tag.Save(); err != nil {
log.Fatal("Error : ", err)
}
}