forked from tanner/qotnews
Serve client through apiserver, adding meta info
This commit is contained in:
parent
25a671f58e
commit
f0721519e1
|
@ -7,11 +7,13 @@ import copy
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
import shelve
|
import shelve
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
import feed
|
import feed
|
||||||
from utils import gen_rand_id
|
from utils import gen_rand_id
|
||||||
|
|
||||||
from flask import abort, Flask, request
|
from flask import abort, Flask, request, render_template
|
||||||
|
from werkzeug.exceptions import NotFound
|
||||||
from flask_cors import CORS
|
from flask_cors import CORS
|
||||||
|
|
||||||
CACHE_LENGTH = 300
|
CACHE_LENGTH = 300
|
||||||
|
@ -25,11 +27,20 @@ with shelve.open(DATA_FILE) as db:
|
||||||
news_ref_to_id = db.get('news_ref_to_id', {})
|
news_ref_to_id = db.get('news_ref_to_id', {})
|
||||||
news_cache = db.get('news_cache', {})
|
news_cache = db.get('news_cache', {})
|
||||||
|
|
||||||
flask_app = Flask(__name__)
|
def get_story(id):
|
||||||
|
if id in news_cache:
|
||||||
|
return {'story': news_cache[id]}
|
||||||
|
with shelve.open(DATA_FILE) as db:
|
||||||
|
if id in db:
|
||||||
|
return {'story': db[id]}
|
||||||
|
return None
|
||||||
|
|
||||||
|
build_folder = '../webclient/build'
|
||||||
|
flask_app = Flask(__name__, template_folder=build_folder, static_folder=build_folder, static_url_path='')
|
||||||
cors = CORS(flask_app)
|
cors = CORS(flask_app)
|
||||||
|
|
||||||
@flask_app.route('/')
|
@flask_app.route('/api')
|
||||||
def index():
|
def api():
|
||||||
front_page = [news_cache[news_ref_to_id[ref]] for ref in news_list]
|
front_page = [news_cache[news_ref_to_id[ref]] for ref in news_list]
|
||||||
front_page = [copy.copy(x) for x in front_page if 'text' in x and x['text']][:100]
|
front_page = [copy.copy(x) for x in front_page if 'text' in x and x['text']][:100]
|
||||||
for story in front_page:
|
for story in front_page:
|
||||||
|
@ -37,16 +48,44 @@ def index():
|
||||||
if 'text' in story: story.pop('text')
|
if 'text' in story: story.pop('text')
|
||||||
return {'stories': front_page}
|
return {'stories': front_page}
|
||||||
|
|
||||||
@flask_app.route('/<id>')
|
@flask_app.route('/api/<id>')
|
||||||
def comments(id):
|
def story(id):
|
||||||
if id in news_cache:
|
return get_story(id) or abort(404)
|
||||||
return {'story': news_cache[id]}
|
|
||||||
|
|
||||||
with shelve.open(DATA_FILE) as db:
|
@flask_app.route('/')
|
||||||
if id in db:
|
def index():
|
||||||
return {'story': db[id]}
|
return render_template('index.html',
|
||||||
|
title='Feed',
|
||||||
|
url='news.t0.vc',
|
||||||
|
description='Reddit, Hacker News, and Tildes combined, then pre-rendered in reader mode')
|
||||||
|
|
||||||
abort(404)
|
@flask_app.route('/<id>', strict_slashes=False)
|
||||||
|
@flask_app.route('/<id>/c', strict_slashes=False)
|
||||||
|
def static_story(id):
|
||||||
|
try:
|
||||||
|
return flask_app.send_static_file(id)
|
||||||
|
except NotFound:
|
||||||
|
pass
|
||||||
|
|
||||||
|
story = get_story(id)
|
||||||
|
if story:
|
||||||
|
story = story['story']
|
||||||
|
else:
|
||||||
|
return abort(404)
|
||||||
|
|
||||||
|
score = story['score']
|
||||||
|
num_comments = story['num_comments']
|
||||||
|
source = story['source']
|
||||||
|
description = '{} point{}, {} comment{} on {}'.format(
|
||||||
|
score, 's' if score != 1 else '',
|
||||||
|
num_comments, 's' if num_comments != 1 else '',
|
||||||
|
source)
|
||||||
|
url = urlparse(story['url']).hostname.replace('www.', '')
|
||||||
|
|
||||||
|
return render_template('index.html',
|
||||||
|
title=story['title'],
|
||||||
|
url=url,
|
||||||
|
description=description)
|
||||||
|
|
||||||
print('Starting Flask...')
|
print('Starting Flask...')
|
||||||
web_thread = threading.Thread(target=flask_app.run, kwargs={'port': 33842})
|
web_thread = threading.Thread(target=flask_app.run, kwargs={'port': 33842})
|
||||||
|
|
Loading…
Reference in New Issue
Block a user