-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
100 lines (86 loc) · 3.72 KB
/
app.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
const puppeteer = require('puppeteer');
const {Client} = require("@notionhq/client");
const dotenv = require("dotenv");
// ENV CONFIG
dotenv.config();
// GLOBALES
const notion = new Client({auth:process.env.NOTION_KEY}),
database_id = process.env.NOTION_DATABASE_ID;
// FUNCTIONS
const findModification = async url => {
// Get notes from awwards & from Notion (already saved)
const [freshData, oldData] = await Promise.all([getAwwwards(url), getNotion()]);
// Make the diff (from name property -> unique in awwwards)
const newdata = freshData.filter(data => oldData.indexOf(data.name) === -1);
// Post data to Notion
for (const [key,value] of Object.entries(newdata)){
await notion.request({
path:'pages',
method:"POST",
body:{
"parent": { "database_id": database_id},
"properties": {
"Voter":[{ "text": {"content" :value.name}}],
"Status": {"name": value.status},
"Country": [{ "text": {"content": value.country !== value.name ? value.country : ' '}}],
"Website": value.website || ' ' ,
"Vote": parseFloat(value.note),
"Design": parseInt(value.design,10),
"Usability": parseInt(value.usability,10),
"Creativity": parseInt(value.creativity,10),
"Content": parseInt(value.content,10),
}
}
})
}
if(newdata.length > 0) {
console.log(`${newdata.length} new votes recorded.`);
}
};
// Get URL function
const getUrl = async () => {
//get data (title must be the awwwards link)
const response = await notion.databases.retrieve({ database_id: database_id });
return response.title[0].href;
};
// Get Notion saved data (filtered -> Name)
const getNotion = async () => {
const response = await notion.databases.query({database_id: database_id});
return response.results.map(data => data.properties.Voter.title[0].plain_text);
};
// Get data from awwwards
const getAwwwards = async url => {
// Start browser (https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#running-puppeteer-on-heroku)
const browser = await puppeteer.launch({args: ['--no-sandbox']}); //args:['--no-sandbox'] -> for Heroku
const page = await browser.newPage();
await page.goto(url);
// Wait for vote display
await page.waitForSelector("#user_votes");
// Scrap data from Awwwards
const votesContent = await page.$$eval("#user_votes > li", els => {
return els.map((el) => {
return {
name: el.querySelector(".info > .rows > .row a").textContent,
status: el.querySelector(".list-number-awards .tooltip-text").textContent,
website: el.querySelector(".info > .rows .row:last-child a").textContent,
country: el.querySelector(".info > .rows .row strong:last-child").textContent,
note: el.querySelector(".note").textContent,
design: el.querySelector(".list-circle-notes > .design").dataset.note,
usability: el.querySelector(".list-circle-notes > .usability").dataset.note,
creativity: el.querySelector(".list-circle-notes > .creativity").dataset.note,
content: el.querySelector(".list-circle-notes > .content").dataset.note,
}
})
});
// Close and return scrapped results
await browser.close();
return votesContent;
};
// Main Function
const main = async () => {
const awardsUrl = await getUrl().catch(console.error);
await findModification(awardsUrl).catch(console.error);
}
module.exports = {
main: main
}