Skip to content

Commit b7fec8a

Browse files
committed
.
2 parents 2512258 + 083e4ca commit b7fec8a

File tree

6 files changed

+92
-58
lines changed

6 files changed

+92
-58
lines changed

.github/workflows/python-publish.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This workflows will upload a Python Package using Twine when a release is created
2+
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries
3+
4+
name: Upload Python Package
5+
6+
on:
7+
release:
8+
types: [created]
9+
10+
jobs:
11+
deploy:
12+
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- uses: actions/checkout@v2
17+
- name: Set up Python
18+
uses: actions/setup-python@v2
19+
with:
20+
python-version: '3.x'
21+
- name: Install dependencies
22+
run: |
23+
python -m pip install --upgrade pip
24+
pip install setuptools wheel twine
25+
- name: Build and publish
26+
env:
27+
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
28+
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
29+
run: |
30+
python setup.py sdist bdist_wheel
31+
twine upload dist/*

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
* Build on top of existing code if possible.
44
* Use PEP8 formatting
55
* Be descriptive in your Pull Request. Make sure we know *what* you've done and *why*.
6-
* Please consider using [these](https://github.com/atom/atom/blob/master/CONTRIBUTING.md#git-commit-messages) guidelines when commiting, along with :heavy_plus_sign: `:heavy_plus_sign:` when adding new functionality
6+
* Please use [these](https://github.com/atom/atom/blob/master/CONTRIBUTING.md#git-commit-messages) guidelines when commiting, along with :heavy_plus_sign: `:heavy_plus_sign:` when adding new functionality
275 Bytes
Binary file not shown.
4.07 KB
Binary file not shown.

jokeapi/main.py

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
class Jokes:
1414
def __init__(self):
1515
self.http = urllib3.PoolManager()
16+
self.info = self.http.request('GET', "https://sv443.net/jokeapi/v2/info")
17+
self.info = data = json.loads(self.info.data.decode('utf-8'))["jokes"]
18+
print("Sv443's JokeAPI")
1619

1720
def build_request(
1821
self,
@@ -22,42 +25,38 @@ def build_request(
2225
type=None,
2326
search_string=None,
2427
id_range=None,
28+
amount=1,
29+
lang="en"
2530
):
2631
r = "https://sv443.net/jokeapi/v2/joke/"
2732

28-
if len(category) > 0:
33+
if len(category):
2934
for c in category:
30-
if not c.lower() in ["programming", "miscellaneous", "dark"]:
35+
if not c.lower() in self.info["categories"]:
3136
raise ValueError(
32-
'''Invalid category selected. Available categories are:
33-
"programming"
34-
"miscellaneous"
35-
"dark".
37+
f'''Invalid category selected.
38+
You selected {c}.
39+
Available categories are:
40+
{"""
41+
""".join(self.info["categories"])}
3642
Leave blank for any.'''
3743
)
38-
return
44+
3945
cats = ",".join(category)
4046
else:
4147
cats = "Any"
4248

4349
if len(blacklist) > 0:
4450
for b in blacklist:
45-
if b not in [
46-
"nsfw",
47-
"religious",
48-
"political",
49-
"racist",
50-
"sexist"
51-
]:
51+
if b not in self.info["flags"]:
5252
raise ValueError(
53-
'''\n\n
54-
You have blacklisted flags which are not available.
53+
f'''
54+
55+
56+
You have blacklisted flags which are not available or you have not put the flags in a list.
5557
Available flags are:
56-
"racist"
57-
"religious"
58-
"political"
59-
"sexist"
60-
"nsfw"
58+
{"""
59+
""".join(self.info["flags"])}
6160
'''
6261
)
6362
return
@@ -86,13 +85,7 @@ def build_request(
8685
else:
8786
search_string = urllib.parse.quote(search_string)
8887
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"]
88+
range_limit = self.info["totalCount"]
9689

9790
if len(id_range) > 2:
9891
raise ValueError("id_range must be no longer than 2 items.")
@@ -107,52 +100,60 @@ def build_request(
107100

108101
r += cats
109102

110-
prev_flags = None
103+
r += f"?format={response_format}"
111104

112105
if blacklistFlags:
113-
r += f"?blacklistFlags={blacklistFlags}"
114-
prev_flags = True
115-
if prev_flags:
116-
r += f"&format={response_format}"
117-
else:
118-
r += f"?format={response_format}"
119-
prev_flags = True
120-
if prev_flags:
121-
r += f"&type={type}"
122-
else:
123-
r += f"?type={type}"
124-
prev_flags = True
106+
r += f"&blacklistFlags={blacklistFlags}"
107+
108+
109+
r += f"&type={type}"
110+
125111
if search_string:
126-
if prev_flags:
127-
r += f"&contains={search_string}"
128-
prev_flags = True
129-
else:
130-
r += f"?contains={search_string}"
112+
r += f"&contains={search_string}"
131113
if id_range:
132-
if prev_flags:
133-
r += f"&idRange={id_range[0]}-{id_range[1]}"
134-
else:
135-
r += f"?idRange={id_range[0]}-{id_range[1]}"
114+
r += f"i&dRange={id_range[0]}-{id_range[1]}"
115+
if amount > 10:
116+
raise ValueError(
117+
f"amount parameter must be no greater than 10. you passed {amount}."
118+
)
119+
r += f"&amount={amount}"
120+
121+
r += f"&lang={lang}"
136122

137123
return r
138124

139-
def send_request(self, request, response_format, return_headers, auth_token, user_agent):
125+
def send_request(self,
126+
request,
127+
response_format,
128+
return_headers,
129+
auth_token,
130+
user_agent
131+
):
140132
returns = []
141133

142134
if auth_token:
143-
r = self.http.request('GET', request, headers={'Authorization': str(
144-
auth_token), 'user-agent': str(user_agent)})
135+
r = self.http.request('GET',
136+
request,
137+
headers={'Authorization': str(auth_token),
138+
'user-agent': str(user_agent),
139+
#'accept-encoding': 'gzip'
140+
}
141+
)
145142
else:
146143
r = self.http.request('GET', request, headers={'user-agent': str(user_agent)})
147144

148-
data = r.data
145+
data = r.data.decode('utf-8')
149146

150147
if response_format == "json":
151148
try:
152149
data = json.loads(data)
153150
except:
154151
print(data)
155152
raise
153+
else:
154+
if len(' '.join(re.split("error", data.lower().replace("\n", "NEWLINECHAR"))[0:][1:]).replace(
155+
'<', '').replace('/', '').replace(' ', '').replace(':', '').replace('>', '').replace('NEWLINECHAR', '\n')) == 4:
156+
return [Exception(f"API returned an error. Full response: \n\n {data}")]
156157

157158
headers = str(r.headers).replace(r'\n', '').replace(
158159
'\n', '').replace(r'\\', '').replace(r"\'", '')[15:-1]
@@ -176,12 +177,14 @@ def get_joke(
176177
type=None,
177178
search_string=None,
178179
id_range=None,
180+
amount=1,
181+
lang=None,
179182
auth_token=None,
180183
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0",
181184
return_headers=False
182185
):
183186
r = self.build_request(
184-
category, blacklist, response_format, type, search_string, id_range
187+
category, blacklist, response_format, type, search_string, id_range, amount, lang
185188
)
186189

187190
response = self.send_request(r, response_format, return_headers, auth_token, user_agent)

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
setuptools.setup(
77
name="jokeapi",
88
packages=["jokeapi"],
9-
version="0.2.2",
9+
version="0.2.6",
1010
license="GNU General Public License v3 (GPLv3)",
1111
description="An API Wrapper for Sv443's JokeAPI",
1212
long_description=long_description,
1313
long_description_content_type="text/markdown",
1414
author="thenamesweretakenalready",
1515
author_email="[email protected]",
1616
url="""https://github.com/thenamesweretakenalready/Sv443s-JokeAPI-Python-Wrapper""",
17-
download_url="https://github.com/thenamesweretakenalready/Sv443s-JokeAPI-Python-Wrapper/archive/v0.2.2.tar.gz",
17+
download_url="https://github.com/thenamesweretakenalready/Sv443s-JokeAPI-Python-Wrapper/archive/v0.2.6.tar.gz",
1818
keywords=["api wrapper", "wrapper", "api", "jokes", "python", "joke api"],
1919
install_requires=[
2020
"urllib3==1.25.8",

0 commit comments

Comments
 (0)