-
Notifications
You must be signed in to change notification settings - Fork 1
/
collect.js
97 lines (90 loc) · 2.5 KB
/
collect.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
// collect.js
const axios = require("axios");
const r = require("rethinkdb");
const { getRethinkDB } = require("./reql.js");
// Get articles from dev.to
const getArticles = async function () {
let articles = [];
let page = 1;
while (true) {
let articles_page = await axios.get(
"https://dev.to/api/articles/me?page=" + page,
{
headers: {
"api-key": process.env.API_KEY,
},
}
);
articles.push(...articles_page.data);
// If a page is not full we are done
if (articles_page.data.length < 30) {
break;
}
}
return articles;
};
// Save article stats to RethinkDB
const saveStats = async function () {
const now = new Date();
let day = ("0" + now.getDate()).slice(-2);
let month = ("0" + (now.getMonth() + 1)).slice(-2);
let year = now.getFullYear();
const today = year + "-" + month + "-" + day;
console.log("Collect stats", today);
// Get all articles and extract stats
const articles = await getArticles();
// Save stats
let conn = await getRethinkDB();
articles.forEach(async (article) => {
let db_article = await r.table("articles").get(article.id).run(conn);
if (!db_article) {
// save article
await r
.table("articles")
.insert({
id: article.id,
title: article.title,
url: article.url,
latest_stats: today,
})
.run(conn);
// save stats
await r
.table("stats")
.insert({
article_id: article.id,
date: today,
comments: article.comments_count,
reactions: article.public_reactions_count,
views: article.page_views_count,
})
.run(conn);
} else if (db_article.latest_stats < today) {
// update article
await r
.table("articles")
.get(article.id)
.update({ latest_stats: today })
.run(conn);
// save stats
await r
.table("stats")
.insert({
article_id: article.id,
date: today,
comments: article.comments_count,
reactions: article.public_reactions_count,
views: article.page_views_count,
})
.run(conn);
} else {
console.log("Already got stats for article " + article.id);
}
});
};
// Run once immediately
saveStats();
// Interval should be less than 24h. Running more than once a day
// is not a problem but a missed day cannot be recovered.
const interval = 6 * 60 * 60 * 1000;
setInterval(saveStats, interval);