news-ai-app / news.js
yashxx07's picture
Update news.js
e759895 verified
raw
history blame
5.76 kB
//require('dotenv').config()
let Parser = require('rss-parser');
let parser = new Parser();
const { Pool } = require('pg');
var parse = require('pg-connection-string').parse;
var config = parse(process.env.DATABASE_URL);
const pool = new Pool(config);
const rssList = [
{
"https://search.cnbc.com/rs/search/combinedcms/view.xml?partnerId=wrss01&id=100003114": ["top", "https://www.cnbc.com", "cnbc.com"],
"https://search.cnbc.com/rs/search/combinedcms/view.xml?partnerId=wrss01&id=100727362": ["world", "https://www.cnbc.com", "cnbc.com"],
"https://search.cnbc.com/rs/search/combinedcms/view.xml?partnerId=wrss01&id=10001147": ["business", "https://www.cnbc.com", "cnbc.com"],
"https://search.cnbc.com/rs/search/combinedcms/view.xml?partnerId=wrss01&id=10001054": ["science", "https://www.cnbc.com", "cnbc.com"],
"https://search.cnbc.com/rs/search/combinedcms/view.xml?partnerId=wrss01&id=19854910": ["technology", "https://www.cnbc.com", "cnbc.com"],
"http://timesofindia.indiatimes.com/rssfeeds/1081479906.cms": ["entertainment", "https://timesofindia.indiatimes.com/", "timesofindia.indiatimes.com"],
"https://timesofindia.indiatimes.com/rssfeedstopstories.cms": ["top", "https://timesofindia.indiatimes.com/", "timesofindia.indiatimes.com"],
"https://timesofindia.indiatimes.com/rssfeeds/296589292.cms": ["world", "https://timesofindia.indiatimes.com/", "timesofindia.indiatimes.com"],
"https://timesofindia.indiatimes.com/rssfeeds/1898055.cms": ["business", "https://timesofindia.indiatimes.com/", "timesofindia.indiatimes.com"],
"https://timesofindia.indiatimes.com/rssfeeds/-2128672765.cms": ["science", "https://timesofindia.indiatimes.com/", "timesofindia.indiatimes.com"],
"https://timesofindia.indiatimes.com/rssfeeds/4719148.cms": ["sports", "https://timesofindia.indiatimes.com/", "timesofindia.indiatimes.com"],
"https://timesofindia.indiatimes.com/rssfeeds/66949542.cms": ["technology", "https://timesofindia.indiatimes.com/", "timesofindia.indiatimes.com"],
"http://timesofindia.indiatimes.com/rssfeeds/1081479906.cms": ["entertainment", "https://timesofindia.indiatimes.com/", "timesofindia.indiatimes.com"],
"http://timesofindia.indiatimes.com/rssfeeds/-2128936835.cms": ["india", "https://timesofindia.indiatimes.com/", "timesofindia.indiatimes.com"],
},
{
"https://feeds.abcnews.com/abcnews/topstories": ["top", "https://abcnews.go.com/", "abcnews.go.com"],
"https://feeds.abcnews.com/abcnews/internationalheadlines": ["world", "https://abcnews.go.com/", "abcnews.go.com"],
"https://feeds.abcnews.com/abcnews/moneyheadlines": ["business", "https://abcnews.go.com/", "abcnews.go.com"],
"https://feeds.abcnews.com/abcnews/technologyheadlines": ["technology", "https://abcnews.go.com/", "abcnews.go.com"],
"https://feeds.abcnews.com/abcnews/entertainmentheadlines": ["entertainment", "https://abcnews.go.com/", "abcnews.go.com"],
"https://feeds.abcnews.com/abcnews/sportsheadlines": ["sports", "https://abcnews.go.com/", "abcnews.go.com"],
"https://moxie.foxnews.com/google-publisher/latest.xml": ["top", "https://www.foxnews.com/", "foxnews.com"],
"https://moxie.foxnews.com/google-publisher/science.xml": ["science", "https://www.foxnews.com/", "foxnews.com"],
"https://moxie.foxnews.com/google-publisher/sports.xml": ["sports", "https://www.foxnews.com/", "foxnews.com"],
"https://moxie.foxnews.com/google-publisher/tech.xml": ["technology", "https://www.foxnews.com/", "foxnews.com"],
"https://moxie.foxnews.com/google-publisher/world.xml": ["world", "https://www.foxnews.com/", "foxnews.com"],
"http://www.moneycontrol.com/rss/MCtopnews.xml": ["india business", "http://www.moneycontrol.com", "moneycontrol.com"],
"https://www.livemint.com/rss/sports": ["india sports", "https://www.livemint.com", "livemint.com"],
}
];
const getNews = async (req, res) => {
try {
const id = req.query.id;
//console.log(id,rssList[id]);
for (const url in rssList[id]) {
const data = [];
let feed = await parser.parseURL(url);
for (let i = 0; i < 6; i++) {
data.push(feed.items[i]);
}
//console.log(data, rssList[id][url][0], rssList[id][url][1], rssList[id][url][2]);
await uploadNewsOnDatabase(data, rssList[id][url][0], rssList[id][url][1], rssList[id][url][2]);
}
res.send({ STATUS: "SUCCESS" });
} catch (e) {
return e;
}
}
function convertToISO8601(dateString) {
return new Date(dateString).toISOString();
}
const uploadNewsOnDatabase = async (data, category, newsSource, newsSourceName) => {
try {
data.forEach(async (item) => {
try {
const query = `
INSERT INTO news (id, title, source, article_url, image_url, published_date, category, source_name)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
`;
const values = [
item.title.replace(/[^a-zA-Z0-9]/g, '') ,
item.title,
newsSource,
item.link,
item.enclosure ? item.enclosure.url : null,
convertToISO8601(item.pubDate),
category,
newsSourceName
];
await pool.query(query, values);
} catch (e) {
// console.log(e);
}
});
} catch (error) {
console.error("Error fetching data:", error);
}
}
module.exports = { getNews };