-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
219 lines (164 loc) · 6.16 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
from io import StringIO, BytesIO
import json
import urllib.parse
from boto.s3.connection import S3Connection
from boto.s3.key import Key
from boto.exception import S3ResponseError
from flask import Flask, request, make_response, abort
from flask_cors import cross_origin
import requests
import sentry_sdk
from sentry_sdk import capture_message
from sentry_sdk.integrations.flask import FlaskIntegration
from app_config import AWS_KEY, AWS_SECRET, SENTRY_DSN, IMAGE_SECRET
from sentry_handlers import before_send
app = Flask(__name__)
LEGISTARS = {
"chicago": "https://ord.legistar.com/View.ashx",
"nyc": "http://legistar.council.nyc.gov/View.ashx",
"lametro": "https://metro.legistar.com/View.ashx",
}
WHITELIST = [
"ord.legistar.com",
"chicago.legistar.com",
"metro.legistar1.com",
"metro.legistar.com",
]
sentry_sdk.init(
dsn=SENTRY_DSN, integrations=[FlaskIntegration()], before_send=before_send
)
MISSING_PIN_ERROR = "Could not find image for PIN"
MISSING_DOCUMENT_ERROR = "Could not find document at URL"
MISSING_IMAGE_ERROR = "Image does not exist at"
@app.route("/<pin>.jpg")
def index(pin):
s3_conn = S3Connection(AWS_KEY, AWS_SECRET)
bucket = s3_conn.get_bucket("property-image-cache")
s3_key = Key(bucket)
s3_key.key = "{0}.jpg".format(pin)
if s3_key.exists():
output = BytesIO()
s3_key.get_contents_to_file(output)
else:
image_viewer = (
"https://legacy.cookcountyassessor.com/PropertyImage.aspx?pin={0}"
)
image_url = image_viewer.format(pin)
image = requests.get(image_url)
if "image/jpeg" in image.headers["Content-Type"]:
output = BytesIO(image.content)
s3_key.set_metadata("Content-Type", "image/jpg")
s3_key.set_contents_from_file(output)
s3_key.set_acl("public-read")
else:
capture_message("{0} {1}".format(MISSING_PIN_ERROR, pin))
abort(404)
output.seek(0)
response = make_response(output.read())
response.headers["Content-Type"] = "image/jpg"
return response
@app.route("/<city>/document/")
@cross_origin()
def document(city):
query = urllib.parse.parse_qs(urllib.parse.urlparse(request.url).query)
if not query or not set(query.keys()) == {"document_url", "filename"}:
abort(400)
try:
(document_url,) = query["document_url"]
(filename,) = query["filename"]
except ValueError:
abort(400)
document_url_parsed = urllib.parse.urlparse(document_url)
if document_url_parsed.netloc not in WHITELIST:
abort(400)
document_query = urllib.parse.parse_qs(document_url_parsed.query)
s3_conn = S3Connection(AWS_KEY, AWS_SECRET)
bucket = s3_conn.get_bucket("councilmatic-document-cache")
s3_key = Key(bucket)
s3_key.key = urllib.parse.quote_plus(document_url)
if s3_key.exists():
output = BytesIO()
s3_key.get_contents_to_file(output)
content_type = s3_key.content_type
filename = s3_key.metadata["filename"]
source_url = s3_key.metadata.get("source_url", None)
else:
doc = requests.get(document_url, verify=False)
source_url = doc.url
if doc.status_code == 200:
output = BytesIO(doc.content)
content_type = doc.headers["content-type"]
if doc.headers.get("content-disposition"):
filename = (
doc.headers["content-disposition"]
.rsplit("=", 1)[1]
.replace('"', "")
)
s3_key.set_metadata("content-type", content_type)
s3_key.set_metadata("filename", filename)
s3_key.set_metadata("source_url", source_url)
s3_key.set_contents_from_file(output)
s3_key.set_acl("public-read")
else:
capture_message("{0} {1}".format(MISSING_DOCUMENT_ERROR, document_url))
abort(doc.status_code)
response = make_response(output.getvalue())
response.headers["Content-Type"] = content_type
response.headers["Source-URL"] = urllib.parse.quote(source_url, safe=":/")
if "pdf" not in content_type:
response.headers["Content-Disposition"] = 'attachment;filename="{}"'.format(
filename
)
return response
@app.route("/image/")
def image():
if not request.args.get("key") == IMAGE_SECRET:
abort(401)
try:
image_url = request.args["url"]
except KeyError:
abort(400)
parsed_url = urlparse(image_url)
if not parsed_url.netloc:
abort(400)
remote_file = parsed_url.path.rsplit("/", 1)[-1]
s3_conn = S3Connection(AWS_KEY, AWS_SECRET)
bucket = s3_conn.get_bucket("myreps-image-cache")
s3_key = Key(bucket)
s3_key.key = "{0}/{1}".format(parsed_url.netloc, remote_file)
if s3_key.exists():
output = BytesIO()
s3_key.get_contents_to_file(output)
else:
# For some reason, things hosted on illinois.gov do not have a valid
# SSL cert, at least according to my humble operating system. The
# problem is that it redirects to HTTPS and then the cert is not valid.
# *shrug*
image = requests.get(image_url, verify=False)
if image.status_code == 200 and "image" in image.headers["Content-Type"]:
output = BytesIO(image.content)
s3_key.set_metadata("Content-Type", "image/jpg")
s3_key.set_contents_from_file(output)
s3_key.set_acl("public-read")
else:
capture_message("{0} {1}".format(MISSING_IMAGE_ERROR, image_url))
abort(404)
output.seek(0)
response = make_response(output.read())
response.headers["Content-Type"] = "image/jpg"
return response
@app.route("/test-logging/")
def test_logging():
from uuid import uuid4
base_messages = (
MISSING_PIN_ERROR,
MISSING_DOCUMENT_ERROR,
MISSING_IMAGE_ERROR,
)
for message in base_messages:
capture_message("{0} {1}".format(message, uuid4()))
raise Exception("Exception raised")
if __name__ == "__main__":
import os
port = int(os.environ.get("PORT", 5000))
app.run(host="0.0.0.0", port=port, debug=True)