-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_votes.js
More file actions
120 lines (103 loc) · 3.7 KB
/
Copy pathdata_votes.js
File metadata and controls
120 lines (103 loc) · 3.7 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
const express = require('express');
const axios = require('axios');
const app = express();
const port = 3000;
const API_KEY = '###';
const BILL_LIST_URL = 'https://open.assembly.go.kr/portal/openapi/nwbpacrgavhjryiph';
const VOTE_URL = 'https://open.assembly.go.kr/portal/openapi/nojepdqqaweusdfbi';
let billIdCache = [];
// 모든 BILL_ID 가져오기 (페이지네이션 적용)
async function fetchBillIds() {
if (billIdCache.length > 0) {
console.log("Using cached BILL_IDs:", billIdCache);
return billIdCache;
}
let pIndex = 1;
const billIds = [];
let hasMoreData = true;
console.log("Starting to fetch BILL_IDs...");
try {
while (hasMoreData) {
const response = await axios.get(BILL_LIST_URL, {
params: {
Key: API_KEY,
Type: 'json',
AGE: 22,
pSize: 10, // 페이지당 데이터 개수
pIndex: pIndex // 페이지 인덱스
}
});
const data = response.data;
console.log(`API Response for Bill List (pIndex: ${pIndex}):`, data);
if (data.nwbpacrgavhjryiph && data.nwbpacrgavhjryiph[1].row) {
const rows = data.nwbpacrgavhjryiph[1].row;
for (let row of rows) {
if (row.BILL_ID) {
billIds.push(row.BILL_ID);
}
}
pIndex++; // 다음 페이지로 이동
} else {
console.log("No more rows found in the response for BILL_IDs.");
hasMoreData = false; // 더 이상 데이터가 없으면 반복 종료
}
}
} catch (error) {
console.error("Error fetching bill list:", error.message);
}
billIdCache = billIds;
console.log("Retrieved BILL_IDs:", billIds);
return billIds;
}
// 특정 국회의원의 표결 데이터를 BILL_ID로 가져오기
async function fetchVoteDataForMember(billId, memberName) {
try {
const response = await axios.get(VOTE_URL, {
params: {
Key: API_KEY,
Type: 'json',
BILL_ID: billId,
AGE: 22,
HG_NM: memberName
}
});
const data = response.data;
console.log(`API Response for Vote Data (BILL_ID: ${billId}):`, data);
// API 응답 데이터 구조에 맞춰 조건문 수정
if (
data.nojepdqqaweusdfbi &&
data.nojepdqqaweusdfbi[1] &&
data.nojepdqqaweusdfbi[1].row
) {
return data.nojepdqqaweusdfbi[1].row;
} else {
console.log(`No items found for BILL_ID: ${billId}`);
return [];
}
} catch (error) {
console.error(`Error fetching vote data for BILL_ID ${billId}:`, error.message);
return [];
}
}
// 모든 BILL_ID에 대해 병렬로 표결 데이터 가져오기
async function fetchAllVoteDataForMember(memberName) {
const billIds = await fetchBillIds();
const allVoteData = [];
for (let billId of billIds) {
const voteData = await fetchVoteDataForMember(billId, memberName);
allVoteData.push(...voteData);
}
console.log("Total Vote Data for Member:", allVoteData);
return allVoteData;
}
app.get('/api/vote_data', async (req, res) => {
const memberName = req.query.name;
if (!memberName) {
return res.status(400).json({ error: "Member name is required" });
}
const voteData = await fetchAllVoteDataForMember(memberName);
res.json(voteData);
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});