77class Jokes :
88 def __init__ (self ):
99 self .http = urllib3 .PoolManager ()
10+ self .info = self .http .request ('GET' , "https://sv443.net/jokeapi/v2/info" )
1011 print ("Sv443's JokeAPI" )
1112
1213 def build_request (
@@ -24,7 +25,7 @@ def build_request(
2425
2526 if len (category ):
2627 for c in category :
27- if not c .lower () in [ "programming" , "miscellaneous" , "dark" , "pun " ]:
28+ if not c .lower () in self . info [ "categories " ]:
2829 raise ValueError (
2930 f'''Invalid category selected.
3031 You selected { c } .
@@ -36,28 +37,21 @@ def build_request(
3637 Leave blank for any.'''
3738 )
3839
39- cats = "," .join (category ) + "?"
40+ cats = "," .join (category )
4041 else :
41- cats = "Any? "
42+ cats = "Any"
4243
4344 if len (blacklist ) > 0 :
4445 for b in blacklist :
45- if b not in [
46- "nsfw" ,
47- "religious" ,
48- "political" ,
49- "racist" ,
50- "sexist"
51- ]:
46+ if b not in self .info ["flags" ]:
5247 raise ValueError (
53- '''\n \n
48+ f'''
49+
50+
5451 You have blacklisted flags which are not available or you have not put the flags in a list.
5552 Available flags are:
56- "racist"
57- "religious"
58- "political"
59- "sexist"
60- "nsfw"
53+ { """
54+ """ .join (self .info ["flags" ])}
6155 '''
6256 )
6357 return
@@ -86,13 +80,7 @@ def build_request(
8680 else :
8781 search_string = urllib .parse .quote (search_string )
8882 if id_range :
89-
90- response = self .http .request (
91- 'GET' ,
92- "https://sv443.net/jokeapi/v2/info"
93- )
94- dict = json .loads (response .data )
95- range_limit = dict ["jokes" ]["totalCount" ]
83+ range_limit = self .info ["totalCount" ]
9684
9785 if len (id_range ) > 2 :
9886 raise ValueError ("id_range must be no longer than 2 items." )
@@ -143,13 +131,13 @@ def send_request(self,
143131 request ,
144132 headers = {'Authorization' : str (auth_token ),
145133 'user-agent' : str (user_agent ),
146- 'accept-encoding' : 'gzip'
134+ # 'accept-encoding': 'gzip'
147135 }
148136 )
149137 else :
150138 r = self .http .request ('GET' , request , headers = {'user-agent' : str (user_agent )})
151139
152- data = r .data
140+ data = r .data . decode ( 'utf-8' )
153141
154142 if response_format == "json" :
155143 try :
@@ -158,7 +146,6 @@ def send_request(self,
158146 print (data )
159147 raise
160148 else :
161- data = str (data )[2 :- 1 ].replace (r'\n' , '\n ' ).replace ('\\ ' , '' )
162149 if len (' ' .join (re .split ("error" , data .lower ().replace ("\n " , "NEWLINECHAR" ))[0 :][1 :]).replace (
163150 '<' , '' ).replace ('/' , '' ).replace (' ' , '' ).replace (':' , '' ).replace ('>' , '' ).replace ('NEWLINECHAR' , '\n ' )) == 4 :
164151 return [Exception (f"API returned an error. Full response: \n \n { data } " )]
@@ -183,7 +170,7 @@ def get_joke(
183170 type = None ,
184171 search_string = None ,
185172 id_range = None ,
186- amount = None ,
173+ amount = 1 ,
187174 lang = None ,
188175 auth_token = None ,
189176 user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0" ,
0 commit comments