-
Notifications
You must be signed in to change notification settings - Fork 4
/
lunch.py
executable file
·43 lines (34 loc) · 1.1 KB
/
lunch.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
# -*- encoding: utf-8 -*-
import os
import json
import scrapers
from datetime import date
from cache import format_cache_file
from bottle import route, run, response, request
PORT = 40000
@route('/')
def index():
cached_file, cache_dir = format_cache_file()
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
ret = ""
if os.path.isfile(cached_file):
with open(cached_file) as f:
ret = f.read()
else:
print("Scraping new day")
daily_specials = scrapers.get_daily_specials()
with open(cached_file, "w") as f:
print("Saving cache as %s" % cached_file)
f.write(json.dumps(daily_specials))
ret = json.dumps(daily_specials)
content_type = 'application/json'
if request.query.callback:
ret = "{cb}({json});".format(cb=request.query.callback, json=ret)
content_type = 'application/javascript'
print(request.environ.get('REMOTE_ADDR'))
response.set_header('Access-Control-Allow-Origin', '*')
response.set_header('Access-Control-Allow-Headers', 'x-requested-with')
response.content_type = content_type + '; charset=UTF8'
return ret
run(server='cheroot', host='0.0.0.0', port=PORT)