-
Notifications
You must be signed in to change notification settings - Fork 0
/
LobbyingFirms.py
68 lines (59 loc) · 1.79 KB
/
LobbyingFirms.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
import datetime
import os
from dotenv import load_dotenv
from db_connect import *
from dataclasses import dataclass
@dataclass
class LobbyingFirm:
FilerId: int
FirmName: str
FirmCity: str
FirmState: str
FirmZip: str
FirmAddress: str
DateRegistered: datetime.date
load_dotenv()
connection = db_connect(os.getenv('CALACCESSDB'))
cursor = connection.cursor()
# get all lobbying firms
lobbying_firms = []
cursor.execute("""
SELECT DISTINCT FILER_ID, NAML, CITY, ST, ZIP4,
CONCAT_WS(' ', ADR1, ADR2) AS Address, EFFECT_DT
FROM FILERNAME_CD
WHERE FILER_TYPE = "FIRM"
""")
db_lobbying_firms = cursor.fetchall()
print("Unique Lobbying Firms: ", cursor.rowcount)
for firm in db_lobbying_firms:
lobbying_firms.append(
LobbyingFirm(
FilerId=firm[0],
FirmName=firm[1],
FirmCity=firm[2],
FirmState=firm[3],
FirmZip=firm[4],
FirmAddress=firm[5],
DateRegistered=firm[6]
)
)
cursor.close()
connection.close()
# insert LobbyingFirms into db
connection = db_connect(os.getenv('DEVDB'))
cursor = connection.cursor()
insert_query = """
INSERT IGNORE INTO LobbyingFirms (FilerId, FirmName, FirmCity, FirmState, FirmZip, FirmAddress, DateRegistered)
VALUES (%s, %s, %s, %s, %s, %s, %s)
"""
data_to_insert = [
(firm.FilerId, firm.FirmName, firm.FirmCity, firm.FirmState, firm.FirmZip, firm.FirmAddress, firm.DateRegistered)
for firm in lobbying_firms
]
batch_size = 1000
for i in range(0, len(data_to_insert), batch_size):
batch_data = data_to_insert[i:i + batch_size]
cursor.executemany(insert_query, batch_data)
connection.commit()
cursor.close()
connection.close()