yashxx07 commited on
Commit
ce39e5b
1 Parent(s): e759895

Update emails.js

Browse files
Files changed (1) hide show
  1. emails.js +62 -14
emails.js CHANGED
@@ -9,26 +9,69 @@ const pool = new Pool(config);
9
 
10
  // ***** update daily top 3 news
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  const updateEmailNews = async (req, res) => {
13
  try {
14
-
15
  const catList = ["science", "india", 'business',
16
  "sports", "entertainment", "technology", "world"];
17
 
18
- catList.forEach(async (cat) => {
19
-
20
  const data = await getTopNews(cat);
21
 
22
- data.forEach(async (d) => {
23
-
24
- const summary = await getSummary(d.article_url);
25
-
26
- //set news in database
27
  try {
 
 
28
  const query = `
29
- update top_news set
30
- title = $1, article_url = $2, source = $3, summary = $4, published_date = $5 where category = '${cat}_${data.indexOf(d) + 1}' ;
31
- `;
 
32
  const values = [
33
  d.title,
34
  d.article_url,
@@ -41,20 +84,25 @@ const updateEmailNews = async (req, res) => {
41
  } catch (er) {
42
  console.log(er);
43
  }
44
-
45
  });
46
 
 
 
47
  });
48
 
49
- res.send({ "status": "running" });
 
50
 
 
 
51
 
52
  } catch (e) {
53
  console.log(e);
 
54
  }
55
-
56
  }
57
 
 
58
  async function getTopNews(category) {
59
  const query = `
60
  select title, article_url, category, source_name, published_date from news where category like '%${category}%' order by published_date desc limit 3;
 
9
 
10
  // ***** update daily top 3 news
11
 
12
+ // const updateEmailNews = async (req, res) => {
13
+ // try {
14
+
15
+ // const catList = ["science", "india", 'business',
16
+ // "sports", "entertainment", "technology", "world"];
17
+
18
+ // catList.forEach(async (cat) => {
19
+
20
+ // const data = await getTopNews(cat);
21
+
22
+ // data.forEach(async (d) => {
23
+
24
+ // const summary = await getSummary(d.article_url);
25
+
26
+ // //set news in database
27
+ // try {
28
+ // const query = `
29
+ // update top_news set
30
+ // title = $1, article_url = $2, source = $3, summary = $4, published_date = $5 where category = '${cat}_${data.indexOf(d) + 1}' ;
31
+ // `;
32
+ // const values = [
33
+ // d.title,
34
+ // d.article_url,
35
+ // d.source_name,
36
+ // summary,
37
+ // d.published_date
38
+ // ];
39
+
40
+ // await pool.query(query, values);
41
+ // } catch (er) {
42
+ // console.log(er);
43
+ // }
44
+
45
+ // });
46
+
47
+ // });
48
+
49
+ // res.send({ "status": "running" });
50
+
51
+
52
+ // } catch (e) {
53
+ // console.log(e);
54
+ // }
55
+
56
+ // }
57
+
58
  const updateEmailNews = async (req, res) => {
59
  try {
 
60
  const catList = ["science", "india", 'business',
61
  "sports", "entertainment", "technology", "world"];
62
 
63
+ const promises = catList.map(async (cat) => {
 
64
  const data = await getTopNews(cat);
65
 
66
+ const articlePromises = data.map(async (d) => {
 
 
 
 
67
  try {
68
+ const summary = await getSummary(d.article_url);
69
+
70
  const query = `
71
+ UPDATE top_news SET
72
+ title = $1, article_url = $2, source = $3, summary = $4, published_date = $5
73
+ WHERE category = '${cat}_${data.indexOf(d) + 1}';
74
+ `;
75
  const values = [
76
  d.title,
77
  d.article_url,
 
84
  } catch (er) {
85
  console.log(er);
86
  }
 
87
  });
88
 
89
+
90
+ await Promise.all(articlePromises);
91
  });
92
 
93
+
94
+ await Promise.all(promises);
95
 
96
+
97
+ res.send({ "status": "completed" });
98
 
99
  } catch (e) {
100
  console.log(e);
101
+ res.status(500).send({ "error": "An error occurred" });
102
  }
 
103
  }
104
 
105
+
106
  async function getTopNews(category) {
107
  const query = `
108
  select title, article_url, category, source_name, published_date from news where category like '%${category}%' order by published_date desc limit 3;