def test_get_release(self): with messybrainz.engine.connect() as connection: recording_msid = data.submit_recording(connection, recording) release_msid = data.get_release(connection, recording['release']) recording_data = data.load_recordings_from_msids( connection, [recording_msid])[0] self.assertEqual(release_msid, recording_data['ids']['release_msid'])
def test_get_artist_credit(self): with messybrainz.engine.connect() as connection: recording_msid = data.submit_recording(connection, recording) artist_msid = data.get_artist_credit(connection, recording['artist']) recording_data = data.load_recordings_from_msids( connection, [recording_msid])[0] self.assertEqual(artist_msid, recording_data['ids']['artist_msid'])
def load_recordings_from_msids(msids): """ Returns data for a recording with specified MessyBrainz ID. Args: msid (uuid): the MessyBrainz ID of the recording Returns: A dict containing the recording data for the recording with specified MessyBrainz ID """ with engine.begin() as connection: return data.load_recordings_from_msids(connection, msids)
def insert_single(connection, recording): """ Inserts a single recording into MessyBrainz. Args: connection: the sqlalchemy db connection to be used to execute queries recording: the recording to be inserted Returns: A dict containing the recording data for inserted recording """ gid = data.get_id_from_recording(connection, recording) if not gid: gid = data.submit_recording(connection, recording) loaded = data.load_recordings_from_msids(connection, [gid])[0] return loaded
def insert_test_data_with_metadata(self, user_id, neg_score=False): """ Insert test data with metadata into the database """ with msb_db.engine.connect() as connection: msid = get_id_from_meta_hash(connection, self.sample_recording) if msid is None: msid = submit_recording(connection, self.sample_recording) msid = str(msid) artists = load_recordings_from_msids(connection, [msid]) self.saved_artist_msid = artists[0]["ids"]["artist_msid"] self.sample_feedback_with_metadata[0]["recording_msid"] = msid query = """INSERT INTO mbid_mapping_metadata (recording_mbid, release_mbid, release_name, artist_credit_id, artist_mbids, artist_credit_name, recording_name) VALUES ('076255b4-1575-11ec-ac84-135bf6a670e3', '1fd178b4-1575-11ec-b98a-d72392cd8c97', 'release_name', 65, '{6a221fda-2200-11ec-ac7d-dfa16a57158f}'::UUID[], 'artist name', 'recording name')""" with ts.engine.connect() as connection: connection.execute(sqlalchemy.text(query)) query = """INSERT INTO mbid_mapping (recording_msid, recording_mbid, match_type, last_updated) VALUES (:msid, :mbid, :match_type, now())""" with ts.engine.connect() as connection: connection.execute( sqlalchemy.text(query), { "msid": msid, "mbid": "076255b4-1575-11ec-ac84-135bf6a670e3", "match_type": "exact_match" }) for fb in self.sample_feedback_with_metadata: db_feedback.insert( Feedback(user_id=user_id, recording_msid=fb["recording_msid"], score=fb["score"])) return len(self.sample_feedback_with_metadata)
def test_load_recordings_from_msids(self): with messybrainz.engine.connect() as connection: recording_msid = data.submit_recording(connection, recording) result = data.load_recordings_from_msids(connection, [recording_msid])[0] self.assertDictEqual(result['payload'], recording)
def get_feedback_for_user(user_id: int, limit: int, offset: int, score: int = None, metadata: bool = False) -> List[Feedback]: """ Get a list of recording feedback given by the user in descending order of their creation Args: user_id: the row ID of the user in the DB score: the score value by which the results are to be filtered. If 1 then returns the loved recordings, if -1 returns hated recordings. limit: number of rows to be returned offset: number of feedback to skip from the beginning metadata: fetch metadata for the returned feedback recordings Returns: A list of Feedback objects """ args = {"user_id": user_id, "limit": limit, "offset": offset} query = """ SELECT user_id, "user".musicbrainz_id AS user_name, recording_msid::text, score, recording_feedback.created FROM recording_feedback JOIN "user" ON "user".id = recording_feedback.user_id WHERE user_id = :user_id """ if score: query += " AND score = :score" args["score"] = score query += """ ORDER BY recording_feedback.created DESC LIMIT :limit OFFSET :offset """ with db.engine.connect() as connection: result = connection.execute(sqlalchemy.text(query), args) feedback = [Feedback(**dict(row)) for row in result.fetchall()] if metadata and len(feedback) > 0: msids = [f.recording_msid for f in feedback] index = {f.recording_msid: f for f in feedback} # Fetch the artist and track names from MSB with msb_db.engine.connect() as connection: try: msb_recordings = load_recordings_from_msids(connection, msids) except NoDataFoundException: msb_recordings = [] artist_msids = {} if msb_recordings: for rec in msb_recordings: index[rec["ids"]["recording_msid"]].track_metadata = { "artist_name": rec["payload"]["artist"], "release_name": rec["payload"].get("release_name", ""), "track_name": rec["payload"]["title"] } artist_msids[rec["ids"] ["recording_msid"]] = rec["ids"]["artist_msid"] # Fetch the mapped MBIDs from the mapping query = """SELECT recording_msid::TEXT, m.recording_mbid::TEXT, release_mbid::TEXT, artist_mbids::TEXT[] FROM mbid_mapping m JOIN mbid_mapping_metadata mm ON m.recording_mbid = mm.recording_mbid WHERE recording_msid in :msids ORDER BY recording_msid""" with timescale.engine.connect() as connection: result = connection.execute(sqlalchemy.text(query), msids=tuple(msids)) for row in result.fetchall(): if row["recording_mbid"] is not None: index[row["recording_msid"]].track_metadata[ 'additional_info'] = { "recording_mbid": row["recording_mbid"], "release_mbid": row["release_mbid"], "artist_mbids": row["artist_mbids"], "artist_msid": artist_msids[row["recording_msid"]] } return feedback