Skip to content

Commit b0e5d50

Browse files
committed
initial commit
0 parents  commit b0e5d50

File tree

10 files changed

+795
-0
lines changed

10 files changed

+795
-0
lines changed

.gitignore

+177
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
# *.json
2+
saves/
3+
*.stuff
4+
test*
5+
output*
6+
*.yaml
7+
*.yml
8+
*.log
9+
users.json
10+
tokens.json
11+
static/user_data/
12+
13+
# Byte-compiled / optimized / DLL files
14+
__pycache__/
15+
*.py[cod]
16+
*$py.class
17+
18+
# C extensions
19+
*.so
20+
21+
# Distribution / packaging
22+
.Python
23+
build/
24+
develop-eggs/
25+
dist/
26+
downloads/
27+
eggs/
28+
.eggs/
29+
lib/
30+
lib64/
31+
parts/
32+
sdist/
33+
var/
34+
wheels/
35+
share/python-wheels/
36+
*.egg-info/
37+
.installed.cfg
38+
*.egg
39+
MANIFEST
40+
41+
# PyInstaller
42+
# Usually these files are written by a python script from a template
43+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
44+
*.manifest
45+
*.spec
46+
47+
# Installer logs
48+
pip-log.txt
49+
pip-delete-this-directory.txt
50+
51+
# Unit test / coverage reports
52+
htmlcov/
53+
.tox/
54+
.nox/
55+
.coverage
56+
.coverage.*
57+
.cache
58+
nosetests.xml
59+
coverage.xml
60+
*.cover
61+
*.py,cover
62+
.hypothesis/
63+
.pytest_cache/
64+
cover/
65+
66+
# Translations
67+
*.mo
68+
*.pot
69+
70+
# Django stuff:
71+
*.log
72+
local_settings.py
73+
db.sqlite3
74+
db.sqlite3-journal
75+
76+
# Flask stuff:
77+
instance/
78+
.webassets-cache
79+
80+
# Scrapy stuff:
81+
.scrapy
82+
83+
# Sphinx documentation
84+
docs/_build/
85+
86+
# PyBuilder
87+
.pybuilder/
88+
target/
89+
90+
# Jupyter Notebook
91+
.ipynb_checkpoints
92+
93+
# IPython
94+
profile_default/
95+
ipython_config.py
96+
97+
# pyenv
98+
# For a library or package, you might want to ignore these files since the code is
99+
# intended to run in multiple environments; otherwise, check them in:
100+
# .python-version
101+
102+
# pipenv
103+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
104+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
105+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
106+
# install all needed dependencies.
107+
#Pipfile.lock
108+
109+
# poetry
110+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
111+
# This is especially recommended for binary packages to ensure reproducibility, and is more
112+
# commonly ignored for libraries.
113+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
114+
#poetry.lock
115+
116+
# pdm
117+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
118+
#pdm.lock
119+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
120+
# in version control.
121+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
122+
.pdm.toml
123+
.pdm-python
124+
.pdm-build/
125+
126+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
127+
__pypackages__/
128+
129+
# Celery stuff
130+
celerybeat-schedule
131+
celerybeat.pid
132+
133+
# SageMath parsed files
134+
*.sage.py
135+
136+
# Environments
137+
.env
138+
.venv
139+
env/
140+
venv/
141+
ENV/
142+
env.bak/
143+
venv.bak/
144+
145+
# Spyder project settings
146+
.spyderproject
147+
.spyproject
148+
149+
# Rope project settings
150+
.ropeproject
151+
152+
# mkdocs documentation
153+
/site
154+
155+
# mypy
156+
.mypy_cache/
157+
.dmypy.json
158+
dmypy.json
159+
160+
# Pyre type checker
161+
.pyre/
162+
163+
# pytype static type analyzer
164+
.pytype/
165+
166+
# Cython debug symbols
167+
cython_debug/
168+
169+
# PyCharm
170+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
171+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
172+
# and can be added to the global gitignore or merged into this file. For a more nuclear
173+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
174+
#.idea/
175+
176+
# macOS Cache
177+
.DS_STORE

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Joke API (Example)
2+
3+
This is an example for the (not yet official) RaspAPI YSWS program.
4+
5+
## Routes

app.py

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
from flask import Flask, jsonify, request, render_template
2+
import json
3+
import random
4+
import datetime
5+
from lepton import generate_joke
6+
7+
with open('jokes.json', 'r') as f:
8+
jokes = json.load(f)
9+
10+
app = Flask(__name__)
11+
12+
@app.route('/', methods=['GET', 'POST'])
13+
def index():
14+
if request.method == 'POST':
15+
random.seed()
16+
return jsonify(random.choice(jokes))
17+
else:
18+
random.seed()
19+
joke = random.choice(jokes)
20+
# calculate joke of the day
21+
random.seed(hash(datetime.datetime.now().strftime('%Y-%m-%d')))
22+
joke_number = random.randint(0, len(jokes) - 1)
23+
joke_of_the_day = jokes[joke_number]
24+
# render html page
25+
html_page = render_template('index.html', joke=joke, html_page="<html>...</html>", jokes=jokes, joke_of_the_day=joke_of_the_day)
26+
return render_template('index.html', joke=joke, html_page=html_page, jokes=jokes, joke_of_the_day=joke_of_the_day)
27+
28+
@app.route('/puns', methods=['POST'])
29+
def puns():
30+
data = request.get_json(silent=True)
31+
if data and 'number' in data:
32+
number = int(data['number'])
33+
else:
34+
number = len(jokes)
35+
36+
return jsonify(random.choices(jokes, k=number))
37+
38+
@app.route('/joke-of-the-day', methods=['POST'])
39+
def joke_of_the_day():
40+
date = None
41+
if request.is_json:
42+
data = request.get_json(silent=True)
43+
if data and 'date' in data:
44+
date = data['date']
45+
46+
if date is None:
47+
date = datetime.datetime.now().strftime('%Y-%m-%d')
48+
49+
random.seed(hash(date))
50+
joke_number = random.randint(0, len(jokes) - 1)
51+
joke_of_the_day = jokes[joke_number]
52+
return jsonify(joke_of_the_day)
53+
54+
@app.route('/generate-joke', methods=['POST'])
55+
def generate_joke_api():
56+
data = request.get_json(silent=True)
57+
if data and 'prompt' in data:
58+
prompt = data['prompt']
59+
else:
60+
prompt = ""
61+
joke_string = generate_joke(prompt)
62+
try:
63+
joke_json = json.loads(joke_string)
64+
return jsonify(joke_json)
65+
except json.JSONDecodeError:
66+
return jsonify({"error": "Something went wrong while generating the joke"}), 500
67+
68+
@app.route('/search-jokes', methods=['POST'])
69+
def search_jokes():
70+
data = request.get_json(silent=True)
71+
if data and 'query' in data:
72+
query = data['query']
73+
else:
74+
return jsonify({"error": "No query provided"}), 400
75+
if data and 'limit' in data:
76+
limit = int(data['limit'])
77+
else:
78+
limit = 10
79+
80+
results = []
81+
for joke in jokes:
82+
if query in joke['q'] or query in joke['a'] or query in joke['tags']:
83+
results.append(joke)
84+
if len(results) == limit:
85+
break
86+
return jsonify(results)
87+
88+
@app.route('/get-jokes-by-tag', methods=['POST'])
89+
def get_jokes_by_tag():
90+
data = request.get_json(silent=True)
91+
if data and 'tag' in data:
92+
tag = data['tag']
93+
else:
94+
return jsonify({"error": "No tag provided"}), 400
95+
96+
results = []
97+
for joke in jokes:
98+
if tag in joke['tags']:
99+
results.append(joke)
100+
return jsonify(results)
101+
102+
if __name__ == '__main__':
103+
app.run(host='0.0.0.0', port=8080, debug=True)

0 commit comments

Comments
 (0)