2019-08-23 08:23:48 +00:00
|
|
|
import logging
|
|
|
|
logging.basicConfig(
|
|
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
|
|
level=logging.INFO)
|
|
|
|
|
2020-10-24 22:30:00 +00:00
|
|
|
import gevent
|
|
|
|
from gevent import monkey
|
|
|
|
monkey.patch_all()
|
|
|
|
from gevent.pywsgi import WSGIServer
|
|
|
|
|
2019-08-23 08:23:48 +00:00
|
|
|
import copy
|
2020-06-26 21:28:39 +00:00
|
|
|
import json
|
2019-08-23 08:23:48 +00:00
|
|
|
import threading
|
2020-06-25 23:38:16 +00:00
|
|
|
import traceback
|
2019-08-23 08:23:48 +00:00
|
|
|
import time
|
2019-12-02 23:15:51 +00:00
|
|
|
from urllib.parse import urlparse, parse_qs
|
2019-08-23 08:23:48 +00:00
|
|
|
|
2020-06-26 21:28:39 +00:00
|
|
|
import database
|
2020-06-27 22:53:39 +00:00
|
|
|
import search
|
2019-08-24 08:49:11 +00:00
|
|
|
import feed
|
2019-08-28 04:13:02 +00:00
|
|
|
from utils import gen_rand_id
|
2019-08-24 08:49:11 +00:00
|
|
|
|
2019-11-08 05:55:30 +00:00
|
|
|
from flask import abort, Flask, request, render_template, stream_with_context, Response
|
2019-10-10 21:54:29 +00:00
|
|
|
from werkzeug.exceptions import NotFound
|
2019-08-23 08:23:48 +00:00
|
|
|
from flask_cors import CORS
|
|
|
|
|
2020-06-26 21:28:39 +00:00
|
|
|
database.init()
|
2020-06-27 22:53:39 +00:00
|
|
|
search.init()
|
2019-08-23 08:23:48 +00:00
|
|
|
|
2020-06-26 21:28:39 +00:00
|
|
|
FEED_LENGTH = 75
|
2019-08-23 08:23:48 +00:00
|
|
|
news_index = 0
|
2019-08-24 05:07:16 +00:00
|
|
|
|
2019-11-08 05:55:30 +00:00
|
|
|
def new_id():
|
|
|
|
nid = gen_rand_id()
|
2020-06-26 21:28:39 +00:00
|
|
|
while database.get_story(nid):
|
2019-11-08 05:55:30 +00:00
|
|
|
nid = gen_rand_id()
|
|
|
|
return nid
|
|
|
|
|
2019-10-10 21:54:29 +00:00
|
|
|
build_folder = '../webclient/build'
|
|
|
|
flask_app = Flask(__name__, template_folder=build_folder, static_folder=build_folder, static_url_path='')
|
2019-08-23 08:23:48 +00:00
|
|
|
cors = CORS(flask_app)
|
|
|
|
|
2019-10-10 21:54:29 +00:00
|
|
|
@flask_app.route('/api')
|
|
|
|
def api():
|
2020-06-26 21:28:39 +00:00
|
|
|
stories = database.get_stories(FEED_LENGTH)
|
2020-11-09 02:45:10 +00:00
|
|
|
res = Response(json.dumps({"stories": stories}))
|
2020-06-26 21:28:39 +00:00
|
|
|
res.headers['content-type'] = 'application/json'
|
|
|
|
return res
|
2019-08-23 08:23:48 +00:00
|
|
|
|
2019-10-12 05:32:17 +00:00
|
|
|
@flask_app.route('/api/search', strict_slashes=False)
|
2020-06-27 22:53:39 +00:00
|
|
|
def apisearch():
|
2020-06-26 21:28:39 +00:00
|
|
|
q = request.args.get('q', '')
|
|
|
|
if len(q) >= 3:
|
2020-06-27 22:53:39 +00:00
|
|
|
results = search.search(q)
|
2019-10-12 05:32:17 +00:00
|
|
|
else:
|
2020-06-26 21:28:39 +00:00
|
|
|
results = []
|
2020-06-27 22:53:39 +00:00
|
|
|
return dict(results=results)
|
2019-10-12 05:32:17 +00:00
|
|
|
|
2019-11-08 05:55:30 +00:00
|
|
|
@flask_app.route('/api/submit', methods=['POST'], strict_slashes=False)
|
|
|
|
def submit():
|
2019-12-02 23:15:51 +00:00
|
|
|
try:
|
|
|
|
url = request.form['url']
|
|
|
|
nid = new_id()
|
|
|
|
|
|
|
|
parse = urlparse(url)
|
|
|
|
if 'news.ycombinator.com' in parse.hostname:
|
|
|
|
source = 'hackernews'
|
|
|
|
ref = parse_qs(parse.query)['id'][0]
|
|
|
|
elif 'tildes.net' in parse.hostname and '~' in url:
|
|
|
|
source = 'tildes'
|
|
|
|
ref = parse.path.split('/')[2]
|
|
|
|
elif 'reddit.com' in parse.hostname and 'comments' in url:
|
|
|
|
source = 'reddit'
|
|
|
|
ref = parse.path.split('/')[4]
|
2020-06-27 22:53:39 +00:00
|
|
|
elif 'news.t0.vc' in parse.hostname:
|
|
|
|
raise Exception('Invalid article')
|
2019-12-02 23:15:51 +00:00
|
|
|
else:
|
|
|
|
source = 'manual'
|
|
|
|
ref = url
|
|
|
|
|
2020-06-27 22:53:39 +00:00
|
|
|
existing = database.get_story_by_ref(ref)
|
|
|
|
if existing:
|
|
|
|
return {'nid': existing.sid}
|
2019-12-02 23:15:51 +00:00
|
|
|
else:
|
2020-06-27 22:53:39 +00:00
|
|
|
story = dict(id=nid, ref=ref, source=source)
|
|
|
|
valid = feed.update_story(story, is_manual=True)
|
|
|
|
if valid:
|
|
|
|
database.put_story(story)
|
|
|
|
search.put_story(story)
|
|
|
|
return {'nid': nid}
|
|
|
|
else:
|
|
|
|
raise Exception('Invalid article')
|
2019-12-02 23:15:51 +00:00
|
|
|
|
|
|
|
except BaseException as e:
|
|
|
|
logging.error('Problem with article submission: {} - {}'.format(e.__class__.__name__, str(e)))
|
2020-06-25 23:38:16 +00:00
|
|
|
print(traceback.format_exc())
|
2019-11-08 05:55:30 +00:00
|
|
|
abort(400)
|
|
|
|
|
2019-12-02 23:15:51 +00:00
|
|
|
|
2019-10-15 20:36:20 +00:00
|
|
|
@flask_app.route('/api/<sid>')
|
|
|
|
def story(sid):
|
2020-06-26 21:28:39 +00:00
|
|
|
story = database.get_story(sid)
|
|
|
|
if story:
|
2020-11-09 02:45:10 +00:00
|
|
|
res = Response(json.dumps({"story": story.data}))
|
2020-06-26 21:28:39 +00:00
|
|
|
res.headers['content-type'] = 'application/json'
|
|
|
|
return res
|
|
|
|
else:
|
|
|
|
return abort(404)
|
2019-08-24 05:07:16 +00:00
|
|
|
|
2019-10-10 21:54:29 +00:00
|
|
|
@flask_app.route('/')
|
2019-10-15 20:36:20 +00:00
|
|
|
@flask_app.route('/search')
|
2019-10-10 21:54:29 +00:00
|
|
|
def index():
|
|
|
|
return render_template('index.html',
|
|
|
|
title='Feed',
|
|
|
|
url='news.t0.vc',
|
|
|
|
description='Reddit, Hacker News, and Tildes combined, then pre-rendered in reader mode')
|
|
|
|
|
2019-10-15 20:36:20 +00:00
|
|
|
@flask_app.route('/<sid>', strict_slashes=False)
|
|
|
|
@flask_app.route('/<sid>/c', strict_slashes=False)
|
|
|
|
def static_story(sid):
|
2019-10-10 21:54:29 +00:00
|
|
|
try:
|
2019-10-15 20:36:20 +00:00
|
|
|
return flask_app.send_static_file(sid)
|
2019-10-10 21:54:29 +00:00
|
|
|
except NotFound:
|
|
|
|
pass
|
|
|
|
|
2020-06-26 21:28:39 +00:00
|
|
|
story = database.get_story(sid)
|
2019-10-12 05:32:17 +00:00
|
|
|
if not story: return abort(404)
|
2020-11-09 02:45:10 +00:00
|
|
|
story = story.data
|
2019-10-10 21:54:29 +00:00
|
|
|
|
|
|
|
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)
|
2019-11-07 22:10:08 +00:00
|
|
|
url = urlparse(story['url']).hostname or urlparse(story['link']).hostname or ''
|
|
|
|
url = url.replace('www.', '')
|
2019-08-24 05:07:16 +00:00
|
|
|
|
2019-10-10 21:54:29 +00:00
|
|
|
return render_template('index.html',
|
|
|
|
title=story['title'],
|
|
|
|
url=url,
|
|
|
|
description=description)
|
2019-08-23 08:23:48 +00:00
|
|
|
|
2020-08-14 03:57:18 +00:00
|
|
|
http_server = WSGIServer(('', 33842), flask_app)
|
2019-08-24 05:07:16 +00:00
|
|
|
|
2019-11-08 02:37:57 +00:00
|
|
|
def feed_thread():
|
|
|
|
global news_index
|
|
|
|
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
# onboard new stories
|
|
|
|
if news_index == 0:
|
2020-06-26 21:28:39 +00:00
|
|
|
for ref, source in feed.list():
|
2020-06-27 22:53:39 +00:00
|
|
|
if database.get_story_by_ref(ref):
|
|
|
|
continue
|
2020-06-26 21:28:39 +00:00
|
|
|
try:
|
|
|
|
nid = new_id()
|
2020-06-27 22:53:39 +00:00
|
|
|
database.put_ref(ref, nid, source)
|
2020-06-26 21:28:39 +00:00
|
|
|
logging.info('Added ref ' + ref)
|
|
|
|
except database.IntegrityError:
|
|
|
|
continue
|
2019-11-08 02:37:57 +00:00
|
|
|
|
2020-06-27 22:53:39 +00:00
|
|
|
ref_list = database.get_reflist(FEED_LENGTH)
|
|
|
|
|
2019-11-08 02:37:57 +00:00
|
|
|
# update current stories
|
2020-06-26 21:28:39 +00:00
|
|
|
if news_index < len(ref_list):
|
2020-06-27 22:53:39 +00:00
|
|
|
item = ref_list[news_index]
|
|
|
|
|
|
|
|
try:
|
2020-11-09 02:45:10 +00:00
|
|
|
story = database.get_story(item['sid']).data
|
2020-06-27 22:53:39 +00:00
|
|
|
except AttributeError:
|
|
|
|
story = dict(id=item['sid'], ref=item['ref'], source=item['source'])
|
|
|
|
|
2020-11-02 00:13:43 +00:00
|
|
|
logging.info('Updating story: ' + str(story['ref']) + ', index: ' + str(news_index))
|
|
|
|
|
2020-06-26 21:28:39 +00:00
|
|
|
valid = feed.update_story(story)
|
2019-11-08 02:37:57 +00:00
|
|
|
if valid:
|
2020-06-26 21:28:39 +00:00
|
|
|
database.put_story(story)
|
2020-06-27 22:53:39 +00:00
|
|
|
search.put_story(story)
|
2019-11-08 02:37:57 +00:00
|
|
|
else:
|
2020-06-27 22:53:39 +00:00
|
|
|
database.del_ref(item['ref'])
|
|
|
|
logging.info('Removed ref {}'.format(item['ref']))
|
2020-07-04 22:39:38 +00:00
|
|
|
else:
|
2020-11-02 00:13:43 +00:00
|
|
|
logging.info('Skipping index: ' + str(news_index))
|
2019-11-08 02:37:57 +00:00
|
|
|
|
2020-07-04 22:39:38 +00:00
|
|
|
gevent.sleep(6)
|
2019-11-08 02:37:57 +00:00
|
|
|
|
|
|
|
news_index += 1
|
2020-06-26 21:28:39 +00:00
|
|
|
if news_index == FEED_LENGTH: news_index = 0
|
2019-11-08 02:37:57 +00:00
|
|
|
|
2019-11-08 05:55:30 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
logging.info('Ending feed thread...')
|
|
|
|
except ValueError as e:
|
2019-11-08 02:37:57 +00:00
|
|
|
logging.error('feed_thread error: {} {}'.format(e.__class__.__name__, e))
|
|
|
|
http_server.stop()
|
|
|
|
|
|
|
|
print('Starting Feed thread...')
|
|
|
|
gevent.spawn(feed_thread)
|
|
|
|
|
|
|
|
print('Starting HTTP thread...')
|
|
|
|
try:
|
|
|
|
http_server.serve_forever()
|
2019-10-15 21:03:47 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
logging.info('Exiting...')
|