-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
executable file
·86 lines (69 loc) · 3.49 KB
/
server.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
#!/usr/bin/env python3
# This file provided by Facebook is for non-commercial testing and evaluation purposes only.
# Facebook reserves all rights not expressly granted.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import json
import os
import rethinkdb
from flask import Flask, Response, request
import hn
LIMIT = 700
app = Flask(__name__, static_url_path='', static_folder='public')
app.add_url_rule('/', 'root', lambda: app.send_static_file('./index.html'))
@app.route('/hnjobs', methods=['GET'])
def comments_handler():
cursor = app.table.filter({'parent': app.main_id, 'type': 'comment'})
navmode = request.args.get('navmode', '0')
if navmode == '1': # cool
cursor = cursor.filter(rethinkdb.row['cool'].gt(1))
elif navmode == '2': # uncool
cursor = cursor.filter({'cool': 0})
elif navmode == '3': # potential remote
cursor = cursor.filter({'cool': 1}).filter(rethinkdb.row['text'].match('(?i)remote'))
else: # potential
cursor = cursor.filter({'cool': 1})
jobfilter = request.args.get('filter', None)
if jobfilter:
cursor = cursor.filter(rethinkdb.row['text'].match(r'(?i){}'.format(jobfilter)))
jobs = list(cursor.limit(LIMIT).run(app.connection))
return Response(json.dumps(jobs), mimetype='application/json', headers={'Cache-Control': 'no-cache'})
@app.route('/hnjobs/latest', methods=['GET'])
def latest_handler():
counters = app.table.group('cool').count().run(app.connection)
rtv = { \
'url': 'https://news.ycombinator.com/item?id={}'.format(app.main_id),
'date': app.table.get(app.main_id).run(app.connection)['time'],
'latest': app.table.filter({'parent': app.main_id}).max('time').run(app.connection)['time'],
'liked': counters[2],
'disliked': counters[0],
'unclassified': counters[1],
'total': sum(counters.values())}
return Response(json.dumps(rtv), mimetype='application/json', headers={'Cache-Control': 'no-cache'})
@app.route('/hnjobs/update/<jobid>', methods=['DELETE', 'POST'])
def hide_a_job_handler(jobid):
success = False
try:
jobid = int(jobid)
if request.method == 'DELETE':
rtv = app.table.get(jobid).update({'cool': 0}).run(app.connection)
success = rtv.get('skipped', 1) == 0
app.logger.debug('Hide {} {}'.format(jobid, rtv))
elif request.method == 'POST':
rtv = app.table.get(jobid).update({'cool': rethinkdb.row['cool'] + 1}).run(app.connection)
success = rtv.get('skipped', 1) == 0
app.logger.debug('Updated {} {}'.format(jobid, rtv))
rtv = {'success': success}
except ValueError:
pass
return Response(json.dumps(rtv), mimetype='application/json', headers={'Cache-Control': 'no-cache'})
def main():
app.connection, app.table, app.main_id = hn.get_connection()
app.run(port=int(os.environ.get('PORT', 3000)), debug=True)
if __name__ == '__main__':
main()