-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
94 lines (85 loc) · 3.82 KB
/
app.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
87
88
89
90
91
92
93
94
import sqlite3
import csv
import sys
import os
import json
import re
from flask import Flask, redirect, url_for, Response, make_response, request, current_app
from werkzeug import secure_filename
from datetime import timedelta
from functools import update_wrapper
from flask import render_template
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'
@app.route('/index')
def hello(name=None):
return render_template('gabe.html')
@app.route("/api/<state>/<year>/<cla>")
def patent(state, year, cla):
print state
import json
conn = sqlite3.connect('/data/patentdata/LATEST/invpat.sqlite3')
c = conn.cursor()
if state == "empty" and cla == "empty":
print "not here"
c.execute('''
select Patent, Longitude, Latitude, Lastname, Firstname, Assignee from invpat where Country = "US" AND AppYear = {year} limit 35000;
'''.format(year=year))
elif state == "empty" and cla != "empty":
print "here"
c.execute('''
select Patent, Longitude, Latitude, Lastname, Firstname, Assignee from invpat where Country = "US" AND AppYear = {year} AND Class like "{cla}/%" limit 35000;
'''.format(year=year, cla=cla))
elif cla != "empty":
c.execute('''
select Patent, Longitude, Latitude, Lastname, Firstname, Assignee from invpat where State = "{state}" AND AppYear = {year} And Class like "{cla}/%" limit 30000;
'''.format(state=state, year=year, cla=cla))
#;
#select Patent, Latitude, Longitude, count(*) from (select * from invpat where State = "{state}" and AppYear = {year} And Class like "{cla}/%") group by Latitude, Longitude limit 200;
else :
c.execute('''
select Patent, Longitude, Latitude, Lastname, Firstname, Assignee from (select * from (select * from invpat where State = "{state}") where AppYear = {year});
'''.format(state=state, year=year))
#
#select Patent, Longitude, Latitude from (select * from invpat where State = "{state}" and AppYear = {year}) group by Latitude, Longitude;
results = c.fetchall()
d = dict(zip([x[0] for x in results],[x[1:] for x in results]))
jsonout = json.dumps(d, indent=4)
if 'callback' in request.args:
jsonout = "{callback}({json})".format(callback=request.args["callback"], json=jsonout)
elif 'jsonp' in request.args:
jsonout = "{callback}({json})".format(callback=request.args["jsonp"], json=jsonout)
return Response(jsonout, mimetype="application/json")
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1] in 'txt'
@app.route("/upload/", methods=['POST'])
def upload_file():
print request
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
print filepath
file.save(filepath)
return parse(filepath)
def parse(filename):
f = open(filename, 'rb')
conn = sqlite3.connect('/data/patentdata/LATEST/invpat.sqlite3')
c = conn.cursor()
results = []
for line in f:
line = line.strip()
c.execute('''
select Patent, Longitude, Latitude, Lastname, Firstname, Assignee from invpat where Patent = "0{pat}";
'''.format(pat=line))
results += c.fetchall()
d = dict(zip([x[0] for x in results],[x[1:] for x in results]))
jsonout = json.dumps(d, indent=4)
if 'callback' in request.args:
jsonout = "{callback}({json})".format(callback=request.args["callback"], json=jsonout)
elif 'jsonp' in request.args:
jsonout = "{callback}({json})".format(callback=request.args["jsonp"], json=jsonout)
return Response(jsonout, mimetype="application/json")
if __name__ == "__main__":
app.run(host='0.0.0.0',debug=True)