-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
76 lines (58 loc) · 1.7 KB
/
app.py
File metadata and controls
76 lines (58 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
from flask import Flask
from flask import jsonify
from flask import request
from flask import Response
from flask import send_file
import json
import os
import random
from datetime import date
app = Flask(__name__)
quotes = []
with open('data/quotes.json') as quotes_json:
# A magical one-liner. Please don't touch.
quotes = json.loads(quotes_json.read().replace('\n', ''))
def _date_index(date, start=date(2013, 11, 11)):
return (date - start).days
def JSON(quote):
return jsonify(quote)
def TXT(quote):
response = Response()
response.data = '%s -- %s' % (quote['content'], quote['author'])
response.mimetype = 'text/plain'
return response
mapping = {
'.txt': TXT,
'.json': JSON
}
@app.route('/quote')
def quote():
return quote_ext('.txt')
@app.route('/quote<string:extension>')
def quote_ext(extension):
quote = random.choice(quotes)
if not extension.lower() in mapping.keys():
return TXT(quote)
return mapping[extension.lower()](quote)
@app.route('/quoteOTD')
def quoteOTD():
return quoteOTD_ext('.txt')
@app.route('/quoteOTD<string:extension>')
def quoteOTD_ext(extension):
index = _date_index(date.today())
quote = quotes[index%len(quotes)]
if not extension.lower() in mapping.keys():
return TXT(quote)
return mapping[extension.lower()](quote)
@app.route('/picture.jpg')
def picture():
return send_file('images/'+random.choice(os.listdir('images')))
@app.route('/pictureOTD.jpg')
def pictureOTD():
index = _date_index(date.today())
images = os.listdir('images/')
image = images[index%len(images)]
return send_file('images/'+image)
if __name__ == '__main__':
app.run(debug=True)