Skip to content

Commit 086220f

Browse files
committed
Fixed encoding issues, optimized JSON file reads
1 parent 7faef2c commit 086220f

File tree

4 files changed

+638
-32
lines changed

4 files changed

+638
-32
lines changed

app.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# -*- coding: utf-8 -*-
2-
from flask import Flask, jsonify
2+
from flask import Flask, jsonify, request
33
from flask_restful import Resource, Api
4-
import joke
5-
import sys
64
from flask_cors import CORS
75

6+
import joke
7+
88
app = Flask(__name__)
99
api = Api(app)
1010

@@ -15,28 +15,31 @@ class APP(Resource):
1515

1616
def get(self):
1717
return jsonify({
18-
'author' : 'Sameer Kumar',
19-
'author_url' : 'https://www.sameerkumar.website',
20-
'base_url' : 'https://geek-jokes.sameerkumar.website',
21-
'project_name' : 'Geek Joke API',
22-
'project_url' : 'https://github.com/sameerkumar18/geek-joke-api'
23-
})
24-
18+
'author': 'Sameer Kumar',
19+
'author_url': 'https://www.sameerkumar.website',
20+
'base_url': 'https://geek-jokes.sameerkumar.website',
21+
'project_name': 'Geek Joke API',
22+
'project_url': 'https://github.com/sameerkumar18/geek-joke-api'
23+
})
2524

2625
def post(self):
2726
jk = joke.get_joke()
28-
jk = jk.encode('ascii', 'ignore').decode('ascii')
29-
#jk = jk.encode('utf-8')
30-
return jk
27+
if request.args.get('format') == 'json':
28+
return {'joke': jk}
29+
else:
30+
# Making sure existing clients don't break
31+
return jk
3132

3233

3334
class API(Resource):
3435

3536
def get(self):
3637
jk = joke.get_joke()
37-
jk = jk.encode('ascii', 'ignore').decode('ascii')
38-
# jk = jk.encode('utf-8')
39-
return jk
38+
if request.args.get('format') == 'json':
39+
return {'joke': jk}
40+
else:
41+
# Making sure existing clients don't break
42+
return jk
4043

4144

4245
api.add_resource(APP, '/')

data.json

Lines changed: 609 additions & 1 deletion
Large diffs are not rendered by default.

joke.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
import json
33
from random import randint
44

5+
with open('data.json', encoding='utf-8-sig') as data_file:
6+
data = json.load(data_file)
7+
JOKE_COUNT = len(data)
58

6-
def random_digits(joke_count):
7-
# Return a joke index between first and last joke in data
8-
return randint(1, joke_count)
99

1010
def get_joke():
1111
# Return random joke
12-
with open('data.json', encoding="utf8") as data_file:
13-
data = json.load(data_file)
14-
joke = data[random_digits(len(data))]
15-
return joke
12+
random_num = randint(1, JOKE_COUNT)
13+
joke = data[random_num]
14+
return joke.replace('\"', "'") + str(random_num)

requirements.txt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
Flask==1.0.2
2-
gunicorn==19.9.0
3-
httplib2==0.11.3
4-
Jinja2==2.10
5-
virtualenv==16.0.0
6-
Werkzeug==0.14.1
7-
flask-restful
8-
flask-cors==3.0.6
1+
Flask==1.1.1
2+
gunicorn==20.0.4
3+
flask-restful==0.3.8
4+
flask-cors==3.0.7

0 commit comments

Comments
 (0)