Tenor API

· Tenor API docs · API ·

import cachetools.func, requests, json, re, logging, string

@cachetools.func.ttl_cache(maxsize=50, ttl=5)
def tenor_search(text: str) -> str:
    apikey = 'tenor api key from https://developers.google.com/'
    ckey = 'tenor api server id from https://developers.google.com'
    lmt = 1
    search_query = search_query.translate(str.maketrans('','', string.punctuation))
    logging.info(f'Gif query: {text}')

    response = requests.get(
        f'https://tenor.googleapis.com/v2/search?q={text}&key={apikey}&client_key={ckey}&limit={lmt}&media_filter=mp4&random=true')

    if response.status_code == 200:
        top_gifs = json.loads(response.content)
        result = top_gifs['results'][0]['media_formats']['mp4']['url']
        logging.info(f'Gif url: {result}')
        return result
    else:
        logging.warning('Gif search returned no results')
        return None