A website inspired by Last.fm that will keep track of your listening statistics
lastfm music statistics
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Make it possible to fetch images from coverartarchive

oscar345 fe906683 eb9bd386

+88 -5
+8
internal/image/image.go
··· 12 12 URL *url.URL 13 13 Width int 14 14 Height int 15 + 16 + CoverArtDetails CoverArtDetails 17 + } 18 + 19 + type CoverArtDetails struct { 20 + Front bool 21 + Back bool 22 + Approved bool 15 23 } 16 24 17 25 func fetchImageGeneral(ctx context.Context, client *http.Client, image Image) ([]byte, error) {
+80 -5
internal/image/release.go
··· 1 1 package image 2 2 3 - import "context" 3 + import ( 4 + "context" 5 + "encoding/json" 6 + "fmt" 7 + "io" 8 + "net/http" 9 + "net/url" 10 + 11 + "github.com/oscar345/keeptrack/pkg/enum" 12 + ) 4 13 5 14 type ReleaseImageFetcher interface { 6 15 ListImages(ctx context.Context, mbid string) ([]Image, error) 7 16 FetchImage(ctx context.Context, image Image) ([]byte, error) 8 17 } 9 18 10 - type ReleaseImageFetcherCoverArtArchive struct{} 19 + type ReleaseImageFetcherCoverArtArchive struct { 20 + client *http.Client 21 + } 11 22 12 23 var _ ReleaseImageFetcher = (*ReleaseImageFetcherCoverArtArchive)(nil) 13 24 14 25 func NewReleaseImageFetcherCoverArtArchive() *ReleaseImageFetcherCoverArtArchive { 15 - return &ReleaseImageFetcherCoverArtArchive{} 26 + return &ReleaseImageFetcherCoverArtArchive{ 27 + client: http.DefaultClient, 28 + } 16 29 } 17 30 18 31 func (f *ReleaseImageFetcherCoverArtArchive) ListImages(ctx context.Context, mbid string) ([]Image, error) { 19 - return nil, nil 32 + type archiveImage struct { 33 + Approved bool `json:"approved"` 34 + Back bool `json:"back"` 35 + Comment string `json:"comment"` 36 + Edit int `json:"edit"` 37 + Front bool `json:"front"` 38 + Image string `json:"image"` 39 + } 40 + 41 + type response struct { 42 + Images []archiveImage `json:"images"` 43 + } 44 + 45 + req, err := http.NewRequestWithContext( 46 + ctx, http.MethodGet, fmt.Sprintf("https://coverartarchive.org/%s/%s", "release-group", mbid), nil, 47 + ) 48 + if err != nil { 49 + return nil, err 50 + } 51 + 52 + resp, err := f.client.Do(req) 53 + if err != nil { 54 + return nil, err 55 + } 56 + defer resp.Body.Close() 57 + 58 + if resp.StatusCode != http.StatusOK { 59 + return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) 60 + } 61 + 62 + var data response 63 + if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { 64 + return nil, err 65 + } 66 + 67 + images := enum.Map(data.Images, func(item archiveImage) Image { 68 + imageUrl, _ := url.Parse(item.Image) 69 + return Image{ 70 + URL: imageUrl, 71 + CoverArtDetails: CoverArtDetails{ 72 + Approved: item.Approved, 73 + Back: item.Back, 74 + Front: item.Front, 75 + }, 76 + } 77 + }) 78 + 79 + return images, nil 20 80 } 21 81 22 82 func (f *ReleaseImageFetcherCoverArtArchive) FetchImage(ctx context.Context, image Image) ([]byte, error) { 23 - return nil, nil 83 + req, err := http.NewRequestWithContext(ctx, http.MethodGet, image.URL.String(), nil) 84 + if err != nil { 85 + return nil, err 86 + } 87 + 88 + resp, err := f.client.Do(req) 89 + if err != nil { 90 + return nil, err 91 + } 92 + defer resp.Body.Close() 93 + 94 + if resp.StatusCode != http.StatusOK { 95 + return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) 96 + } 97 + 98 + return io.ReadAll(resp.Body) 24 99 }