Example #1
0
    def fetch_illustration_comments(self,
                                    illustration_id,
                                    offset=None,
                                    include_total_comments=False):
        """
        Fetch the comments of an illustration. A maximum of 30 comments
        are returned in one response.

        **Note:** The ``total_comments`` key does not equal the number
        of comments that will be returned by the API. If requesting all
        the comments, use the ``next`` key to determine whether or not
        to continue, not the ``total_comments`` key.

        :param int illustration_id: ID of the illustration.
        :param int offset: Number of comments to offset by.
        :param bool include_total_comments: Whether or not to include a
            the total number of comments on the illustration. If set to
            False, the ``total_comments`` key in the response will be ``0``.

        :return: A dictionary containing the comments, the offset for the
            next page of comments, and the total number of comments.

        .. code-block:: python

           {
               'comments': [Comment, ...],  # List of comments.
               'next': 30,  # Offset to get the next page of comments.
               'total_comments': 142,
           }

        :rtype: dict

        :raises requests.RequestException: If the request fails.
        :raises BadApiResponse: If the response is not valid JSON.
        """
        response = self._request_json(
            method='get',
            url=f'{BASE_URL}/v1/illust/comments',
            params={
                'illust_id': illustration_id,
                'offset': offset,
                'include_total_comments': format_bool(include_total_comments),
            },
        )

        return {
            'comments': [
                Comment(**comment, client=self)
                for comment in response['comments']
            ],
            'next':
            parse_qs(response['next_url'], param='offset'),
            'total_comments':
            response['total_comments'],
        }
Example #2
0
    def fetch_illustrations_recommended(
        self,
        content_type=ContentType.ILLUSTRATION,
        include_ranking_illustrations=False,
        max_bookmark_id_for_recommend=None,
        min_bookmark_id_for_recent_illustrations=None,
        offset=None,
        bookmark_illust_ids=None,
        include_ranking_label=True,
    ):
        """
        Fetch one's recommended illustrations.

        :param ContentType content_type: The type of content to fetch. Accepts
            ``ILLUSTRATION`` and ``MANGA``.
        :param bool include_ranking_illustrations: If ``True``, the top 10 ranked
            illustrations daily are included in the response. If False, the
            ``ranking_illustrations`` key in the response dict will be empty.
        :param int max_bookmark_id_for_recommend: The maximum bookmark ID for
            recommended illustrations, used for changing the returned illustrations.
        :param int min_bookmark_id_for_recent_illustrations: The minimum bookmark ID for
            recent illustrations, used for changing the returned illustrations.
        :param int offset: The number of illustrations to offset by.
        :param list bookmark_illust_ids: A list of illustration IDs.
        :param bool include_ranking_label: Whether or not to include the ranking label.

        :return: A dictionary containing the recommended illustrations and the
            parameters for the next page of illustrations.

        .. code-block:: python

           {
               'contest_exists': False,  # Does a contest exist?
               'illustrations': [Illustration, ...],  # List of illustrations.
               'next': {  # Parameters to get the next set of illustrations.
                   'min_bookmark_id_for_recent_illustrations': 6277740037,
                   'max_bookmark_id_for_recommend': 6268205545,
                   'offset': 0,
               },
               'ranking_illustrations': [Illustration, ...] # Ranking illust.
           }

        :rtype: dict

        :raises requests.RequestException: If the request fails.
        :raises BadApiResponse: If the response is not valid JSON.
        """
        if bookmark_illust_ids:
            bookmark_illust_ids = ",".join(
                str(bid) for bid in bookmark_illust_ids)

        response = self._request_json(
            method="get",
            url=f"{BASE_URL}/v1/illust/recommended",
            params={
                "content_type":
                content_type.value,
                "include_ranking_label":
                format_bool(include_ranking_label),
                "max_bookmark_id_for_recommend":
                max_bookmark_id_for_recommend,
                "min_bookmark_id_for_recent_illust":
                (min_bookmark_id_for_recent_illustrations, ),
                "offset":
                offset,
                "include_ranking_illusts":
                format_bool(include_ranking_illustrations),
                "bookmark_illust_ids":
                bookmark_illust_ids,
            },
        )
        return {
            "contest_exists":
            response["contest_exists"],
            "illustrations": [
                Illustration(**illust, client=self)
                for illust in response["illusts"]
            ],
            "next": {
                "min_bookmark_id_for_recent_illustrations": (parse_qs(
                    response["next_url"],
                    param="min_bookmark_id_for_recent_illust",
                )),
                "max_bookmark_id_for_recommend": (parse_qs(
                    response["next_url"],
                    param="max_bookmark_id_for_recommend",
                )),
                "offset":
                parse_qs(response["next_url"], param="offset"),
            },
            "ranking_illustrations": [
                Illustration(**illust, client=self)
                for illust in response["ranking_illusts"]
            ],
        }
Example #3
0
def test_format_bool(arg, result):
    assert format_bool(arg) is result