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 missing_mb_data(user_name: str): """ Shows missing musicbrainz data """ user = _get_user(user_name) missing_data = get_user_missing_musicbrainz_data(user.id, "cf") if missing_data is None: missing_data_list = [] else: missing_data_list = missing_data.data.dict( )["missing_musicbrainz_data"] for item in missing_data_list: item["listened_at"] += "Z" props = { "missingData": missing_data_list, "user": { "id": user.id, "name": user.musicbrainz_id, } } return render_template("user/missing_data.html", user=user, props=ujson.dumps(props))
def get_missing_musicbrainz_data(user_name): """ Get musicbrainz data sorted on "listened_at" that the user has submitted to ListenBrainz but has not submitted to MusicBrainz. A sample response from the endpoint may look like: .. code-block:: json { "payload": { "last_updated": 1588494361, "data": [ { "artist_msid": "fd32e967-b874-44b2-809c-3862f714813c", "artist_name": "Red City Radio", "listened_at": "2020-04-29 23:40:47", "recording_msid": "78f63ece-86e1-48bf-a7ff-29793d4a84e6", "release_msid": "47818692-f669-4846-acbc-cb0a69987aee", "release_name": "The Dangers Of Standing Still", "track_name": "Never Bring A Cup Of Water To A Gunfight" }, { "artist_msid": "fd32e967-b874-44b2-809c-3862f714813c", "artist_name": "Red City Radio", "listened_at": "2020-04-29 23:37:57", "recording_msid": "d226200a-a9be-4e9e-9f7c-d74a71647893", "release_msid": "47818692-f669-4846-acbc-cb0a69987aee", "release_name": "The Dangers Of Standing Still", "track_name": "Nathaniel Martinez" } ], "count": 2, "offset": 4, "total_data_count": 25, "user_name": "Vansika" } } :param count: Optional, number of records to return, Default: :data:`~webserver.views.api.DEFAULT_ITEMS_PER_GET` Max: :data:`~webserver.views.api.MAX_ITEMS_PER_GET` :type count: ``int`` :param offset: Optional, number of records to skip from the beginning, for pagination. Ex. An offset of 5 means the 5 records will be skipped, defaults to 0 :type offset: ``int`` :statuscode 200: Successful query, you have data! :statuscode 400: Bad request, check ``response['error']`` for more details :statuscode 404: User not found. :statuscode 204: Missing MusicBrainz data for the user not calculated , empty response will be returned """ # source indicates the *source* script/algorithm by which the missing musicbrainz data was calculated. # The source may change in future source = 'cf' user = db_user.get_by_mb_id(user_name) if user is None: raise APINotFound("Cannot find user: {}".format(user_name)) offset = get_non_negative_param('offset', default=0) count = get_non_negative_param('count', default=DEFAULT_ITEMS_PER_GET) count = min(count, MAX_ITEMS_PER_GET) data = db_missing_musicbrainz_data.get_user_missing_musicbrainz_data( user['id'], source) if data is None: err_msg = 'Missing MusicBrainz data for {} not calculated'.format( user_name) raise APINoContent(err_msg) missing_musicbrainz_data_list = getattr( data, 'data').dict()['missing_musicbrainz_data'] missing_musicbrainz_data_list_filtered = missing_musicbrainz_data_list[ offset:count] for index in range(len(missing_musicbrainz_data_list_filtered)): missing_musicbrainz_data_list_filtered[index]["listened_at"] += "Z" payload = { 'payload': { 'user_name': user_name, 'last_updated': int(getattr(data, 'created').timestamp()), 'count': len(missing_musicbrainz_data_list_filtered), 'total_data_count': len(missing_musicbrainz_data_list), 'offset': offset, 'data': missing_musicbrainz_data_list_filtered } } return jsonify(payload)