-
Notifications
You must be signed in to change notification settings - Fork 1
/
call.py
177 lines (133 loc) · 4.47 KB
/
call.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
import collections
import csv
import datetime
import sqlite3
import requests
import yaml
from flask import Flask, abort, g, redirect, render_template, request, url_for
DATABASE = "database.sqlite3"
app = Flask(__name__)
# https://flask.palletsprojects.com/en/2.3.x/patterns/sqlite3/
def get_db():
db = getattr(g, "_database", None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
db.row_factory = sqlite3.Row
return db
@app.teardown_appcontext
def close_connection(_):
db = getattr(g, "_database", None)
if db is not None:
db.close()
def query_db(query, args=(), one=False):
cur = get_db().execute(query, args)
rv = cur.fetchall()
cur.close()
return (rv[0] if rv else None) if one else rv
def init_db():
with app.app_context():
db = get_db()
with app.open_resource("schema.sql", mode="r") as f:
db.cursor().executescript(f.read())
db.commit()
with open("campaigns.yml") as f:
campaigns = yaml.safe_load(f)
for key, campaign in campaigns.items():
with open(campaign["members_file"]) as f:
members = list(csv.DictReader(f))
members_dict = collections.defaultdict(list)
for mem in members:
members_dict[mem["ocdid"]].append(mem)
campaign["members"] = members_dict
def find_ocdids(lat, lng):
result = requests.get(
"https://districts.socialism.gay/v1/match.json",
params={"lat": str(lat), "lon": str(lng)},
)
print(result.text)
return result.json()["ocdids"]
@app.route("/call/")
@app.route("/call/<c>")
@app.route("/call/<c>/")
def do_call(c="contact"):
if c not in campaigns:
abort(404)
if "lat" in request.args and "lng" in request.args:
lat = request.args["lat"]
lng = request.args["lng"]
ocdids = find_ocdids(float(lat), float(lng))
if not ocdids:
return redirect(url_for("not_found"))
return redirect(
url_for(
"show_representatives",
key=c,
ocdids=",".join(ocdids),
)
)
return render_template("call.html", campaign=campaigns[c], campaigns=campaigns)
@app.route("/call/not_found/")
def not_found():
return render_template("not_found.html")
@app.route("/call/<key>/reps/")
def show_representatives(key):
campaign = campaigns[key]
seen_districts = set()
reps = campaign["reps"]
result = []
ocdids = request.args.get("ocdids", "").split(",")
query_db(
"INSERT INTO visits (timestamp, ip, source, campaign, ocdids) "
"VALUES (?, ?, ?, ?, ?)",
(
datetime.datetime.now().isoformat(),
request.headers.get("X-Forwarded-For", request.remote_addr),
request.cookies.get("src", ""),
key,
",".join(ocdids),
),
)
get_db().commit()
for rep in reps:
if rep["ocdid"] == "theirs":
if "ocdid_prefix" in rep:
dists = [i for i in ocdids if i.startswith(rep["ocdid_prefix"])]
else:
dists = ocdids
else:
dists = [rep["ocdid"]]
for dist in dists:
if dist in seen_districts:
continue
seen_districts.add(dist)
for member in campaign["members"][dist]:
rep_note = None
if rep["rep_note"]:
for note in member["note"].split("|"):
if note in campaign["notes"]:
rep_note = campaign["notes"][note]
note = rep.get("note")
member["clean_phone"] = "".join(
x for x in member["phone"] if x.isnumeric()
)
result.append((rep, member, note, rep_note))
return render_template(
"legislators.html", key=key, campaign=campaign, result=result
)
@app.route("/call/click/")
def click():
query_db(
"INSERT INTO clicks (timestamp, ip, source, campaign, tag, ocdid) VALUES (?, ?, ?, ?, ?, ?)",
(
datetime.datetime.now().isoformat(),
request.headers.get("X-Forwarded-For", request.remote_addr),
request.cookies.get("src", ""),
request.args.get("key"),
request.args.get("tag"),
request.args.get("ocdid"),
),
)
get_db().commit()
return "OK"
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=False, port=8899)