socialblade-com-api
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -1,2 +0,2 @@ | ||
const validSources = ['twitter', 'instagram'/*, 'facebook', 'youtube' */] | ||
const validSources = ['twitter', 'instagram', 'facebook'/* , 'youtube' */] | ||
@@ -7,2 +7,8 @@ function isValidSource (source) { | ||
function generateUrl (urlPrefix, source, username) { | ||
const userUrl = source === 'facebook' ? 'page' : 'user' | ||
const urlSuffix = source === 'facebook' ? '' : '/monthly' | ||
return `${urlPrefix}https://socialblade.com/${source}/${userUrl}/${username}${urlSuffix}` | ||
} | ||
function cleanRows (rows) { | ||
@@ -33,13 +39,33 @@ const days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] | ||
function convertArrayToObject (arrays) { | ||
function convertArrayToObject (source, arrays) { | ||
return arrays.map(array => { | ||
const [date, followersDelta, followers, followingDelta, following, postsDelta, posts] = array | ||
return { | ||
date: date.replace(/-/g, '/'), | ||
followersDelta: +followersDelta || 0, | ||
followers: +followers || 0, | ||
followingDelta: +followingDelta || 0, | ||
following: +following || 0, | ||
postsDelta: +postsDelta || 0, | ||
posts: +posts || 0 | ||
const [col1, col2, col3, col4, col5, col6, col7] = array | ||
switch (source) { | ||
case 'twitter': | ||
case 'instagram': | ||
return { | ||
date: dateDashToSlash(col1), | ||
followersDelta: +col2 || 0, | ||
followers: +col3 || 0, | ||
followingDelta: +col4 || 0, | ||
following: +col5 || 0, | ||
postsDelta: +col6 || 0, | ||
posts: +col7 || 0 | ||
} | ||
case 'facebook': | ||
return { | ||
date: dateDashToSlash(col1), | ||
likesDelta: +col2 || 0, | ||
likes: +col3 || 0, | ||
talkingAboutDelta: +col4 || 0, | ||
talkingAbout: +col5 || 0 | ||
} | ||
case 'youtube': | ||
return { | ||
date: dateDashToSlash(col1), | ||
subscribersDelta: +(convertUnit(col2)) || 0, | ||
subscribers: +(convertUnit(col3)) || 0, | ||
viewsDelta: +col4 || 0, | ||
views: +col5 || 0 | ||
} | ||
} | ||
@@ -49,4 +75,13 @@ }) | ||
function convertUnit (str) { | ||
return str.replace('K', '000').replace('M', '000000') | ||
} | ||
function dateDashToSlash (str) { | ||
return str.replace(/-/g, '/') | ||
} | ||
module.exports = { | ||
validSources, | ||
generateUrl, | ||
isValidSource, | ||
@@ -53,0 +88,0 @@ cleanRows, |
13
index.js
@@ -9,2 +9,3 @@ const axios = require('axios') | ||
fillArray, | ||
generateUrl, | ||
isValidSource, | ||
@@ -16,6 +17,6 @@ validSources | ||
function callSocialblade (urlPrefix, source, username) { | ||
function callSocialblade (url) { | ||
return axios({ | ||
method: 'GET', | ||
url: `${urlPrefix}https://socialblade.com/${source}/user/${username}/monthly` | ||
url | ||
}) | ||
@@ -29,10 +30,12 @@ } | ||
} | ||
const html = await callSocialblade(urlPrefix, source, username) | ||
const url = generateUrl(urlPrefix, source, username) | ||
const html = await callSocialblade(url) | ||
const $ = cheerio.load(html.data) | ||
const table = $('#socialblade-user-content > div:nth-child(5)').text() | ||
const tableRows = cleanRows(table.split('\n')) | ||
const itemsPerRow = 7 | ||
const itemsPerRowCriteria = { facebook: 5, youtube: 6 } | ||
const itemsPerRow = itemsPerRowCriteria[source] || 7 | ||
let arrays = createArrayOfArrays(tableRows.length / itemsPerRow) | ||
arrays = fillArray(arrays, tableRows, itemsPerRow) | ||
const array2obj = convertArrayToObject(arrays) | ||
const array2obj = convertArrayToObject(source, arrays) | ||
return array2obj | ||
@@ -39,0 +42,0 @@ } catch (err) { |
{ | ||
"name": "socialblade-com-api", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Unofficial APIs for Socialblade.com website.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
const assert = require('assert') | ||
const { cleanRows, createArrayOfArrays, fillArray, convertArrayToObject } = require('../functions') | ||
const { cleanRows, convertArrayToObject, createArrayOfArrays, fillArray, generateUrl } = require('../functions') | ||
@@ -21,2 +21,9 @@ const mockArrayToClean = [ | ||
describe('Tests for socialblade-com-api', () => { | ||
it('should generate the url', () => { | ||
const url1 = generateUrl('prefix', 'twitter', 'barackobama') | ||
const url2 = generateUrl('prefix', 'facebook', 'barackobama') | ||
assert.strictEqual(url1, 'prefixhttps://socialblade.com/twitter/user/barackobama/monthly') | ||
assert.strictEqual(url2, 'prefixhttps://socialblade.com/facebook/page/barackobama') | ||
}) | ||
it('should clean an array from unwanted chars', () => { | ||
@@ -56,3 +63,3 @@ const cleanedArray = cleanRows(mockArrayToClean) | ||
it('should convert an array to object', () => { | ||
const convertedArray = convertArrayToObject(mockFilledArray) | ||
const convertedArray = convertArrayToObject('twitter', mockFilledArray) | ||
assert.strictEqual(convertedArray.length, 3) | ||
@@ -59,0 +66,0 @@ assert.strictEqual(convertedArray[0].date, '2020/04/27') |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12321
229