Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/ 구글 관심경향 차트 및 구글 뉴스 크롤링 api 구현완료 #36

Merged
merged 4 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,49 @@ var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");
const axios = require('axios');
const cheerio = require('cheerio');
const axios = require("axios");
const cheerio = require("cheerio");

// 라우터 추가

const {wsdata}=require("./src/utils/WSPrice");
const { wsdata } = require("./src/utils/WSPrice");
var indexRouter = require("./src/routes/index");
var companyRouter = require("./src/routes/company");
var keywordRouter = require("./src/routes/keyword"); //연관검색어 router
var googleRouter = require("./src/routes/google");
var stockInfoRouter = require("./src/routes/stock.info.detail");
var googleNewsRouter = require("./src/routes/google-news");

const db = require("./src/models/DB");
const http=require('http');
const http = require("http");
var app = express();
app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
const server = http.createServer(app);
const WebSocket = require('ws');
const WebSocket = require("ws");
const wss = new WebSocket.Server({ server });
wss.on('connection', async function connection(ws) {
console.log('새로운 WebSocket 클라이언트가 연결되었습니다.');
wss.on("connection", async function connection(ws) {
console.log("새로운 WebSocket 클라이언트가 연결되었습니다.");
let id;
ws.on('message', (message) => {
ws.on("message", (message) => {
const data = JSON.parse(message);
console.log('Received data:', data);
console.log("Received data:", data);
id = data.id;
if (id) {
wsdata(ws, id); // id가 설정된 후에 wsdata 호출
wsdata(ws, id); // id가 설정된 후에 wsdata 호출
} else {
console.error('ID is undefined');
console.error("ID is undefined");
}
});
});

ws.on('close', function close() {
console.log('WebSocket 연결이 종료되었습니다.');
ws.on("close", function close() {
console.log("WebSocket 연결이 종료되었습니다.");
});
});
server.listen(3002, () => {
console.log('서버가 3002번 포트에서 실행 중입니다.');
console.log("서버가 3002번 포트에서 실행 중입니다.");
});

db.sequelize
Expand All @@ -65,9 +66,10 @@ db.sequelize
// 라우터 url 설정
app.use("/api", indexRouter);
app.use("/api/company", companyRouter);
app.use("/api/keyword",keywordRouter);
app.use("/api/keyword", keywordRouter);
app.use("/api/trends", googleRouter);
app.use("/api/stockInfo", stockInfoRouter);
app.use("/api/news/google", googleNewsRouter);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"pg": "^8.12.0",
"pg-hstore": "^2.3.4",
"python-shell": "^5.0.0",
"rss-parser": "^3.13.0",
"sequelize": "^6.37.3",
"ws": "^8.17.1"
}
Expand Down
42 changes: 42 additions & 0 deletions src/routes/google-news.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const express = require("express");
const router = express.Router();
const Parser = require("rss-parser");
const parser = new Parser();

router.get("/", async (req, res) => {
try {
console.log(req.query.keyword);
const keyword = req.query.keyword;
const feed = await parser.parseURL(
`https://news.google.com/rss/search?q=${encodeURIComponent(
keyword
)}&hl=ko&gl=KR&ceid=KR:ko`
);

console.log(`Title: ${feed.title}`);
console.log(`Link: ${feed.link}`);
console.log(`Description: ${feed.description}`);

const newsItems = feed.items.map((item) => {
const titleParts = item.title.split(" - ");
const title = titleParts[0];
const source = titleParts[titleParts.length - 1] || "Unknown";

return {
title,
link: item.link,
pubDate: new Date(item.pubDate),
contentSnippet: item.contentSnippet,
source,
};
});

newsItems.sort((a, b) => b.pubDate - a.pubDate);
res.json(newsItems);
} catch (err) {
console.error("Error fetching RSS feed:", err);
res.status(500).send("Error fetching RSS feed");
}
});

module.exports = router;
4 changes: 2 additions & 2 deletions src/routes/google.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ const googleTrends = require("google-trends-api");
router.get("/", async (req, res) => {
try {
const keyword = req.query.keyword;
console.log("키워드:", keyword);
const start = req.query.startTime;

// 현재 날짜 기준으로 30일 전부터 데이터 가져오기
const startDate = new Date();
startDate.setDate(startDate.getDate() - 28);
startDate.setDate(startDate.getDate() - Number(start) + 2);
startDate.setHours(0, 0, 0, 0);
console.log("날짜:", startDate);

Expand Down