-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
77 lines (72 loc) · 2.86 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
const puppeteer = require('puppeteer');
const {
getOutput,
cleanRows,
createArrayOfArrays,
convertArrayToObject,
fillArray,
generateUrl,
isValidSource,
validSources,
} = require('./functions');
/**
* Get content from a Socialblade page
* @param {string} url The URL of Socialblade page
* @param {string} [cookieValue] Your Socialblade cookie, needed for Instagram
* @return {Promise<string>} Content of the page
*/
async function callSocialblade(url, cookieValue = '') {
const browser = await puppeteer.launch({ headless: 'new' });
const page = await browser.newPage();
// eslint-disable-next-line max-len
await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36');
const cookie = {
domain: '.socialblade.com',
name: 'PHPSESSXX',
value: cookieValue,
};
await page.setCookie(cookie);
await page.goto(url);
// Youtube has some redirects, we need to go always monthly page
const currentUrl = page.url();
if (!currentUrl.includes('/monthly') && currentUrl.includes('youtube')) {
await page.goto(`${currentUrl}/monthly`);
}
const content = await page.content();
await browser.close();
return content;
}
/**
* Return data from Socialblade
* @param {string} source The social source. Valid inputs: twitter, instagram, facebook, youtube
* @param {string} username The social username
* @param {string} [cookie] Your Socialblade cookie, needed for Instagram
* @return {Promise<{table, charts}>} data from Socialblade
*/
async function socialblade(source, username, cookie = '') {
try {
if (!isValidSource(source)) {
throw Error(`Invalid source. Valid sources are: ${validSources.join(', ')}`);
}
if (source === 'instagram' && !cookie) {
throw Error(`The cookie param is needed for ${source}, see Readme for more info`);
}
const url = generateUrl(source, username);
const html = await callSocialblade(url, cookie);
const { table, charts } = getOutput(html, source);
const { tableRows, chartsRows } = cleanRows(table, charts);
const itemsPerRow = { twitter: 7, instagram: 7, facebook: 5, youtube: 6, tiktok: 9 };
let tableArrays = createArrayOfArrays(tableRows.length / itemsPerRow[source]);
tableArrays = fillArray(tableArrays, tableRows, itemsPerRow[source]);
const tableArrayOfObjects = convertArrayToObject(source, tableArrays);
let chartsArrays = createArrayOfArrays(chartsRows.length / 2);
chartsArrays = fillArray(chartsArrays, chartsRows, 2);
const chartsArrayOfObjects = convertArrayToObject('charts', chartsArrays);
// console.log(tableArrayOfObjects, chartsArrayOfObjects);
return { table: tableArrayOfObjects, charts: chartsArrayOfObjects };
} catch (err) {
console.log(err.message);
}
}
// socialblade('youtube', 'selenagomez', 'asd123');
exports.socialblade = socialblade;