Skip to content

Commit

Permalink
Merge pull request suryanshsk#351 from IshuSinghSE/add-random-joke-ge…
Browse files Browse the repository at this point in the history
…nerator

added random joke generator
  • Loading branch information
suryanshsk authored Oct 20, 2024
2 parents d3b9f6c + b1cedaf commit 8195904
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions random_joke_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import requests
from textblob import TextBlob

def get_random_joke():
joke_url = "https://icanhazdadjoke.com/"
emoji_map = {
"positive": "😊",
"neutral": "😐",
"negative": "😢"
}

headers = {
"Accept": "application/json",
"User-Agent": "Mozilla/5.0"
}

def get_sentiment(joke):
analysis = TextBlob(joke)
if analysis.sentiment.polarity > 0:
return "positive"
elif analysis.sentiment.polarity == 0:
return "neutral"
else:
return "negative"

try:
joke_response = requests.get(joke_url, headers=headers)
joke_response.raise_for_status()
joke_data = joke_response.json()
joke = joke_data["joke"]

sentiment = get_sentiment(joke)
emoji = emoji_map.get(sentiment, "")

return f"{joke} {emoji}"
except requests.exceptions.RequestException as e:
return f"Error fetching joke: {e}"

if __name__ == "__main__":
print(get_random_joke())

0 comments on commit 8195904

Please sign in to comment.