Skip to content

Commit efe9e48

Browse files
authored
boink
1 parent e202da5 commit efe9e48

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

JokeAPI Wrapper/main.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import requests
2+
import urllib
3+
4+
5+
class Jokes:
6+
def __init__(self):
7+
print("Sv443's JokeAPI")
8+
9+
def build_request(
10+
self,
11+
category=[],
12+
blacklist=[],
13+
response_format="json",
14+
type=None,
15+
search_string=None,
16+
id_range=None,
17+
):
18+
r = "https://sv443.net/jokeapi/v2/joke/"
19+
20+
if len(category) > 0:
21+
for c in category:
22+
if not c.lower() in ["programming", "miscellaneous", "dark"]:
23+
raise Exception(
24+
'Invalid category selected. Available categories are "programming", "miscellaneous", and "dark". Leave blank for any.'
25+
)
26+
return
27+
cats = ",".join(category)
28+
else:
29+
cats = "Any"
30+
31+
if len(blacklist) > 0:
32+
for b in blacklist:
33+
if not b in ["nsfw", "religious", "political", "racist", "sexist"]:
34+
raise Exception(
35+
'You have blacklisted flags which are not available. Available flags are:\n"racist"\n"religious"\n"political"\n"sexist"\n"nsfw"'
36+
)
37+
return
38+
blacklistFlags = ",".join(blacklist)
39+
else:
40+
blacklistFlags = None
41+
42+
if response_format not in ["json", "xml", "yaml"]:
43+
raise Exception("Response format must be either json, xml or yaml.")
44+
if type:
45+
if type not in ["single", "twopart"]:
46+
raise Exception(
47+
'Invalid joke type. Available options are "single" or "twopart".'
48+
)
49+
return
50+
else:
51+
type = "Any"
52+
53+
if search_string:
54+
if not isinstance(search_string, str):
55+
raise Exception("search_string must be a string.")
56+
return
57+
else:
58+
search_string = urllib.parse.quote(search_string)
59+
if id_range:
60+
range_limit = requests.get("https://sv443.net/jokeapi/v2/info").json()[
61+
"jokes"
62+
]["totalCount"]
63+
if len(id_range) > 2:
64+
raise Exception("id_range must be no longer than 2 items.")
65+
elif id_range[0] < 0:
66+
raise Exception("id_range[0] must be greater than or equal to 0.")
67+
elif id_range[1] > range_limit:
68+
raise Exception(
69+
f"id_range[1] must be less than or equal to {range_limit-1}."
70+
)
71+
72+
r += cats
73+
74+
prev_flags = None
75+
76+
if blacklistFlags:
77+
r += f"?blacklistFlags={blacklistFlags}"
78+
prev_flags = True
79+
if prev_flags:
80+
r += f"&format={response_format}"
81+
else:
82+
r += f"?format={response_format}"
83+
prev_flags = True
84+
if prev_flags:
85+
r += f"&type={type}"
86+
else:
87+
r += f"?type={type}"
88+
prev_flags = True
89+
if search_string:
90+
if prev_flags:
91+
r += f"&contains={search_string}"
92+
prev_flags = True
93+
else:
94+
r += f"?contains={search_string}"
95+
if id_range:
96+
if prev_flags:
97+
r += f"&idRange={id_range[0]}-{id_range[1]}"
98+
else:
99+
r += f"?idRange={id_range[0]}-{id_range[1]}"
100+
101+
return r
102+
103+
def send_request(self, request, response_format):
104+
r = requests.get(request)
105+
if response_format == "json":
106+
return r.json()
107+
elif response_format == "xml":
108+
return r.content
109+
else:
110+
return r.content
111+
112+
def get_joke(
113+
self,
114+
category=[],
115+
blacklist=[],
116+
response_format="json",
117+
type=None,
118+
search_string=None,
119+
id_range=None,
120+
):
121+
r = self.build_request(
122+
category, blacklist, response_format, type, search_string, id_range
123+
)
124+
response = self.send_request(r, response_format)
125+
return response

0 commit comments

Comments
 (0)