Skip to content

Commit

Permalink
feat(lastfm): strip copyright text from albumInfo/artistInfo responses
Browse files Browse the repository at this point in the history
Release-As: 0.16.3
  • Loading branch information
sentriz committed Dec 10, 2023
1 parent b27c02f commit aa82b94
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lastfm/client.go
Expand Up @@ -8,8 +8,10 @@ import (
"fmt"
"net/http"
"net/url"
"regexp"
"sort"
"strconv"
"strings"
"time"

"github.com/andybalholm/cascadia"
Expand Down Expand Up @@ -57,6 +59,10 @@ func (c *Client) ArtistGetInfo(artistName string) (Artist, error) {
if err != nil {
return Artist{}, fmt.Errorf("make request: %w", err)
}

resp.Artist.Bio.Summary = cleanLicenceText(resp.Artist.Bio.Summary)
resp.Artist.Bio.Content = cleanLicenceText(resp.Artist.Bio.Content)

return resp.Artist, nil
}

Expand All @@ -77,6 +83,9 @@ func (c *Client) AlbumGetInfo(artistName, albumName string) (Album, error) {
return Album{}, fmt.Errorf("make request: %w", err)
}

resp.Album.Wiki.Summary = cleanLicenceText(resp.Album.Wiki.Summary)
resp.Album.Wiki.Content = cleanLicenceText(resp.Album.Wiki.Content)

return resp.Album, nil
}

Expand Down Expand Up @@ -324,3 +333,15 @@ func GetParamSignature(params url.Values, secret string) string {
hash := md5.Sum([]byte(toHash))
return hex.EncodeToString(hash[:])
}

var doublePuncExpr = regexp.MustCompile(`\.\s+\.\s+`)
var licenceExpr = regexp.MustCompile(`(?i)user-contributed text.*`)

func cleanLicenceText(text string) string {
text = licenceExpr.ReplaceAllString(text, "")
text = doublePuncExpr.ReplaceAllString(text, ". ")
text = strings.ReplaceAll(text, " .", ".")
text = strings.Join(strings.Fields(text), " ")
text = strings.TrimSpace(text)
return text
}

0 comments on commit aa82b94

Please sign in to comment.