-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (79 loc) · 2.61 KB
/
index.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
const fs = require("fs")
const path = require("path")
const puppeteer = require("puppeteer")
const onlyLetters = (str) => {
let regExp = /[^a-z]/g
let letters = str.toLowerCase().replace(regExp, "")
return letters
}
;(async () => {
const playlistsFile = fs.readFileSync(
path.resolve(__dirname, "playlists.txt")
)
const playlistsLines = playlistsFile.toString().split(/\n/gim)
const getData = async () =>
new Promise(async (resolve, reject) => {
const playlists = playlistsLines.map((playlist) => {
let info = playlist.split(" --- ")
return {
name: info[0],
link: info[1]
}
})
const infos = []
const browser = await puppeteer.launch({
// headless: false // uncomment if you want to see Chrome in action
})
const page = await browser.newPage()
await page.setViewport({
width: 1024,
height: 768,
deviceScaleFactor: 1
})
for (let index = 0; index < playlists.length; index++) {
const { name, link } = playlists[index]
const screenShotFile = `./screenshots/${onlyLetters(name)}.png`
try {
console.log("Going to:", link)
await page.goto(link, {
waitUntil: ["networkidle2"]
})
console.log("Saving screenshot file:", screenShotFile)
await page.screenshot({ path: screenShotFile })
const { description, owner, ownerProfile } = await page.evaluate(
() => {
const { body } = document
// Note: this selector of course will change
const owner = body.querySelector(".RANLXG3qKB61Bh33I0r2")
// Note: this selector of course will change - too
return {
description: body.querySelector(".fUYMR7LuRXv0KJWFvRZA")
.textContent,
owner: owner.textContent,
ownerProfile: `https://open.spotify.com${owner
.querySelector("a")
.getAttribute("href")}`
}
}
)
infos.push({
name,
link,
description,
owner,
ownerProfile,
screenShotFile
})
console.log("Information obtained:", infos[index])
} catch (error) {
console.error('ERROR: ', error)
}
}
await browser.close()
resolve(infos)
})
const playlistsInfos = await getData()
console.log('Saving playlists.json file...')
fs.writeFileSync("./playlists.json", JSON.stringify(playlistsInfos))
console.log("Done.")
})()