Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
hdddhdd committed Nov 12, 2023
2 parents 3a9460c + 33460fc commit a82ef0e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
27 changes: 15 additions & 12 deletions api/findroad/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ module.exports = async function (context, req) {

if (response.ok) {
const json_response = await response.json();
console.log(JSON.stringify(json_response, null, 2));
// console.log(JSON.stringify(json_response, null, 2));

let itineraryText = '';
var text_counter = 0;

for (const itinerary of json_response['metaData']["plan"]["itineraries"]) {
const legs = itinerary.legs || [];
Expand All @@ -40,33 +41,35 @@ module.exports = async function (context, req) {
if ("Lane" in leg) {
const lanes = leg["Lane"];

console.log("Lanes:", lanes);
// console.log("Lanes:", lanes);

for (let i = 0; i < lanes.length; i++) {
const route = lanes[i].route || `No route information for Lane ${i}`;
if (route.startsWith('SRT') || route.startsWith('KTX') || route.startsWith('ITX') || route.startsWith('무궁화')) {
if (route.startsWith('KTX')) {
const departure_station = leg["start"]["name"];
const arrival_station = leg["end"]["name"];

console.log(`야삐 Departure Station: ${departure_station}`);
console.log(`야삐 Arrival Station: ${arrival_station}`);
console.log(`야삐 Route: ${route}`);

itineraryText += `Departure Station: ${departure_station}\n`;
itineraryText += `Arrival Station: ${arrival_station}\n`;
itineraryText += `Route: ${route}\n\n`;

// console.log(`야삐 Route: ${route}`);
if (text_counter == 0) {
itineraryText += `{"dep": ${departure_station},`;
itineraryText += `"arr": ${arrival_station},}`;
// itineraryText += `"Route": '${route}',}`;

text_counter += 1;
}
}
}
}
}
}

context.res.body = { itineraryText };
console.log(itineraryText)
context.res.body = JSON.parse(itineraryText);

context.res = {
status: 200,
body: { itineraryText }, // 직접 context.res.body에 itineraryText를 넣어줌
body: JSON.parse(itineraryText), // 직접 context.res.body에 itineraryText를 넣어줌
headers: {
'Content-Type': 'application/json'
}
Expand Down
18 changes: 9 additions & 9 deletions api/ktx_schedule/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ const axios = require('axios');

module.exports = async function (context, req) {

const dep = "동대구";
const arr = "서울";
const date = "20231112";
const time = "120000";
const {dep, arr, date, time} = req.body;

const call_command = `python3 ./ktx_schedule/ktx_api.py ${dep} ${arr} ${date} ${time}`;
const call_command = `python ./ktx_schedule/ktx_api.py ${dep} ${arr} ${date} ${time}`;
// console.log(call_command);

const execSync = require('child_process').execSync;
Expand All @@ -24,8 +21,11 @@ module.exports = async function (context, req) {
console.error('JSON 파싱 오류:', error.message);
}

context.res.json({
// status: 200, /* Defaults to 200 */
res: jsonData
});
context.res = {
status: 200,
body: jsonData,
headers: {
'Content-Type': 'application/json'
}
};
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"node-fetch": "^2.7.0",
"swa": "^0.0.1"
},
"type":"module"
"type": "module"
}
5 changes: 4 additions & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ <h1>Vanilla JavaScript App</h1>


<script>
/*
(async function () {
const { res } = await (await fetch(`/api/ktx_schedule`)).json();
Expand All @@ -222,6 +223,7 @@ <h1>Vanilla JavaScript App</h1>
document.querySelector('#ktx_result').textContent = res_text;
}());
*/
</script>


Expand Down Expand Up @@ -256,8 +258,9 @@ <h1>Vanilla JavaScript App</h1>
}),
})
).json();

console.log(text.body);
document.querySelector('#findroad').textContent = text;

} catch (error) {
console.error('주소 변환 및 API 호출에 실패하였습니다:', error);
}
Expand Down

0 comments on commit a82ef0e

Please sign in to comment.