예제 #1
0
    def scrobble_tracks(cls,
                        tracks: List[ScrobbleTrack],
                        batch_size=10) -> ListModel[ScrobbleTrack]:
        """
        Split tracks into the desired batch size, with maximum size set to 50
        and send the tracks for processing, I am debating if this even belongs
        here.

        :param tracks: The tracks to scrobble
        :param batch_size: The number of tracks to submit per cycle
        :rtype: :class:`pydrag.models.common.ListModel` of :class:`~pydrag.models.common.ScrobbleTrack`
        """
        def divide_chunks(l, n):
            for i in range(0, len(l), n):
                yield l[i:i + n]

        data: List[ScrobbleTrack] = []
        params = []
        for batch in list(divide_chunks(tracks, min(batch_size, 50))):
            res = Track._scrobble(batch)
            data += res.data
            params.append(res.params)

        result = ListModel(data)
        result.params = params
        return result
예제 #2
0
    def bind_data(
        cls,
        bind: Type[BaseModel],
        body: Optional[Dict],
        flatten: Optional[str] = None,
    ):
        """
        Construct a BaseModel from the response body and the flatten directive.

        :param bind: Class type to construct from the api response.
        :type bind: :class:`~pydrag.models.common.BaseModel`
        :param Dict body: The api response
        :param str flatten: A dot separated string used to flatten nested list of values
        :rtype: :class:`~pydrag.models.common.BaseModel`
        """
        if not body:
            return bind()

        data = body[next(iter(body.keys()))]
        if data and not isinstance(data, Dict):
            data = body

        if flatten is None:
            return bind.from_dict(data)

        keys = flatten.split(".")
        items = get_nested(data, keys, ensure_list=True)
        data.pop(next(iter(keys)))
        data.update({"data": [bind.from_dict(i) for i in items]})
        return ListModel.from_dict(data)
예제 #3
0
 def test_sync_with_chart(self, top_tracks_chart, *args):
     top_tracks_chart.return_value = ListModel(["a", "b", "c"])
     actual = LastService.get_tracks(
         type=PlaylistType.CHART.value, limit=10
     )
     self.assertEqual(["a", "b", "c"], actual)
     top_tracks_chart.assert_called_once_with(limit=10)
예제 #4
0
 def test_sync_with_artist_chart(self, get_artist, get_top_tracks, *args):
     get_artist.return_value = Artist(name="queen")
     get_top_tracks.return_value = ListModel(["a", "b", "c"])
     actual = LastService.get_tracks(
         type=PlaylistType.ARTIST.value, limit=10, artist="queeen"
     )
     self.assertEqual(["a", "b", "c"], actual)
     get_artist.assert_called_once_with("queeen")
     get_top_tracks.assert_called_once_with(limit=10)
예제 #5
0
 def test_sync_with_tag_chart(self, get_tag, get_top_tracks, *args):
     get_tag.return_value = Tag(name="rock")
     get_top_tracks.return_value = ListModel(["a", "b", "c"])
     actual = LastService.get_tracks(
         type=PlaylistType.TAG.value, limit=10, tag="rock"
     )
     self.assertEqual(["a", "b", "c"], actual)
     get_tag.assert_called_once_with("rock")
     get_top_tracks.assert_called_once_with(limit=10)
예제 #6
0
 def test_sync_with_country_chart(self, top_tracks_by_country, *args):
     top_tracks_by_country.return_value = ListModel(["a", "b", "c"])
     actual = LastService.get_tracks(
         type=PlaylistType.COUNTRY.value, limit=10, country="greece"
     )
     self.assertEqual(["a", "b", "c"], actual)
     top_tracks_by_country.assert_called_once_with(
         limit=10, country="greece"
     )
예제 #7
0
    def test_sync_with_user_loved_tracks(self, get_user, loved_tracks, *args):
        get_user.return_value = self.get_user()
        loved_tracks.return_value = ListModel(["a", "b", "c"])

        actual = LastService.get_tracks(
            type=PlaylistType.USER_LOVED_TRACKS.value, limit=10, username="******"
        )
        self.assertEqual(["a", "b", "c"], actual)
        get_user.assert_called_once_with("foo")
        loved_tracks.assert_called_once_with(limit=10)
예제 #8
0
    def test_sync_with_user_top_tracks(self, get_user, top_tracks, *args):
        get_user.return_value = self.get_user()
        top_tracks.return_value = ListModel(["a", "b", "c"])

        actual = LastService.get_tracks(
            type=PlaylistType.USER_TOP_TRACKS.value, limit=10, username="******"
        )
        self.assertEqual(["a", "b", "c"], actual)
        get_user.assert_called_once_with("foo")
        top_tracks.assert_called_once_with(
            period=constants.Period.overall, limit=10
        )