Skip to content

Instantly share code, notes, and snippets.

@straxico
Created March 16, 2020 22:36
Show Gist options
  • Save straxico/ebbaaaee1889003151b31217837dd0d7 to your computer and use it in GitHub Desktop.
Save straxico/ebbaaaee1889003151b31217837dd0d7 to your computer and use it in GitHub Desktop.
const request = require('request')
const cheerio = require('cheerio')
function alexaStats (domain) {
return new Promise((resolve, reject) => {
request({
url: `http://www.alexa.com/siteinfo/${domain}`,
headers: {
Host: 'www.alexa.com',
Referer: `http://www.alexa.com/siteinfo/${domain}`,
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
Pragma: 'no-cache',
Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.8,es;q=0.6'
}
}, (error, response, body) => {
if (error) {
return reject(error)
}
const data = {
domain:domain,
rankData: '',
visitorPercentage: '',
competitorsJSON: '',
topKeywordsJSON: '',
countryRank: [],
}
if (!body) {
return resolve(data)
}
const $ = cheerio.load(body)
//
data.topKeywordsJSON=JSON.parse ($('script#topKeywordsJSON').text())
data.competitorsJSON=JSON.parse ($('script#competitorsJSON').text())
data.visitorPercentage=JSON.parse ($('script#visitorPercentage').text())
data.rankData=JSON.parse ($('script#rankData').text())
$('#countrydropdown li').each((i,x)=> {
const rank=$(x).attr('data-value') || null
const contry=$(x)[0].children[0].data || null
if(rank && contry){
data.countryRank.push({rank,contry})
}
}
)
resolve(data)
})
})
}
module.exports = alexaStats
/////////for use
// const alexaStats = require('./index')
// const domain = 'jajiga.com'
// alexaStats(domain)
// .then(data => {
// console.log(data)
// })
// .catch(error => {
// console.error(error)
// })
////////output
// {
// domain: 'jajiga.com',
// rankData: {
// '3mrank': {
// '20191216': '24187',
// '20191217': '24332',
// '20191218': '24414',
// '20191219': '24565',
// '20191220': '24714',
// '20191221': '25114',
// '20191222': '25112',
// '20191223': '25111',
// '20191224': '25067',
// '20191225': '25016',
// '20191226': '24998',
// '20191227': '24950',
// '20191228': '25059',
// '20191229': '25095',
// '20191230': '25188',
// '20191231': '25258',
// '20200101': '25330',
// '20200102': '25353',
// '20200103': '25464',
// '20200104': '25451',
// '20200105': '25639',
// '20200106': '25775',
// '20200107': '25950',
// '20200108': '26401',
// '20200109': '26459',
// '20200110': '26558',
// '20200111': '26623',
// '20200112': '26774',
// '20200113': '27682',
// '20200114': '27938',
// '20200115': '28179',
// '20200116': '28057',
// '20200117': '28101',
// '20200118': '27873',
// '20200119': '28148',
// '20200120': '28226',
// '20200121': '28291',
// '20200122': '28432',
// '20200123': '28676',
// '20200124': '28690',
// '20200125': '28693',
// '20200126': '28380',
// '20200127': '28264',
// '20200128': '28257',
// '20200129': '28199',
// '20200130': '28269',
// '20200131': '28437',
// '20200201': '28404',
// '20200202': '28590',
// '20200203': '28852',
// '20200204': '29043',
// '20200205': '29369',
// '20200206': '29145',
// '20200207': '29367',
// '20200208': '28959',
// '20200209': '28745',
// '20200210': '29008',
// '20200211': '29069',
// '20200212': '29350',
// '20200213': '29356',
// '20200214': '29272',
// '20200215': '29047',
// '20200216': '29234',
// '20200217': '28941',
// '20200218': '29030',
// '20200219': '29976',
// '20200220': '30526',
// '20200221': '31419',
// '20200222': '31110',
// '20200223': '31148',
// '20200224': '31264',
// '20200225': '31295',
// '20200226': '31410',
// '20200227': '31809',
// '20200228': '32037',
// '20200229': '32048',
// '20200301': '32339',
// '20200302': '32545',
// '20200303': '32639',
// '20200304': '32740',
// '20200305': '32636',
// '20200306': '32976',
// '20200307': '33045',
// '20200308': '33027',
// '20200309': '33007',
// '20200310': '32959',
// '20200311': '32998',
// '20200312': '33053',
// '20200313': '33427',
// '20200314': '33845'
// }
// },
// visitorPercentage: [
// {
// pageviews_per_user: '12',
// code: 'IR',
// visitors_percent: '90.1',
// name: 'Iran',
// pageviews_percent: '99.0'
// },
// {
// pageviews_per_user: '1',
// code: 'US',
// visitors_percent: '5.5',
// name: 'United States',
// pageviews_percent: '0.6'
// }
// ],
// competitorsJSON: {
// site: 'jajiga.com',
// competitors: [ 'vilajar.com', 'shab.ir', 'mizboon.com', 'amlakbashi.com' ]
// },
// topKeywordsJSON: {
// titles: [ 'keyword', 'metric_one', 'metric_two' ],
// 'jajiga.com': [ [Array], [Array], [Array], [Array], [Array] ],
// 'vilajar.com': [ [Array], [Array], [Array], [Array], [Array] ],
// 'shab.ir': [ [Array], [Array], [Array], [Array], [Array] ],
// 'mizboon.com': [ [Array], [Array], [Array], [Array], [Array] ],
// 'amlakbashi.com': [ [Array], [Array], [Array], [Array], [Array] ]
// },
// countryRank: [
// { rank: '1,264', contry: '🇮🇷 Iran ' },
// { rank: '170,384', contry: '🇺🇸 United States ' }
// ]
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment