Module sources.ApiLoader.ApiLoader

Expand source code
#!/usr/bin/env python3

from urllib.parse import urlencode, quote_plus
from typing import Union
import requests
import json


class ApiError(Exception):
    """
    Exception raised when error occurs in ApiLoader.

    Attributes
    ----------
    message : str
        Exception explanation
    """

    def __init__(self, message="Error while searching Gifs from API!"):
        """
        Constructs an actual Api Error Exception class.

        Parameters
        ----------
        message : str
            Message explaning the ApiError
        """
        self.message = message

    def __str__(self):
        """
        Returns the actual error message.

        Returns
        -------
        Actual Api Error message.
        """
        return f'ApiError: {self.message}'


class ApiLoader:
    """
    Class making every Tenor Api handling.

    Class is basically developed to be a "generic Tenor's Api wrapper"

    Attributes
    -------
    _url : str
        Api base endpoint url
    _tranUrl : str
        Transformed url with parameters
    _limit : int
        Number of gif mdCreator is going to get from Tenor's Api
    _search : str
        Actual string build by every gif search keywords
    _params : int
        Parameters to encode for the Api Request
    _build : int
        Boolean verifying if searching url is built
    """

    def __init__(self, url: str, search: str, limit: int = 5) -> None:
        """
        Constructs a new ApiLoader object.

        Parameters
        -------
        _url : str
            Api base endpoint url
        _search : str
            Actual string build by every gif search keywords
        _limit : int
            Number of gif mdCreator is going to get from Tenor's Api
        """

        self._apikey = None
        self._baseUrl = url
        self._tranUrl = ""
        self._limit = limit
        self._search = search
        self._params = dict()
        self._build = False

    # Gifs Tenor API
    def buildUrl(self, apiKey: str) -> None:
        """
        Create the url with baseUrl and encoded parameters.

        Returns
        -------
        None
        """
        if self._search in [None, '']:
            return

        urlLink = self._baseUrl
        self._params = {
            "q": str(self._search),
            "key": apiKey,
            "limit": str(self._limit),
            "media_filter": "minimal"
        }

        urlLink += urlencode(self._params, quote_via=quote_plus)
        self._tranUrl = urlLink
        self._build = True

    def setLimit(self, limit: int) -> None:
        self._limit = limit

    def isUrlBuild(self) -> bool:
        """
        Checks if Api Search Url is built.

        Returns
        -------
        Boolean describing the actual search url state

        True if search url is built

        False otherwise
        """
        return self._build

    def searchGifs(self) -> Union[None, list]:
        """
        Search for gifs depending on the actual searching arguments.

        It executes the search request and checks for the request return code.

        Returns
        -------
        Either None or a list of gifs urls
        """

        gifsUrls = []

        if self._build:
            r = requests.get(self._tranUrl)
            if r.status_code == 200:
                values = json.loads(r.content)
                for gif in values["results"]:
                    for media in gif["media"]:
                        gifsUrls.append(media["gif"]["url"])
            else:
                print(ApiError())
                return None
            return gifsUrls
        print(ApiError("ApiLoader Url isn't build!"))
        return None

Classes

class ApiError (message='Error while searching Gifs from API!')

Exception raised when error occurs in ApiLoader.

Attributes

message : str
Exception explanation

Constructs an actual Api Error Exception class.

Parameters

message : str
Message explaning the ApiError
Expand source code
class ApiError(Exception):
    """
    Exception raised when error occurs in ApiLoader.

    Attributes
    ----------
    message : str
        Exception explanation
    """

    def __init__(self, message="Error while searching Gifs from API!"):
        """
        Constructs an actual Api Error Exception class.

        Parameters
        ----------
        message : str
            Message explaning the ApiError
        """
        self.message = message

    def __str__(self):
        """
        Returns the actual error message.

        Returns
        -------
        Actual Api Error message.
        """
        return f'ApiError: {self.message}'

Ancestors

  • builtins.Exception
  • builtins.BaseException
class ApiLoader (url: str, search: str, limit: int = 5)

Class making every Tenor Api handling.

Class is basically developed to be a "generic Tenor's Api wrapper"

Attributes

_url : str
Api base endpoint url
_tranUrl : str
Transformed url with parameters
_limit : int
Number of gif mdCreator is going to get from Tenor's Api
_search : str
Actual string build by every gif search keywords
_params : int
Parameters to encode for the Api Request
_build : int
Boolean verifying if searching url is built

Constructs a new ApiLoader object.

Parameters

_url : str
Api base endpoint url
_search : str
Actual string build by every gif search keywords
_limit : int
Number of gif mdCreator is going to get from Tenor's Api
Expand source code
class ApiLoader:
    """
    Class making every Tenor Api handling.

    Class is basically developed to be a "generic Tenor's Api wrapper"

    Attributes
    -------
    _url : str
        Api base endpoint url
    _tranUrl : str
        Transformed url with parameters
    _limit : int
        Number of gif mdCreator is going to get from Tenor's Api
    _search : str
        Actual string build by every gif search keywords
    _params : int
        Parameters to encode for the Api Request
    _build : int
        Boolean verifying if searching url is built
    """

    def __init__(self, url: str, search: str, limit: int = 5) -> None:
        """
        Constructs a new ApiLoader object.

        Parameters
        -------
        _url : str
            Api base endpoint url
        _search : str
            Actual string build by every gif search keywords
        _limit : int
            Number of gif mdCreator is going to get from Tenor's Api
        """

        self._apikey = None
        self._baseUrl = url
        self._tranUrl = ""
        self._limit = limit
        self._search = search
        self._params = dict()
        self._build = False

    # Gifs Tenor API
    def buildUrl(self, apiKey: str) -> None:
        """
        Create the url with baseUrl and encoded parameters.

        Returns
        -------
        None
        """
        if self._search in [None, '']:
            return

        urlLink = self._baseUrl
        self._params = {
            "q": str(self._search),
            "key": apiKey,
            "limit": str(self._limit),
            "media_filter": "minimal"
        }

        urlLink += urlencode(self._params, quote_via=quote_plus)
        self._tranUrl = urlLink
        self._build = True

    def setLimit(self, limit: int) -> None:
        self._limit = limit

    def isUrlBuild(self) -> bool:
        """
        Checks if Api Search Url is built.

        Returns
        -------
        Boolean describing the actual search url state

        True if search url is built

        False otherwise
        """
        return self._build

    def searchGifs(self) -> Union[None, list]:
        """
        Search for gifs depending on the actual searching arguments.

        It executes the search request and checks for the request return code.

        Returns
        -------
        Either None or a list of gifs urls
        """

        gifsUrls = []

        if self._build:
            r = requests.get(self._tranUrl)
            if r.status_code == 200:
                values = json.loads(r.content)
                for gif in values["results"]:
                    for media in gif["media"]:
                        gifsUrls.append(media["gif"]["url"])
            else:
                print(ApiError())
                return None
            return gifsUrls
        print(ApiError("ApiLoader Url isn't build!"))
        return None

Methods

def buildUrl(self, apiKey: str) ‑> None

Create the url with baseUrl and encoded parameters.

Returns

None
 
Expand source code
def buildUrl(self, apiKey: str) -> None:
    """
    Create the url with baseUrl and encoded parameters.

    Returns
    -------
    None
    """
    if self._search in [None, '']:
        return

    urlLink = self._baseUrl
    self._params = {
        "q": str(self._search),
        "key": apiKey,
        "limit": str(self._limit),
        "media_filter": "minimal"
    }

    urlLink += urlencode(self._params, quote_via=quote_plus)
    self._tranUrl = urlLink
    self._build = True
def isUrlBuild(self) ‑> bool

Checks if Api Search Url is built.

Returns

Boolean describing the actual search url state
 
True if search url is built
 
False otherwise
 
Expand source code
def isUrlBuild(self) -> bool:
    """
    Checks if Api Search Url is built.

    Returns
    -------
    Boolean describing the actual search url state

    True if search url is built

    False otherwise
    """
    return self._build
def searchGifs(self) ‑> Optional[None]

Search for gifs depending on the actual searching arguments.

It executes the search request and checks for the request return code.

Returns

Either None or a list of gifs urls
 
Expand source code
def searchGifs(self) -> Union[None, list]:
    """
    Search for gifs depending on the actual searching arguments.

    It executes the search request and checks for the request return code.

    Returns
    -------
    Either None or a list of gifs urls
    """

    gifsUrls = []

    if self._build:
        r = requests.get(self._tranUrl)
        if r.status_code == 200:
            values = json.loads(r.content)
            for gif in values["results"]:
                for media in gif["media"]:
                    gifsUrls.append(media["gif"]["url"])
        else:
            print(ApiError())
            return None
        return gifsUrls
    print(ApiError("ApiLoader Url isn't build!"))
    return None
def setLimit(self, limit: int) ‑> None
Expand source code
def setLimit(self, limit: int) -> None:
    self._limit = limit