コード例 #1
0
    def test_insert_user_stats_mult_ranges_recording(self):
        """ Test if multiple time range data is inserted correctly """
        with open(self.path_to_data_file('user_top_recordings_db.json')) as f:
            recordings_data = json.load(f)

        db_stats.insert_user_recordings(user_id=self.user['id'], recordings=UserRecordingStatJson(**{'year': recordings_data}))
        db_stats.insert_user_recordings(user_id=self.user['id'],
                                        recordings=UserRecordingStatJson(**{'all_time': recordings_data}))

        result = db_stats.get_user_recordings(1, 'all_time')
        self.assertDictEqual(result.all_time.dict(), recordings_data)

        result = db_stats.get_user_recordings(1, 'year')
        self.assertDictEqual(result.year.dict(), recordings_data)
コード例 #2
0
    def test_insert_user_recordings(self):
        """ Test if recording stats are inserted correctly """
        with open(self.path_to_data_file('user_top_recordings_db.json')) as f:
            recordings_data = json.load(f)
        db_stats.insert_user_recordings(user_id=self.user['id'],
                                        recordings=UserRecordingStatJson(**{'all_time': recordings_data}))

        result = db_stats.get_user_recordings(user_id=self.user['id'], stats_range='all_time')
        self.assertDictEqual(result.all_time.dict(), recordings_data)
コード例 #3
0
def get_recording(user_name):
    """
    Get top recordings for user ``user_name``.


    A sample response from the endpoint may look like::

        {
            "payload": {
                "recordings": [
                    {
                        "artist_mbids": [],
                        "artist_msid": "7addbcac-ae39-4b4c-a956-53da336d68e8",
                        "artist_name": "Ellie Goulding",
                        "listen_count": 25,
                        "recording_mbid": "0fe11cd3-0be4-467b-84fa-0bd524d45d74",
                        "recording_msid": "c6b65a7e-7284-433e-ac5d-e3ff0aa4738a",
                        "release_mbid": "",
                        "release_msid": "de97ca87-36c4-4995-a5c9-540e35944352",
                        "release_name": "Delirium (Deluxe)",
                        "track_name": "Love Me Like You Do - From \"Fifty Shades of Grey\""
                    },
                    {
                        "artist_mbids": [],
                        "artist_msid": "3b155259-b29e-4515-aa62-cb0b917f4cfd",
                        "artist_name": "The Fray",
                        "listen_count": 23,
                        "recording_mbid": "0008ab49-a6ad-40b5-aa90-9d2779265c22",
                        "recording_msid": "4b5bf07c-782f-4324-9242-bf56e4ba1e57",
                        "release_mbid": "",
                        "release_msid": "2b2a93c3-a0bd-4f46-8507-baf5ad291966",
                        "release_name": "How to Save a Life",
                        "track_name": "How to Save a Life"
                    },
                ],
                "count": 2,
                "total_recording_count": 175,
                "range": "all_time",
                "last_updated": 1588494361,
                "user_id": "John Doe",
                "from_ts": 1009823400,
                "to_ts": 1590029157
            }
        }

    .. note::
        - This endpoint is currently in beta
        - We only calculate the top 1000 all_time recordings
        - ``artist_mbids``, ``artist_msid``, ``release_name``, ``release_mbid``, ``release_msid``,
          ``recording_mbid`` and ``recording_msid`` are optional fields and may not be present in all the responses

    :param count: Optional, number of recordings 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 recordings to skip from the beginning, for pagination.
        Ex. An offset of 5 means the top 5 recordings will be skipped, defaults to 0
    :type offset: ``int``
    :param range: Optional, time interval for which statistics should be collected, possible values are ``week``,
        ``month``, ``year``, ``all_time``, defaults to ``all_time``
    :type range: ``str``
    :statuscode 200: Successful query, you have data!
    :statuscode 204: Statistics for the user haven't been calculated, empty response will be returned
    :statuscode 400: Bad request, check ``response['error']`` for more details
    :statuscode 404: User not found
    :resheader Content-Type: *application/json*
    """
    user = db_user.get_by_mb_id(user_name)
    if user is None:
        raise APINotFound("Cannot find user: %s" % user_name)

    stats_range = request.args.get('range', default='all_time')
    if not _is_valid_range(stats_range):
        raise APIBadRequest("Invalid range: {}".format(stats_range))

    offset = get_non_negative_param('offset', default=0)
    count = get_non_negative_param('count', default=DEFAULT_ITEMS_PER_GET)

    stats = db_stats.get_user_recordings(user['id'], stats_range)
    if stats is None or getattr(stats, stats_range) is None:
        raise APINoContent('')

    entity_list, total_entity_count = _process_user_entity(stats,
                                                           stats_range,
                                                           offset,
                                                           count,
                                                           entity='recording')
    from_ts = int(getattr(stats, stats_range).from_ts)
    to_ts = int(getattr(stats, stats_range).to_ts)
    last_updated = int(stats.last_updated.timestamp())

    return jsonify({
        'payload': {
            "user_id": user_name,
            'recordings': entity_list,
            "count": len(entity_list),
            "total_recording_count": total_entity_count,
            "offset": offset,
            "range": stats_range,
            "from_ts": from_ts,
            "to_ts": to_ts,
            "last_updated": last_updated,
        }
    })
コード例 #4
0
 def test_get_user_recordings(self):
     data_inserted = self.insert_test_data()
     result = db_stats.get_user_recordings(self.user['id'], 'all_time')
     self.assertDictEqual(result.all_time.dict(), data_inserted['user_recordings'])