-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
129 lines (105 loc) · 3.42 KB
/
main.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import falcon
import os
import pystache
import logging
import json
import random
from wsgi_static_middleware import StaticMiddleware
from falcon_sslify import FalconSSLify
try:
import bmemcached
memcached = bmemcached.Client(
os.environ.get('MEMCACHEDCLOUD_SERVERS').split(','),
os.environ.get('MEMCACHEDCLOUD_USERNAME'),
os.environ.get('MEMCACHEDCLOUD_PASSWORD')
)
except:
from pymemcache.client.base import Client
memcached = Client(('127.0.0.1', 11211))
BASE_DIR = os.path.dirname(__name__)
STATIC_DIRS = [os.path.join(BASE_DIR, 'public')]
LIVE_URL = 'https://www.scusedisarri.it'
log = logging.getLogger('SarriLogger')
def get_from_cache(key):
value = memcached.get('SARRI-' + key)
return value
def set_value_in_cache(key, value):
value = memcached.set('SARRI-' + key, value.encode('UTF-8'))
return value
def load_template(filename, context={}):
content = get_from_cache(filename)
if not content:
f = open('public/templates/' + filename)
content = f.read()
set_value_in_cache(filename, content)
f.close()
return pystache.render(content, context)
def pick_quote(searched_quote=None):
data = get_from_cache('quotes')
if not data:
f = open('resources/quotes.json')
quotes_json = f.read()
data = json.loads(quotes_json)
set_value_in_cache('quotes', quotes_json)
f.close()
else:
data = json.loads(data)
if searched_quote:
for quote in data["quotes"]:
if quote['quote_url_share'] == searched_quote:
return (quote)
return None
else:
number_of_quotes = len(data["quotes"])
quote_index = random.randint(0, number_of_quotes-1)
quote = data["quotes"][quote_index]
return (quote)
def pick_og_image():
data = get_from_cache('quotes')
if not data:
f = open('resources/quotes.json')
quotes_json = f.read()
data = json.loads(quotes_json)
set_value_in_cache('quotes', quotes_json)
f.close()
else:
data = json.loads(data)
number_of_images = len(data["og_images"])
og_image_index = random.randint(0,number_of_images-1)
return data["og_images"][og_image_index]["url"]
class MainResource:
def on_get(self, req, resp):
resp.status = falcon.HTTP_200
resp.content_type = 'text/html'
context = {
'live_url' : LIVE_URL,
'og_image_url' : pick_og_image()
}
resp.body = load_template('index.html', context)
class QuoteResource:
def on_get(self, req, resp, quote=None):
resp.status = falcon.HTTP_200
resp.content_type = 'text/html'
picked_quote = pick_quote(quote)
if picked_quote == None:
resp.status = falcon.HTTP_301
resp.set_header('Location', '/dice')
else:
context = picked_quote
context.update({
'live_url' : LIVE_URL,
'og_image_url' : pick_og_image()
})
resp.body = load_template('quote.html', context)
memcached.flush_all()
sslify = FalconSSLify()
DEV = os.environ.get('SARRI_DEV', 0)
if DEV:
log.error(DEV)
app = falcon.API()
else:
app = falcon.API()
app.add_route('/', MainResource())
app.add_route('/dice', QuoteResource())
app.add_route('/dice/{quote}', QuoteResource())
app = StaticMiddleware(app, static_root='static', static_dirs=STATIC_DIRS)