awwwards-best-of
Version:
Find the top rated Site Of The Day winners
198 lines (160 loc) • 5.95 kB
JavaScript
#! /usr/bin/env node
var cheerio = require('cheerio');
var open = require('open');
var colors = require('colors');
var request = require('requestretry');
var abbrev = require('abbrev');
console.time('Script processed in'.red);
console.log("Accessing Awwwards...".cyan);
const WINNERS = "https://www.awwwards.com/awards-of-the-day/";
const BASE = "https://www.awwwards.com";
const SITESPERPAGE = 24;
class App {
constructor() {
this.sites = [];
this.abbrevs = abbrev("design", "usability", "content", "creativity");
this.options = {
'design': 'design',
'creativity': 'creativity',
'content': 'content',
'usability': 'usability'
}
this.options.default = this.getRandom(this.options);
this.getArguments();
this.getUrl();
}
getRandom(obj) {
var result;
var count = 0;
for (var prop in obj)
if (Math.random() < 1/++count)
result = prop;
return result;
}
getArguments() {
if (process.argv[3]) {
for (var i = 2; i < process.argv.length; i++) {
var inputInt = Number(process.argv[i].replace(/[^0-9]/g, ''));
var inputAbbr = process.argv[i].replace(/[^a-zA-Z]/g, '');
var inputChar = this.abbrevs[inputAbbr];
if (!isNaN(inputInt) && inputInt > 0) {
this.scanAmount = inputInt;
}
if (this.options[inputChar]) {
this.sortBy = this.options[inputChar];
}
}
this.sortBy = this.sortBy ? this.sortBy : this.options['default'];
this.scanAmount = this.scanAmount ? this.scanAmount : 7;
} else {
this.scanAmount = process.argv[2] || '7';
this.scanAmount = this.scanAmount.replace(/[^0-9]/g, '');
this.scanAmount = this.scanAmount == '' ? 7 : this.scanAmount;
var inputAbbr = process.argv[2] ? process.argv[2].replace(/[^a-zA-Z]/g, '') : 'default';
var inputChar = this.abbrevs[inputAbbr];
this.sortBy = this.options[inputChar] || this.options['default'];
}
this.pages = Math.ceil(this.scanAmount / SITESPERPAGE);
}
getUrl() {
for (var i = 1; i <= this.pages; i++) {
this.queryString = "?page=" + i;
this.url = WINNERS + this.queryString;
this.getAllSites(i);
}
}
getAllSites(pageNumber) {
var options = {
url: this.url,
maxAttempts: 100,
retryDelay: 1000
}
request(options, (error, response, body) => {
if (error) {
console.log('error requesting url: '.yellow + this.url);
console.log('error:'.red, error);
}
var $ = cheerio.load(body);
this.setAmount = pageNumber * SITESPERPAGE - this.scanAmount;
if (this.setAmount >= 0) {
this.setAmount = SITESPERPAGE - this.setAmount;
this.siteBlocks = $('li:nth-child(-n +' + this.setAmount + ')', '.list-items').find('.rollover');
} else {
this.siteBlocks = $('li', '.list-items').find('.rollover');
}
this.siteLinks = $('.rollover > a', this.siteBlocks);
this.getLinks();
});
}
getLinks() {
this.links = [];
for (var i = 0; i < this.siteLinks.length; i++) {
this.links.push(this.siteLinks[i].attribs.href);
}
for (var i = 0; i < this.links.length; i++) {
this.scrapeEach(this.links[i]);
}
}
scrapeEach(path) {
var options = {
url: BASE + path,
maxAttempts: 100,
retryDelay: 1000
}
request(options, (error, response, body) => {
if (error) {
console.log('error requesting url: '.yellow + BASE + path);
console.log('error:'.red, error);
}
var $ = cheerio.load(body);
this.site = {
title: $('a', '.heading-large').html(),
design: $('.circle', '.box-notesite').eq(0).attr('data-note'),
usability: $('.circle', '.box-notesite').eq(1).attr('data-note'),
creativity: $('.circle', '.box-notesite').eq(2).attr('data-note'),
content: $('.circle', '.box-notesite').eq(3).attr('data-note'),
link: BASE + path
};
this.sites.push(this.site);
console.log(this.sites.length + ' ' + this.site.title);
if (this.sites.length == this.scanAmount) {
this.printResults();
}
});
}
printResults() {
this.sites.sort((a, b) => {
return b[this.sortBy] - a[this.sortBy];
});
this.topScores = [];
for (var i = 0; i < this.sites.length; i++) {
if (this.sites[i][this.sortBy] == this.sites[0][this.sortBy]) {
this.topScores.push(this.sites[i]);
} else {
break;
}
}
if (this.topScores.length > 1) {
console.log(' ');
console.log(this.topScores.length + ' websites received the highest evaluation in '.cyan + this.sortBy.cyan + ' out of the last '.cyan + this.scanAmount.toString().cyan + ' Site of the Day winners.'.cyan);
for (var i = 0; i < this.topScores.length; i++) {
console.log('Title: '.underline.green + this.topScores[i].title.underline.cyan);
console.log(this.sortBy.charAt(0).toUpperCase().underline.green + this.sortBy.slice(1).underline.green + ': '.underline.green + this.topScores[i][this.sortBy].underline.green);
}
} else {
console.log(' ');
console.log(this.sites[0].title.cyan + ' recieved the highest evaluation in '.cyan + this.sortBy.cyan + ' out of the last '.cyan + this.scanAmount.toString().cyan + ' Site of the Day winners.'.cyan);
console.log(' ');
console.log('Title: '.underline.green + this.sites[0].title.underline.green);
console.log(this.sortBy.charAt(0).toUpperCase().underline.green + this.sortBy.slice(1).underline.green + ': '.underline.green + this.sites[0][this.sortBy].underline.green);
console.log(' ');
console.log('Opening page in 10s...'.bold.yellow);
console.log(' ');
}
console.timeEnd('Script processed in'.red);
setTimeout(() => {
open(this.sites[0].link);
}, 10000);
}
}
new App();