Module hdrezka.api.ajax

Sending AJAX requests.

Classes

class AJAX (client: HDRezkaClient)
Expand source code
class AJAX:
    """AJAX endpoints bound to a single ``HDRezkaClient`` session."""

    __slots__ = ('_client',)

    def __init__(self, client: 'HDRezkaClient'):
        """
        :param client: Session used for all AJAX requests.
        """
        self._client = client

    @staticmethod
    def _check(resp: httpx.Response):
        answer = resp.json()
        if not answer.get('success', True):
            raise AJAXFail(answer.get('message', answer))
        return answer

    async def _send_data(self, action: str, **kwargs):
        return self._check(
            await self._client.get_response('POST', self._client.host_join(f'ajax/{action}/'), **kwargs)
        )

    async def get_cdn_series(self, data: dict[str, SupportsInt | str]):
        """Request series payload for one translation."""
        return await self._send_data('get_cdn_series', data=data)

    async def get_episodes(self, id_: SupportsInt | str, translator_id: SupportsInt | str):
        """Request available episodes."""
        return await self.get_cdn_series(
            {'action': 'get_episodes',
             'id': id_,
             'translator_id': translator_id}
        )

    async def get_stream(self, id_: SupportsInt | str, translator_id: SupportsInt | str,
                         season: SupportsInt | str, episode: SupportsInt | str) -> APIResponse:
        """Request stream URLs for an episode."""
        return await self.get_cdn_series(
            {'action': 'get_stream',
             'id': id_,
             'translator_id': translator_id,
             'season': season,
             'episode': episode}
        )

    async def get_movie(self, id_: SupportsInt | str, translator_id: SupportsInt | str):
        """Request stream URLs for a movie."""
        return await self.get_cdn_series(
            {'action': 'get_movie',
             'id': id_, 'translator_id': translator_id}
        )

    async def get_trailer_video(self, id_: SupportsInt | str) -> TrailerResponse:
        """Request trailer video iframe HTML."""
        return self._check(await self._client.get_response(
            'POST',
            self._client.host_join('engine/ajax/gettrailervideo.php'),
            data={'id': id_},
        ))

    async def _favorites(self, **data) -> dict:
        """POST form data to ``/ajax/favorites/``."""
        return self._check(await self._client.get_response(
            'POST',
            self._client.host_join('ajax/favorites/'),
            data=data,
        ))

    async def add_favorites_cat(self, name: str) -> FavoritesCatResponse:
        """
        Create a favorites collection in the user profile.

        Equivalent to ``action=add_cat``.
        """
        return await self._favorites(name=name, action='add_cat')

    async def rename_favorites_cat(
            self,
            cat_id: SupportsInt | str,
            name: str,
    ) -> FavoritesOkResponse:
        """
        Rename a favorites collection.

        Equivalent to ``action=change_cat_name``.
        """
        return await self._favorites(name=name, cat_id=cat_id, action='change_cat_name')

    async def add_favorites_post(
            self,
            post_id: SupportsInt | str,
            cat_id: SupportsInt | str,
    ) -> FavoritesOkResponse:
        """
        Add a title (post) to a favorites collection.

        Equivalent to ``action=add_post``.
        """
        return await self._favorites(post_id=post_id, cat_id=cat_id, action='add_post')

    async def remove_favorites_cat(self, cat_id: SupportsInt | str) -> FavoritesOkResponse:
        """
        Remove a favorites collection from the profile.

        Equivalent to ``action=remove_cat``.
        """
        return await self._favorites(cat_id=cat_id, action='remove_cat')

AJAX endpoints bound to a single HDRezkaClient session.

:param client: Session used for all AJAX requests.

Methods

async def add_favorites_cat(self, name: str) ‑> FavoritesCatResponse
Expand source code
async def add_favorites_cat(self, name: str) -> FavoritesCatResponse:
    """
    Create a favorites collection in the user profile.

    Equivalent to ``action=add_cat``.
    """
    return await self._favorites(name=name, action='add_cat')

Create a favorites collection in the user profile.

Equivalent to action=add_cat.

async def add_favorites_post(self, post_id: typing.SupportsInt | str, cat_id: typing.SupportsInt | str) ‑> FavoritesOkResponse
Expand source code
async def add_favorites_post(
        self,
        post_id: SupportsInt | str,
        cat_id: SupportsInt | str,
) -> FavoritesOkResponse:
    """
    Add a title (post) to a favorites collection.

    Equivalent to ``action=add_post``.
    """
    return await self._favorites(post_id=post_id, cat_id=cat_id, action='add_post')

Add a title (post) to a favorites collection.

Equivalent to action=add_post.

async def get_cdn_series(self, data: dict[str, typing.SupportsInt | str])
Expand source code
async def get_cdn_series(self, data: dict[str, SupportsInt | str]):
    """Request series payload for one translation."""
    return await self._send_data('get_cdn_series', data=data)

Request series payload for one translation.

async def get_episodes(self, id_: typing.SupportsInt | str, translator_id: typing.SupportsInt | str)
Expand source code
async def get_episodes(self, id_: SupportsInt | str, translator_id: SupportsInt | str):
    """Request available episodes."""
    return await self.get_cdn_series(
        {'action': 'get_episodes',
         'id': id_,
         'translator_id': translator_id}
    )

Request available episodes.

async def get_movie(self, id_: typing.SupportsInt | str, translator_id: typing.SupportsInt | str)
Expand source code
async def get_movie(self, id_: SupportsInt | str, translator_id: SupportsInt | str):
    """Request stream URLs for a movie."""
    return await self.get_cdn_series(
        {'action': 'get_movie',
         'id': id_, 'translator_id': translator_id}
    )

Request stream URLs for a movie.

async def get_stream(self,
id_: typing.SupportsInt | str,
translator_id: typing.SupportsInt | str,
season: typing.SupportsInt | str,
episode: typing.SupportsInt | str) ‑> APIResponse
Expand source code
async def get_stream(self, id_: SupportsInt | str, translator_id: SupportsInt | str,
                     season: SupportsInt | str, episode: SupportsInt | str) -> APIResponse:
    """Request stream URLs for an episode."""
    return await self.get_cdn_series(
        {'action': 'get_stream',
         'id': id_,
         'translator_id': translator_id,
         'season': season,
         'episode': episode}
    )

Request stream URLs for an episode.

async def get_trailer_video(self, id_: typing.SupportsInt | str) ‑> TrailerResponse
Expand source code
async def get_trailer_video(self, id_: SupportsInt | str) -> TrailerResponse:
    """Request trailer video iframe HTML."""
    return self._check(await self._client.get_response(
        'POST',
        self._client.host_join('engine/ajax/gettrailervideo.php'),
        data={'id': id_},
    ))

Request trailer video iframe HTML.

async def remove_favorites_cat(self, cat_id: typing.SupportsInt | str) ‑> FavoritesOkResponse
Expand source code
async def remove_favorites_cat(self, cat_id: SupportsInt | str) -> FavoritesOkResponse:
    """
    Remove a favorites collection from the profile.

    Equivalent to ``action=remove_cat``.
    """
    return await self._favorites(cat_id=cat_id, action='remove_cat')

Remove a favorites collection from the profile.

Equivalent to action=remove_cat.

async def rename_favorites_cat(self, cat_id: typing.SupportsInt | str, name: str) ‑> FavoritesOkResponse
Expand source code
async def rename_favorites_cat(
        self,
        cat_id: SupportsInt | str,
        name: str,
) -> FavoritesOkResponse:
    """
    Rename a favorites collection.

    Equivalent to ``action=change_cat_name``.
    """
    return await self._favorites(name=name, cat_id=cat_id, action='change_cat_name')

Rename a favorites collection.

Equivalent to action=change_cat_name.