Skip to content

Commit

Permalink
made bot runnable without api keys and some logging
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanSmudja committed Dec 21, 2023
1 parent 560f721 commit 2a58c08
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions urbandictionary/urban_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
import aiohttp
from dotenv import load_dotenv

load_dotenv()
API_KEY = os.getenv('URBAN_DICTIONARY_TOKEN')
API_KEY = None

base_url = "https://mashape-community-urban-dictionary.p.rapidapi.com"
# Try to load the API key from the .env file
try:
load_dotenv()
API_KEY = os.getenv('URBAN_DICTIONARY_TOKEN')
except Exception as init_error:
print(f"Could not initialise Urban Dictionary client: {init_error}")

# Network request related variables
base_url = "https://mashape-community-urban-dictionary.p.rapidapi.com"
headers = {
'X-RapidAPI-Key': API_KEY,
'X-RapidAPI-Host': "mashape-community-urban-dictionary.p.rapidapi.com"
Expand All @@ -15,7 +21,11 @@

async def request_term(term):
url = base_url + "/define?term=" + term
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers) as response:
data = await response.json()
return data
try:
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers) as response:
data = await response.json()
return data
except Exception as api_error:
print(f"An error occurred: {api_error}")
return None

0 comments on commit 2a58c08

Please sign in to comment.