Skip to content

Commit

Permalink
add dj
Browse files Browse the repository at this point in the history
  • Loading branch information
davemolk committed Oct 22, 2022
1 parent 82d0bd6 commit 83560a9
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ notes.md
foo.py
test.txt
test.csv
test.json
foo/
.DS_Store
# Binaries for programs and plugins
*.exe
*.exe~
Expand Down
86 changes: 86 additions & 0 deletions python/dj/d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import argparse
import json
import random
import time

import requests

def get_joke(url: str, headers: dict) -> str:
headers["User-Agent"] = get_user_agent()
try:
r = requests.get(url, headers=headers)
except requests.exceptions.RequestException as e:
raise SystemExit(e)
return r.text

def get_user_agent() -> str:
agents = [
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4692.56 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4889.0 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/603.3.8 (KHTML, like Gecko)",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.7 (KHTML, like Gecko) Version/9.1.2 Safari/601.7.7",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.0",
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36",
]
rando = random.randint(0, len(agents) - 1)
return agents[rando]

def get_user_input():
parser = argparse.ArgumentParser(description="joke me")
parser.add_argument("-j", "--joke", type=str, help="get random joke")
parser.add_argument("-t", "--term", type=str, default="", help="enter a search term or theme")
args = parser.parse_args()
return args.joke, args.term

def prep_format(joke: str):
if "?" in joke:
format(joke, "?")
elif "." in joke[:-1]:
format(joke, ".")

def format(text, splitter):
split_text = text.split(splitter)
print(split_text[0] + splitter + "\n")
time.sleep(2)
print(split_text[1].strip())

class BaseException(Exception):
pass

class JSONException(BaseException):
pass

class NoJokesFoundException(BaseException):
pass

if __name__ == "__main__":
random_url = "https://icanhazdadjoke.com/"
theme_url = "https://icanhazdadjoke.com/search?term="
joke, term = get_user_input()
if term != "":
headers = {
"Accept": "application/json",
}
url = f'{theme_url}{term}&limit=30'
results = json.loads(get_joke(url, headers))
if results == "":
print("no jokes for that term")
else:
try:
rando = random.randint(0, len(results["results"]) - 1)
joke = results["results"][rando]["joke"]
except JSONException:
print("")
else:
prep_format(joke)

else:
headers = {
"Accept": "text/plain",
}
joke = get_joke(random_url, headers)
prep_format(joke)

0 comments on commit 83560a9

Please sign in to comment.