-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew_script.js
More file actions
141 lines (120 loc) · 5.13 KB
/
new_script.js
File metadata and controls
141 lines (120 loc) · 5.13 KB
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
const newPoliticians = [
{
id: 1,
name: "Nancy Pelosi",
party: "Independent",
state: "New York",
trades: 10,
issuers: 5,
volume: "1M",
lastTraded: "2025-01-15",
image: "./images/nancy_pelosi.jpg" // Add "./" at the beginning
},
// Add more sample politicians here
];
async function fetchNewPoliticians() {
const apiKey = 'cI7iuYn7dlVu1kNAwDnvbVGvHbizUrzPWM5TimpI';
const endpoint = `https://api.congress.gov/v3/member?api_key=${apiKey}`;
try {
const response = await fetch(endpoint);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
console.log(data); // Check the structure of the response
const politicians = data.members.map(politician => ({
id: politician.bioguideID,
registrantId: 'P40014052', // Set all candidate IDs to P40014052
name: politician.name,
party: politician.partyName || 'Unknown',
state: politician.state || 'Unknown',
chamber: politician.terms[0]?.chamber || 'Unknown',
trades: Math.floor(Math.random() * 100), // Fake data
issuers: Math.floor(Math.random() * 50), // Fake data
volume: `${Math.floor(Math.random() * 100)}M`, // Fake data
lastTraded: '2025-01-15', // Fake data
image: politician.depiction?.imageUrl || './images/nancy_pelosi.jpg' // Use API image or placeholder
}));
displayNewPoliticians(politicians);
} catch (error) {
console.error('Error fetching data:', error);
}
}
function displayNewPoliticians(politicians) {
const container = document.getElementById('new-politicians-container');
container.innerHTML = '';
politicians.forEach(politician => {
const card = document.createElement('div');
card.className = 'politician-card';
card.innerHTML = `
<img src="${politician.image}" alt="${politician.name}">
<h3>${politician.name}</h3>
<p>${politician.party} / ${politician.state}</p>
<p>${politician.trades} Trades</p>
<p>${politician.issuers} Issuers</p>
<p>${politician.volume} Volume</p>
<p>Last Traded: ${politician.lastTraded}</p>
`;
card.style.cursor = 'pointer';
card.addEventListener('click', () => {
console.log(`Clicked on ${politician.name} with candidate ID: ${politician.registrantId}`);
fetchLobbyingData(politician.registrantId);
});
container.appendChild(card);
});
}
async function fetchLobbyingData(candidateId) {
const apiKey = '4Hs7xoXStKnuSSpzPPuQgaOBUFmymHnCWeCBYGsK';
const endpoint = `https://api.open.fec.gov/v1/candidate/${candidateId}/totals/?api_key=${apiKey}`;
try {
const response = await fetch(endpoint);
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
console.log('Lobbying Data:', data);
displayLobbyingTable(data.results);
} catch (error) {
console.error('Error fetching lobbying data:', error);
}
}
function displayLobbyingTable(lobbyingData) {
const container = document.getElementById('new-politicians-container');
container.innerHTML = '<h2>Lobbying Data</h2>';
const backButton = document.createElement('button');
backButton.textContent = 'Back to Politicians';
backButton.onclick = fetchNewPoliticians;
container.appendChild(backButton);
const table = document.createElement('table');
table.innerHTML = `
<tr>
<th>Contributor</th>
<th>Amount</th>
</tr>
`;
lobbyingData.forEach(item => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${item.contributor_name || 'N/A'}</td>
<td>${item.total || 'N/A'}</td>
`;
table.appendChild(row);
});
container.appendChild(table);
}
function searchNewPoliticians() {
const nameSearch = document.getElementById('new-politician-search').value.toLowerCase();
const partyFilter = document.getElementById('party-filter').value;
const chamberFilter = document.getElementById('chamber-filter').value;
const stateFilter = document.getElementById('state-filter').value;
const filteredPoliticians = newPoliticians.filter(politician => {
const matchesName = politician.name.toLowerCase().includes(nameSearch);
const matchesParty = partyFilter === 'all' || politician.party === partyFilter;
const matchesChamber = chamberFilter === 'all' || politician.chamber === chamberFilter;
const matchesState = stateFilter === 'all' || politician.state === stateFilter;
return matchesName && matchesParty && matchesChamber && matchesState;
});
displayNewPoliticians(filteredPoliticians);
}
// Call the function to fetch and display politicians
fetchNewPoliticians();