async def tmdb_get(endpoint, **kwargs): url = API_HOST + endpoint params = dict(kwargs, api_key=Config.TMDB_API_KEY) logger.debug(f"TMDB url {url} params {params}") async with aiohttp.ClientSession() as session: async with session.get(url, params=params) as resp: return (resp.status, await resp.json())
def req_item_lookup(conn, URI, params): resp = req(conn, URI, params=params) logger.debug(f"req_item_lookup: resp = {resp}") if resp is not None: if isinstance(resp.json(), dict): json = [resp.json()] elif isinstance(resp.json(), list): json = resp.json() else: json = [] return {"code": resp.status_code, "json": json} return
async def search_multi(search_terms, media_types): status, json = await tmdb_get('/search/multi', query=search_terms) if status != 200: logger.error(f"Got error response from TMDB {json}") return [] logger.debug(f"TMDB search found {json['total_results']} results") def process(entry): date = entry.get('release_date') or entry.get('first_air_date') or '' entry['year'] = date.split('-')[0] entry['title'] = entry.get('title') or entry.get('name') return entry return [ process(entry) for entry in json['results'] if entry['media_type'] in media_types ]
def req(conn, resource, method="GET", params={}, body={}): try: if method == "GET": response = requests.get(conn.ENDPOINT + resource, params=params, headers=conn.HEADERS) elif method == "POST": response = requests.post(conn.ENDPOINT + resource, params=params, json=body, headers=conn.HEADERS) elif method == "PUT": response = requests.put(conn.ENDPOINT + resource, params=params, json=body, headers=conn.HEADERS) else: logger.exception(f"Invalid HTTP method: {method}.") raise Exception except Exception as ex: # failed to do something logger.exception( f"Failed to connect to API. Check endpoint ({conn.ENDPOINT}, {resource}) or API_KEY. Exception message: {ex}" ) raise except ConnectionError as err: logger.error( f"Failed to connect to API. Check endpoint ({conn.ENDPOINT}, {resource}) or API_KEY. Error message: {err}" ) raise if response is None or response.json() is None: logger.exception( f"Failed to connect to API. Check endpoint ({conn.ENDPOINT}, {resource}) or API_KEY." ) raise Exception else: if isinstance(response.json(), dict): # this sucks lol. TODO. try: if response.json()["error"]: logger.debug(f"Error message: {response.json()['error']}") raise Exception elif response.json()["message"]: logger.debug( f"Message: {response.json()['message']}. This may be an error; please investigate." ) except KeyError: pass else: logger.debug( f"Successfully connected to {conn.ENDPOINT + resource}!") try: logger.debug(f"Message response code: {response.status_code}") except: pass return response
async def on_message(message): """ Sends the message to each command until it finds one that generates a reply. """ try: # we do not want the bot to look at its own messages if message.author == client.user: return text = message.content.strip() logger.debug(f"Received message: {text} on {message.channel}") reply = None for cmd in commands: reply = await cmd.handle_message(text, message) if reply: await send_reply(cmd, message.channel, reply) break except (ValueError, Exception) as e: logger.exception(e)
async def on_reaction_add(reaction, user): """ When one of our messages is reacted to, this sends the reaction to the command that generated the message (identified by the message nonce). """ try: # ignore reactions we send, reactions not on our messages if user == client.user or not reaction.message.author == client.user: return logger.debug( f"Received reaction: {reaction} from {user} on {reaction.message.channel}" ) for cmd in commands: if cmd.nonce == reaction.message.nonce: reply = await cmd.handle_reaction(reaction, user) if reply: await send_reply(cmd, reaction.message.channel, reply) except (ValueError, Exception) as e: logger.exception(e)