def handle_missing_musicbrainz_data(data): """ Insert user missing musicbrainz data i.e data submitted to ListenBrainz but not MusicBrainz. """ musicbrainz_id = data['musicbrainz_id'] user = db_user.get_by_mb_id(musicbrainz_id) if not user: return current_app.logger.debug( "Inserting missing musicbrainz data for {}".format(musicbrainz_id)) missing_musicbrainz_data = data['missing_musicbrainz_data'] source = data['source'] try: db_missing_musicbrainz_data.insert_user_missing_musicbrainz_data( user['id'], UserMissingMusicBrainzDataJson( **{'missing_musicbrainz_data': missing_musicbrainz_data}), source) except ValidationError: current_app.logger.error( """ValidationError while inserting missing MusicBrainz data from source "{source}" for user with musicbrainz_id: {musicbrainz_id}. Data: {data}""" .format(musicbrainz_id=musicbrainz_id, data=json.dumps(data, indent=3), source=source), exc_info=True) current_app.logger.debug( "Missing musicbrainz data for {} inserted".format(musicbrainz_id))
def test_handle_missing_musicbrainz_data(self, mock_get_by_mb_id, mock_db_insert): data = { 'type': 'missing_musicbrainz_data', 'musicbrainz_id': 'vansika', 'missing_musicbrainz_data': [{ "artist_name": "Katty Peri", "listened_at": "2020-04-29 23:56:23", "release_name": "No Place Is Home", "recording_name": "How High" }], 'source': 'cf' } mock_get_by_mb_id.return_value = {'id': 1, 'musicbrainz_id': 'vansika'} with self.app.app_context(): handle_missing_musicbrainz_data(data) mock_db_insert.assert_called_with( 1, UserMissingMusicBrainzDataJson(missing_musicbrainz_data=[ UserMissingMusicBrainzDataRecord( artist_name="Katty Peri", listened_at="2020-04-29 23:56:23", release_name="No Place Is Home", recording_name="How High") ]), 'cf')
def insert_user_missing_musicbrainz_data( user_id: int, missing_musicbrainz_data: UserMissingMusicBrainzDataJson, source: str): """ Insert missing musicbrainz data that a user has submitted to ListenBrainz but has not submitted to MusicBrainz in the db. Args: user_id : row id of the user. data : Data that is submitted to ListenBrainz by the users but is not submitted to MusicBrainz. source : Source of generation of missing MusicBrainz data. """ with db.engine.connect() as connection: connection.execute( sqlalchemy.text(""" INSERT INTO missing_musicbrainz_data (user_id, data, source) VALUES (:user_id, :missing_musicbrainz_data, :source) ON CONFLICT (user_id) DO UPDATE SET user_id = :user_id, data = :missing_musicbrainz_data, source = :source, created = NOW() """), { 'user_id': user_id, 'missing_musicbrainz_data': ujson.dumps(missing_musicbrainz_data.dict()), 'source': source })
def handle_missing_musicbrainz_data(data): """ Insert user missing musicbrainz data i.e data submitted to ListenBrainz but not MusicBrainz. """ user_id = data['user_id'] user = db_user.get(user_id) if not user: return current_app.logger.debug(f"Inserting missing musicbrainz data for {user['musicbrainz_id']}") missing_musicbrainz_data = data['missing_musicbrainz_data'] source = data['source'] try: db_missing_musicbrainz_data.insert_user_missing_musicbrainz_data( user['id'], UserMissingMusicBrainzDataJson(missing_musicbrainz_data=missing_musicbrainz_data), source ) except ValidationError: current_app.logger.error(f""" ValidationError while inserting missing MusicBrainz data from source "{source}" for user with musicbrainz_id: {user["musicbrainz_id"]}. Data: {json.dumps(data, indent=3)}""", exc_info=True) current_app.logger.debug(f"Missing musicbrainz data for {user['musicbrainz_id']} inserted")
def setUp(self): super(MissingMusicBrainzDataViewsTestCase, self).setUp() self.user = db_user.get_or_create(1, 'vansika_1') self.user2 = db_user.get_or_create(2, 'vansika_2') with open(self.path_to_data_file('missing_musicbrainz_data.json'), 'r') as f: missing_musicbrainz_data = json.load(f) db_missing_musicbrainz_data.insert_user_missing_musicbrainz_data( user_id=self.user['id'], missing_musicbrainz_data=UserMissingMusicBrainzDataJson( **{'missing_musicbrainz_data': missing_musicbrainz_data}), source='cf') self.data = db_missing_musicbrainz_data.get_user_missing_musicbrainz_data( user_id=self.user['id'], source='cf')
def test_handle_missing_musicbrainz_data(self, mock_get_by_mb_id, mock_db_insert): data = { 'type': 'missing_musicbrainz_data', 'musicbrainz_id': 'vansika', 'missing_musicbrainz_data': [{ "artist_msid": "f26d35e3-5fdd-43cf-8b94-71936451bc07", "artist_name": "Katty Peri", "listened_at": "2020-04-29 23:56:23", "recording_msid": "568eeea3-9255-4878-9df8-296043344e04", "release_msid": "8c5ba30c-4851-48fd-ac02-1b194cdb34d1", "release_name": "No Place Is Home", "track_name": "How High" }], 'source': 'cf' } mock_get_by_mb_id.return_value = {'id': 1, 'musicbrainz_id': 'vansika'} with self.app.app_context(): handle_missing_musicbrainz_data(data) mock_db_insert.assert_called_with( 1, UserMissingMusicBrainzDataJson(missing_musicbrainz_data=[ UserMissingMusicBrainzDataRecord( artist_msid="f26d35e3-5fdd-43cf-8b94-71936451bc07", artist_name="Katty Peri", listened_at="2020-04-29 23:56:23", recording_msid="568eeea3-9255-4878-9df8-296043344e04", release_msid="8c5ba30c-4851-48fd-ac02-1b194cdb34d1", release_name="No Place Is Home", track_name="How High") ]), 'cf')