Skip to content

Instantly share code, notes, and snippets.

@akdh
Last active August 29, 2015 14:20
Show Gist options
  • Save akdh/65c6391eb85b553fcf12 to your computer and use it in GitHub Desktop.
Save akdh/65c6391eb85b553fcf12 to your computer and use it in GitHub Desktop.

Run

See https://github.com/akdh/cst-tools for JSON schemas.

To run install the required dependencies and launch the server.

pip install flask jsonschema
python main.py

Suggestion requests can now be made to this server, see https://gist.github.com/akdh/564cbb5f9c6c92c99d8a.

Note that you must have docs.db in the same directory as main.py.

You can create this file with by running:

cat lod_docs.sql | sqlite3 docs.db

(collection_2015.csv must be in the current directory when this is run).

.mode csv
CREATE TABLE docs (id TEXT PRIMARY KEY, location INT, url TEXT, title TEXT);
.import collection_2015.csv docs
from flask import Flask, jsonify, request, abort
import random, sqlite3, jsonschema, json
app = Flask(__name__)
def get_ids_for_location(location, db_filename = 'docs.db'):
conn = sqlite3.connect(db_filename)
cur = conn.cursor()
cur.execute('select id from docs where location = ?', (location, ))
rows = cur.fetchall()
conn.close()
return [row[0] for row in rows]
@app.route('/suggestions', methods=['POST'])
def suggestions():
data = request.get_json()
with open("request_schema.json") as f:
schema = json.loads(f.read())
print(schema)
jsonschema.validate(schema, data)
location = data['location']
person = data['person']
# Get a set of random IDs for the location
# TODO: Replace this with a more intelligent recommendation strategy
ids = get_ids_for_location(location['id'])
suggestions = random.sample(ids, 10)
return jsonify({'suggestions': suggestions})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5002)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment