summaryrefslogtreecommitdiff
path: root/scraper/s2-geocode-server.py
blob: c9219fe11a73c90ba80c8e5f059648c6fd091d8d (plain)
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
#!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')

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():
  addresses = AddressBook()
  return jsonify({
    'entities': addresses.entities,
    'lookup': addresses.lookup,
  })

@app.route('/api/papers', methods=['GET'])
def list_papers():
  lookup_keys, lines = fetch_google_sheet('citation_lookup')
  paper_lookup = {}
  for line in lines:
    paper_lookup[line[0]] = line
  return jsonify({
    'papers': paper_lookup,
  })

@app.route('/api/address/<sha256>', methods=['GET'])
def find_address(sha256):
  worksheet = fetch_worksheet('paper_locations')
  cell = worksheet.find(sha256)
  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)