/
rvrn.go
276 lines (217 loc) · 6.34 KB
/
rvrn.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
package main
import (
_ "github.com/bmizerany/pq"
"github.com/astaxie/beedb"
"os"
"io/ioutil"
"path/filepath"
"database/sql"
"encoding/json"
"net/http"
"html/template"
"fmt"
"log"
"regexp"
"time"
"taglib"
)
var song_db beedb.Model
var BASE_URL = "http://developer.echonest.com/api/v4/"
var API_KEY = "make one, they are free :D"
var ROOT_MUSIC_FOLDER = "music"
var templates = template.Must(template.ParseFiles("index.html", "error.html", "library.html"))
var reqMerger = regexp.MustCompile(" ")
type Page struct {
Title string
Body []byte
Artists []Artist
}
type echoResponse struct {
Response rData "response"
}
type rData struct {
Status rStatus "status"
Artists []Artist "artists"
Songs []Song "songs"
}
type rStatus struct {
Version string "version"
Code int "code"
Message string "message"
}
type Artist struct {
Name string "name"
Id string "id"
Activity float64 "hotttnesss"
Songs []Song "songs"
}
type Song struct {
Sid int `beedb:"PK"`
ArtistId string "artist_id"
ArtistName string "artist_name"
Title string "title"
Id string "id"
Album string
Comment string
Genre string
Year int
TrackNumber int
Length int
Bitrate int
Samplerate int
Channels int
Created time.Time
}
func grabArtists(artistQuery string) ([]Artist, []byte, error) {
aResp, _, err := makeRequest(artistQuery, "artist")
artist_list, rStatus, err := processResponse(aResp)
//fmt.Print("artistResp: ", processedJSON, "\n")
//fmt.Print(processedJSON.Response.Artists, "\n")
return artist_list.Response.Artists, rStatus, err
}
func grabTracks(artist Artist) ([]Song, []byte, error) {
tResp, _, err := makeRequest(artist.Name, "playlist")
processedJSON, rStatus, err := processResponse(tResp)
song_list := processedJSON.Response.Songs
for i := 0; i < len(song_list); i++ {
var song = processedJSON.Response.Songs[i]
fmt.Print(song, "\n")
t := make(map[string]interface{})
t["title"] = song_list[i].Title
song_db.SetTable("song").Where("title == ?", song_list[i].Title).Update(t)
}
//fmt.Print("tResp: ", processedJSON, "\n")
//fmt.Print("tracks: ", processedJSON.Response.Songs, "\n")
return song_list, rStatus, err
}
func makeRequest(request string, requestType string) ([]byte, []byte, error) {
var queryString string
var queryStatus []byte
switch requestType {
case "artist":
queryString = BASE_URL+"artist/search?api_key="+API_KEY+"&name="+request
case "track":
queryString = BASE_URL+"song/search?api_key="+API_KEY+"&artist="+request+"&results=1"
case "playlist":
queryString = BASE_URL+"playlist/basic?api_key="+API_KEY+"&artist="+request+"&format=json&results=2&type=artist-radio"
default:
fmt.Print("failed to create request string, check request type")
}
//fmt.Print("\n")
//fmt.Print(queryString)
//fmt.Print("\n")
requestString := string(reqMerger.ReplaceAll([]byte(queryString), []byte("+")))
echonest_resp, err := http.Get(requestString)
if err != nil {
sbody := "Error while sending EchoNest query"
queryStatus := []byte(sbody)
return nil, queryStatus, err
}
jsonData, err := ioutil.ReadAll(echonest_resp.Body)
echonest_resp.Body.Close()
if err != nil {
sbody := "Error while decoding JSON response"
queryStatus := []byte(sbody)
return nil, queryStatus, err
}
return jsonData, queryStatus, err
}
func processResponse(resp []byte) (echoResponse, []byte, error) {
//byte slice for storing status of the request and error messages
var respStatus []byte
//var for holding JSON data in response
var qResp echoResponse
//decode JSON response and get artists and songs
err := json.Unmarshal(resp, &qResp)
//handle errors internal to the nginx server
if err != nil {
sbody := "Internal Error in Nginx, check code"
respStatus := []byte(sbody)
return qResp, respStatus, err
}
return qResp, respStatus, err
}
func search(searchString string) ([]byte, []Artist, error) {
artistList, body, err := grabArtists(searchString)
for i := 0; i < len(artistList); i++ {
artistList[i].Songs, _, err = grabTracks(artistList[i])
}
//fmt.Print("finalArtists: ", artistList, "\n")
return body, artistList, err
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
title := "Search by Artist"
sbody := "Type Artist name and hit Submit"
body := []byte(sbody)
p := &Page{Title: title, Body: body}
renderTemplate(w, "index", p)
}
func searchHandler(w http.ResponseWriter, r *http.Request) {
title := "Search by Artist"
searchString := r.FormValue("name")
body, aList, err := search(searchString)
p := &Page{Title: title, Body: body, Artists: aList}
//fmt.Print(err)
//fmt.Print("\n")
if err != nil {
renderTemplate(w, "error", p)
} else {
renderTemplate(w, "index", p)
}
}
func libraryHandler(w http.ResponseWriter, r *http.Request) {
title := "Library"
sbody := "Click 'Add' to add tracks"
body := []byte(sbody)
p := &Page{Title: title, Body: body}
renderTemplate(w, "library", p)
}
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
err := templates.ExecuteTemplate(w, tmpl+".html", p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func saveSongInfo(path string) {
var song Song
f := taglib.Open(path)
if f == nil { return }
defer f.Close()
tags := f.GetTags()
props := f.GetProperties()
song.Album = tags.Album
song.Comment = tags.Comment
song.Genre = tags.Genre
song.Year = tags.Year
song.TrackNumber = tags.Track
song.Length = props.Length
song.Bitrate = props.Bitrate
song.Samplerate = props.Samplerate
song.Channels = props.Channels
song.ArtistName = tags.Artist
song.Created = time.Now()
err := song_db.Save(&song)
if err != nil {
fmt.Println(err)
}
}
func openDbConnection(engine string, arg_string string) {
db, err := sql.Open(engine, arg_string)
if err != nil {
panic(err)
}
song_db = beedb.New(db, "pg")
}
func insertSongInfo(path string, f os.FileInfo, err error) error {
saveSongInfo(path)
return nil
}
func main() {
openDbConnection("postgres", "user=nate dbname=rvrn")
err := filepath.Walk(ROOT_MUSIC_FOLDER, insertSongInfo)
fmt.Print(err, "\n")
http.HandleFunc("/", indexHandler)
http.HandleFunc("/search", searchHandler)
http.HandleFunc("/library", libraryHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}