#!python import os import sys import json import time import argparse from datetime import datetime from flask import Flask, request, render_template, jsonify from dotenv import load_dotenv load_dotenv() from util import * locations_worksheet = fetch_worksheet('paper_locations') paper_lookup = fetch_google_lookup('citation_lookup') addresses = AddressBook() app = Flask(__name__, static_url_path="/reports", static_folder=os.path.abspath("reports")) # static api route @app.route('/', methods=['GET']) def index(): return app.send_static_file('geocode_papers.html') @app.errorhandler(404) def page_not_found(e): return app.send_static_file('geocode_papers.html') # route to get all the manually geocoded IDs (to dedupe) # route to add a geocoding for a paper @app.route('/api/institutions', methods=['GET']) def list_locations(): return jsonify({ 'entities': addresses.entities, 'lookup': addresses.lookup, }) @app.route('/api/papers', methods=['GET']) def list_papers(): return jsonify({ 'papers': paper_lookup, }) @app.route('/api/address/', methods=['GET']) def find_address(sha256): worksheet = fetch_worksheet('paper_locations') try: cell = worksheet.find(sha256) except: return jsonify({ 'error': 'no_match' }) if cell and cell.row: keys = worksheet.row_values(1) values_list = worksheet.row_values(cell.row) lookup = {} for key, value in zip(keys, values_list): lookup[key] = value return jsonify({ 'paper': lookup, }) else: return jsonify({ 'error': 'no_match' }) @app.route('/api/address/add', methods=['POST']) def add_address(): # id, title, institution_1, institution_2, institution_3, institution_4, notes locations_worksheet.insert_row([ request.form['paper_id'], request.form['title'], request.form['institution_1'], request.form['institution_2'], request.form['institution_3'], request.form['institution_4'], request.form['notes'], ]) return jsonify({ 'status': 'ok' }) if __name__=="__main__": app.run("0.0.0.0", debug=False)