Skip to content

Instantly share code, notes, and snippets.

@spinscale
Created January 11, 2016 09:02
Show Gist options
  • Save spinscale/f8ec58e9c2d5053a4127 to your computer and use it in GitHub Desktop.
Save spinscale/f8ec58e9c2d5053a4127 to your computer and use it in GitHub Desktop.
Presentation counter
var fs = require('fs')
var _ = require('lodash')
var request = require('request')
var cheerio = require('cheerio')
var ProgressBar = require('progress');
var printf = require('printf');
var presentations = []
var data = fs.readFileSync('urls.txt', { encoding: 'UTF-8' }).split("\n")
var lines = _.filter(data, function(line) { return line.indexOf("http") > -1 });
var bar = new ProgressBar('Checked [:bar] :percent', {
complete: '=',
incomplete: ' ',
width: 20,
total: lines.length
});
lines.forEach(function(url, index){
request(url, function (error, response, html) {
bar.tick()
if (!error && response.statusCode == 200) {
var $ = cheerio.load(html);
if (url.indexOf('slideshare.net') > -1) {
var title = $('h1.notranslate.slideshow-title-text span').text().replace(/\n/g, '')
var views = $('div.format-views span.notranslate').clone().children().remove().end().text().replace(/\n/g, '').replace('Views', '').trim().replace(',', '')
} else if (url.indexOf('speakerdeck.com') > -1) {
var title = $('#talk-details header h1').text().replace(/\n/g, '')
var views = $('#talk li.views span').text().replace(/\n/g, '').replace('Views', '').trim().replace(',', '')
}
add({title: title, views: parseInt(views)})
}
});
})
function add(presentation) {
presentations.push(presentation)
if (presentations.length == lines.length) {
_.each(_.take(_.sortByOrder(presentations, 'views', 'desc'), 5), function(p, i) { console.log(printf("%2d. %5d %s", (i+1), p.views, p.title)) })
var totalViews = _.sum(presentations, 'views')
console.log("\nTotal views: " + totalViews)
}
}
{
"name": "count-presentation-views",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cheerio": "^0.19.0",
"line-reader": "^0.3.0",
"lodash": "^3.10.1",
"printf": "^0.2.3",
"progress": "^1.1.8",
"request": "^2.67.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment