~phw/discourse-listenbrainz

9f0f35e40b7c00ec793527cd44f7d0427d67e20e — Philipp Wolfer 1 year, 7 months ago dbd13e8
Load artist and release info for playing-now

This gives direct access to caa_release_mbid and full artist credits list like for regular listens
2 files changed, 38 insertions(+), 5 deletions(-)

M CHANGES.md
M lib/helpers/listen_helper.rb
M CHANGES.md => CHANGES.md +1 -0
@@ 1,6 1,7 @@
# Discourse ListenBrainz Changelog

## 0.12 - unreleased
- Load artist credits and CAA release ID info for playing-now
- Do not load covers if data saving is enabled in browser



M lib/helpers/listen_helper.rb => lib/helpers/listen_helper.rb +37 -5
@@ 20,19 20,23 @@ module ListenBrainz
        end

        def self.enhance_metadata(listen)
            track_metadata = listen&.[]('track_metadata')
            track_metadata = listen&.dig('track_metadata')
            if track_metadata
                # Listening now data has no mapping info
                if !track_metadata['mbid_mapping']
                    metadata = Rails.cache.fetch(
                        cache_key("metadata/#{track_metadata['artist_name']}:#{track_metadata['track_name']}"),
                        expires_in: CACHE_DURATION
                    ) do
                        lb_api.get('/metadata/lookup/', {
                            :artist_name => track_metadata['artist_name'],
                            :recording_name => track_metadata['track_name'],
                        })
                            artist_name: track_metadata['artist_name'],
                            recording_name: track_metadata['track_name'],
                            metadata: 'true',
                            inc: 'artist release',
                        }) || {}
                    end
                    track_metadata['mbid_mapping'] = metadata || {}
                    # Normalize the metadata to the same format as for regular listens
                    track_metadata['mbid_mapping'] = normalize_metadata(metadata)
                end

                additional_info = track_metadata['additional_info'] || {}


@@ 64,8 68,36 @@ module ListenBrainz
            end
        end

        private

        def self.cache_key(key)
            "listenbrainz/#{key}"
        end

        def self.normalize_metadata(metadata)
            metadata['caa_release_mbid'] = metadata.dig('metadata', 'release', 'caa_release_mbid')
            if !metadata['artists']
                artists = normalize_artists(
                    metadata.dig('metadata', 'artist', 'artists'),
                    metadata['artist_mbids']
                )
                metadata['artists'] = artists if artists
            end
            metadata.delete('metadata')
            metadata
        end

        def self.normalize_artists(artist_credits, artist_mbids)
            return nil if !artist_credits
            artists = []
            artist_credits.each_with_index do |artist_credit, i|
                artists.push({
                    artist_credit_name: artist_credit['name'],
                    join_phrase: artist_credit['join_phrase'],
                    artist_mbid: artist_mbids&.[](i),
                })
            end
            artists
        end
    end
end