forked from rramiachraf/dumb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalbum.go
139 lines (114 loc) · 2.9 KB
/
album.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/gorilla/mux"
)
type album struct {
Artist string
Name string
Image string
About [2]string
Tracks []Track
}
type Track struct {
Title string
Url string
}
type albumMetadata struct {
Album struct {
Id int `json:"id"`
Image string `json:"cover_art_thumbnail_url"`
Name string `json:"name"`
Description string `json:"description_preview"`
Artist `json:"artist"`
}
AlbumAppearances []AlbumAppearances `json:"album_appearances"`
}
type AlbumAppearances struct {
Id int `json:"id"`
TrackNumber int `json:"track_number"`
Song struct {
Title string `json:"title"`
Url string `json:"url"`
}
}
type Artist struct {
Name string `json:"name"`
}
func (a *album) parseAlbumData(doc *goquery.Document) {
pageMetadata, exists := doc.Find("meta[itemprop='page_data']").Attr("content")
if !exists {
return
}
var albumMetadataFromPage albumMetadata
json.Unmarshal([]byte(pageMetadata), &albumMetadataFromPage)
albumData := albumMetadataFromPage.Album
a.Artist = albumData.Artist.Name
a.Name = albumData.Name
a.Image = albumData.Image
a.About[0] = albumData.Description
a.About[1] = truncateText(albumData.Description)
for _, track := range albumMetadataFromPage.AlbumAppearances {
url := strings.Replace(track.Song.Url, "https://genius.com", "", -1)
a.Tracks = append(a.Tracks, Track{Title: track.Song.Title, Url: url})
}
}
func (a *album) parse(doc *goquery.Document) {
a.parseAlbumData(doc)
}
func albumHandler(w http.ResponseWriter, r *http.Request) {
artist := mux.Vars(r)["artist"]
albumName := mux.Vars(r)["albumName"]
id := fmt.Sprintf("%s/%s", artist, albumName)
if data, err := getCache(id); err == nil {
render("album", w, data)
return
}
url := fmt.Sprintf("https://genius.com/albums/%s/%s", artist, albumName)
resp, err := sendRequest(url)
if err != nil {
logger.Errorln(err)
w.WriteHeader(http.StatusInternalServerError)
render("error", w, map[string]string{
"Status": "500",
"Error": "cannot reach genius servers",
})
return
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
w.WriteHeader(http.StatusNotFound)
render("error", w, map[string]string{
"Status": "404",
"Error": "page not found",
})
return
}
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
logger.Errorln(err)
w.WriteHeader(http.StatusInternalServerError)
render("error", w, map[string]string{
"Status": "500",
"Error": "something went wrong",
})
return
}
cf := doc.Find(".cloudflare_content").Length()
if cf > 0 {
logger.Errorln("cloudflare got in the way")
render("error", w, map[string]string{
"Status": "500",
"Error": "damn cloudflare, issue #21 on GitHub",
})
return
}
var a album
a.parse(doc)
render("album", w, a)
setCache(id, a)
}