fix: use the open REST API to extact data instead of lyrics page
This commit is contained in:
parent
dd0ee8723b
commit
213f90d779
83
lyrics.go
83
lyrics.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
@ -18,6 +19,27 @@ type song struct {
|
|||||||
About [2]string
|
About [2]string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type songResponse struct {
|
||||||
|
Response struct {
|
||||||
|
Song struct {
|
||||||
|
ArtistNames string `json:"artist_names"`
|
||||||
|
Image string `json:"song_art_image_thumbnail_url"`
|
||||||
|
Title string
|
||||||
|
Description struct {
|
||||||
|
Plain string
|
||||||
|
}
|
||||||
|
CustomPerformances []customPerformance `json:"custom_performances"`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type customPerformance struct {
|
||||||
|
Label string
|
||||||
|
Artists []struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (s *song) parseLyrics(doc *goquery.Document) {
|
func (s *song) parseLyrics(doc *goquery.Document) {
|
||||||
doc.Find("[data-lyrics-container='true']").Each(func(i int, ss *goquery.Selection) {
|
doc.Find("[data-lyrics-container='true']").Each(func(i int, ss *goquery.Selection) {
|
||||||
h, err := ss.Html()
|
h, err := ss.Html()
|
||||||
@ -28,44 +50,57 @@ func (s *song) parseLyrics(doc *goquery.Document) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *song) parseMetadata(doc *goquery.Document) {
|
func (s *song) parseSongData(doc *goquery.Document) {
|
||||||
artist := doc.Find("a[class*='Artist']").First().Text()
|
attr, exists := doc.Find("meta[property='twitter:app:url:iphone']").Attr("content")
|
||||||
title := doc.Find("h1[class*='Title']").First().Text()
|
|
||||||
image, exists := doc.Find("meta[property='og:image']").Attr("content")
|
|
||||||
if exists {
|
if exists {
|
||||||
s.Image = extractURL(image)
|
songID := strings.Replace(attr, "genius://songs/", "", 1)
|
||||||
|
u := fmt.Sprintf("https://genius.com/api/songs/%s?text_format=plain", songID)
|
||||||
|
|
||||||
|
res, err := http.Get(u)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.Title = title
|
defer res.Body.Close()
|
||||||
s.Artist = artist
|
|
||||||
|
var data songResponse
|
||||||
|
decoder := json.NewDecoder(res.Body)
|
||||||
|
err = decoder.Decode(&data)
|
||||||
|
if err != nil {
|
||||||
|
logger.Errorln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *song) parseCredits(doc *goquery.Document) {
|
songData := data.Response.Song
|
||||||
credits := make(map[string]string)
|
s.Artist = songData.ArtistNames
|
||||||
|
s.Image = songData.Image
|
||||||
|
s.Title = songData.Title
|
||||||
|
s.About[0] = songData.Description.Plain
|
||||||
|
s.About[1] = truncateText(songData.Description.Plain)
|
||||||
|
s.Credits = make(map[string]string)
|
||||||
|
|
||||||
doc.Find("[class*='SongInfo__Credit']").Each(func(i int, ss *goquery.Selection) {
|
for _, perf := range songData.CustomPerformances {
|
||||||
key := ss.Children().First().Text()
|
var artists []string
|
||||||
value := ss.Children().Last().Text()
|
for _, artist := range perf.Artists {
|
||||||
credits[key] = value
|
artists = append(artists, artist.Name)
|
||||||
})
|
}
|
||||||
|
s.Credits[perf.Label] = strings.Join(artists, ", ")
|
||||||
s.Credits = credits
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *song) parseAbout(doc *goquery.Document) {
|
func truncateText(text string) string {
|
||||||
s.About[0] = doc.Find("[class*='SongDescription__Content']").Text()
|
textArr := strings.Split(text, "")
|
||||||
summary := strings.Split(s.About[0], "")
|
|
||||||
|
|
||||||
if len(summary) > 250 {
|
if len(textArr) > 250 {
|
||||||
s.About[1] = strings.Join(summary[0:250], "") + "..."
|
return strings.Join(textArr[0:250], "") + "..."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return text
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *song) parse(doc *goquery.Document) {
|
func (s *song) parse(doc *goquery.Document) {
|
||||||
s.parseLyrics(doc)
|
s.parseLyrics(doc)
|
||||||
s.parseMetadata(doc)
|
s.parseSongData(doc)
|
||||||
s.parseCredits(doc)
|
|
||||||
s.parseAbout(doc)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func lyricsHandler(w http.ResponseWriter, r *http.Request) {
|
func lyricsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
{{template "navbar"}}
|
{{template "navbar"}}
|
||||||
<div id="container">
|
<div id="container">
|
||||||
<div id="metadata">
|
<div id="metadata">
|
||||||
<img src="{{.Image}}"/>
|
<img src="{{extractURL .Image}}"/>
|
||||||
<h2>{{.Artist}}</h2>
|
<h2>{{.Artist}}</h2>
|
||||||
<h1>{{.Title}}</h1>
|
<h1>{{.Title}}</h1>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user