Last active
May 29, 2017 12:29
-
-
Save Hikari9/e469f82feb8b6605a89ebc3d94153eb6 to your computer and use it in GitHub Desktop.
Motivation Ball, scrapes from InspirationalShit.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/node | |
var fs = require('fs'); | |
var os = require('os'); | |
var path = require('path'); | |
var request = require('request'); | |
var cheerio = require('cheerio'); | |
var exec = require('child_process').exec; | |
var icon = '/home/rico/.local/share/icons/hicolor/Chibi-PNG-File.png'; | |
/** | |
* Shows a quote via notify-osd | |
* @param {string} quote the content of the quote | |
* @param {string} cite the speaker of the quote | |
* @return {undefined} | |
*/ | |
function showQuote(quote, cite) { | |
quote = quote.trim(); | |
cite = cite.trim(); | |
var command = 'notify-send -i ' | |
+ icon + ' ' | |
+ '"' | |
+ 'Motivation ball' | |
+ '" "' | |
+ '\n\n\\"' | |
+ quote | |
+ '\\"' | |
+ ' \n\n\\-' | |
+ cite | |
+ '"'; | |
exec('killall notify-osd', function() { | |
exec(command, function(error, stdout, stderr) { | |
if (stdout && stdout.length > 0) console.log(stdout); | |
if (stderr && stderr.length > 0) console.error(stderr); | |
}); | |
}); | |
}; | |
/** | |
* Scrapes a quote from inspirationalshit.com and performs a | |
* callback function after scraping. | |
* @param {Function} callback the callback function to call | |
* after scraping; note that the | |
* arguments of the function must | |
* be F(quote, cite) | |
* @return {undefined} | |
*/ | |
function scrapeQuote(callback) { | |
var url = 'http://inspirationalshit.com/quotes'; | |
request(url, function(error, response, html) { | |
if (error) console.error("error", error); | |
else { | |
var $ = cheerio.load(html); | |
var quote = $('blockquote p').text().trim(); | |
var cite = $('blockquote cite').text().trim(); | |
if (quote.length > 140) { | |
scrapeQuote(callback); | |
} else if (callback && typeof callback === "function") { | |
callback(quote, cite); | |
} | |
} | |
}) | |
}; | |
function buildCache(dir, size) { | |
if (!fs.existsSync(dir)) { | |
console.log("created cache directory " + dir); | |
fs.mkdir(dir, function(err) { | |
if (err) console.error(err); | |
}); | |
} | |
var cacheFile = path.join(dir, 'quotes.txt'); | |
fs.writeFile(cacheFile, '', function(err) { | |
if (err) return console.error(err); | |
for (var i = 0; i < size; ++i) { | |
(function(index) { | |
scrapeQuote(function(quote, cite) { | |
if (index == 0) showQuote(quote, cite); | |
fs.appendFileSync(cacheFile, quote + '>>>' + cite + '\n'); | |
console.log('added quote #' + (index + 1)); | |
}); | |
})(i); | |
} | |
}); | |
}; | |
function popQuoteFromCache(cacheSize) { | |
if (!cacheSize) cacheSize = 50; | |
var dir = path.join(os.homedir(), '.cache/motivation-ball'); | |
var cache = path.join(dir, 'quotes.txt'); | |
if (!fs.existsSync(dir) || !fs.existsSync(cache)) | |
// nothing exists yet, build cache | |
buildCache(dir, cacheSize); | |
else { | |
// pop lines from cache, get last two entries | |
exec('tail -n 1 ' + cache, function(err, stdout, stderr) { | |
if (err) return console.error(err); | |
// now, stdout holds the quote and the citation info | |
lines = stdout.split('>>>'); | |
if (lines.length != 2) { | |
// rebuild cache | |
buildCache(dir, cacheSize); | |
} else { | |
// show quote before truncating | |
showQuote(lines[0], lines[1]); | |
// pop lines from cache file | |
fs.stat(cache, function(err, stats) { | |
if (err) return console.error(err); | |
fs.truncateSync(cache, stats.size - stdout.length, function(err) { | |
if (err) return console.error(err); | |
console.log('popped quote'); | |
}); | |
}); | |
} | |
}); | |
} | |
}; | |
popQuoteFromCache(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment