-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
154 lines (140 loc) · 4.3 KB
/
main.js
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import * as fs from "fs";
import { initializeApp } from "firebase/app";
import { getFirestore, setDoc, doc } from "firebase/firestore";
import { getAuth, signInWithEmailAndPassword, signOut } from "firebase/auth";
const firebaseConfig = {
apiKey: "AIzaSyBq3iPo1JMfgqwvJpwK-5qDI6sK16-c-Dk",
authDomain: "fcnews-33a3b.firebaseapp.com",
projectId: "fcnews-33a3b",
storageBucket: "fcnews-33a3b.appspot.com",
messagingSenderId: "1039974117962",
appId: "1:1039974117962:web:c1cc72c85bbd7e214f6cb3",
measurementId: "G-4YX1ERJ3NE",
};
const headlinesId = "headlineNewsId";
const businessId = "businessNewsId";
const technologyId = "technologyNewsId";
const scienceId = "scienceNewsId";
const entertainmentId = "entertainmentNewsId";
const healthId = "healthNewsId";
const SportsId = "SportsNewsId";
const newsColecctions = [
{
category: "Headlines",
colecctiondb: "Headlines",
url: `https://newsapi.org/v2/top-headlines?country=us&apiKey=16c97751c4254e3eb593aa7a7975a3b2`,
id: headlinesId,
},
{
category: "Business",
colecctiondb: "Business",
url: `https://newsapi.org/v2/top-headlines?country=us&category=Business&apiKey=16c97751c4254e3eb593aa7a7975a3b2`,
id: businessId,
},
{
category: "Technology",
colecctiondb: "Technology",
url: `https://newsapi.org/v2/top-headlines?country=us&category=Technology&apiKey=16c97751c4254e3eb593aa7a7975a3b2`,
id: technologyId,
},
{
category: "Science",
colecctiondb: "Science",
url: `https://newsapi.org/v2/top-headlines?country=us&category=Science&apiKey=16c97751c4254e3eb593aa7a7975a3b2`,
id: scienceId,
},
{
category: "Entertainment",
colecctiondb: "Entertainment",
url: `https://newsapi.org/v2/top-headlines?country=us&category=Entertainment&apiKey=16c97751c4254e3eb593aa7a7975a3b2`,
id: entertainmentId,
},
{
category: "Health",
colecctiondb: "Health",
url: `https://newsapi.org/v2/top-headlines?country=us&category=Health&apiKey=16c97751c4254e3eb593aa7a7975a3b2`,
id: healthId,
},
{
category: "Sports",
colecctiondb: "Sports",
url: `https://newsapi.org/v2/top-headlines?country=us&category=Sports&apiKey=16c97751c4254e3eb593aa7a7975a3b2`,
id: SportsId,
},
];
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const db = getFirestore(app);
//Sing-in to FIrebase
const singIn = async () => {
try {
await signInWithEmailAndPassword(auth, "fjch100@yahoo.com", "123456");
// console.log('userCredential', userCredential);
// console.log("current user:", auth?.currentUser?.email);
} catch (err) {
console.log(err);
}
};
//Sign-out to Firebase
const signoutHandler = async () => {
await signOut(auth)
.then()
.catch((err) => console.log(err));
};
//function to log events to file
const logtofile = (logText, addDate = true) => {
let fecha = new Date().toString();
let texto;
if (addDate) {
texto = fecha + logText + "\n";
} else {
texto = logText + "\n";
}
fs.appendFile("./fcnews_log.txt", texto, (err) => {
if (err) {
console.error(err);
}
});
};
// function to update a firebase collection
const updateDataDb = async (Colection, elementId, data) => {
try {
await setDoc(doc(db, Colection, elementId), data);
logtofile(`Updated Category: ${Colection} ... OK`, false);
} catch (err) {
console.log(err);
}
};
//function to fetch news from API
const getNews = async (url, id) => {
try {
const data = await fetch(url);
const apiData = await data.json();
const news = {
_id: id,
articles: apiData.articles,
updatedAt: new Date(),
};
return news;
} catch (err) {
console.error(`Something went wrong trying to find one document: ${err}\n`);
}
};
// Main function to run and orchestration the Bot
async function run() {
logtofile("******************* Updating news **********************", false);
logtofile(" ", true);
await singIn();
for (let index = 0; index < newsColecctions.length; index++) {
const item = newsColecctions[index];
const news = await getNews(item.url, item.id);
// console.log(news);
await updateDataDb(item.colecctiondb, item.id, news);
}
await signoutHandler();
console.log("DONE !");
logtofile("------ Done ! -------", false);
}
// run Main fuction
run().catch(console.dir);