Skip to content
Create a gist now

Instantly share code, notes, and snippets.

anonymous /notifier.js
Created Jul 24, 2015

var notifier = require('node-notifier');
var path = require('path');
var open = require('open');
var fs = require('fs');
var util = require('./util.js');
var sleep = 10000;
var site = '4chan';
var board = 'g'
var sound = false;
var icons = true;
var threads;
var oldThreads;
util.getThreads(util.getThreadsUrl(site, board), function(newThreads){
threads = newThreads;
oldThreads = newThreads;
});
try {
fs.mkdirSync(path.join(__dirname, 'icons'));
} catch(e) {}
var interval = setInterval(function(){
util.getThreads(util.getThreadsUrl(site, board), function(newThreads){
threads = newThreads;
util.updatedThreads(oldThreads, threads, function(updatedThreads){
util.handleThreads(updatedThreads, function(thread){
util.getLastPost(util.getThreadUrl(site, board, thread.no), function(post){
if (icons) {
var image = util.getImageUrl(post, site, board);
var icon = false;
if (image) icon = path.join(__dirname, 'icons', post.tim + post.ext);
}
util.download(image, icon, function(){
console.log('/' + board + '/ No. ' + post.no + '\n' + (util.getPost(post.com) || ' '))
notifier.notify({
title: '/' + board + '/ No. ' + post.no,
message: util.getPost(post.com) || ' ',
icon: icon,
sound: sound,
wait: true,
open: util.getPostUrl(site, board, thread.no, post.no)
});
});
});
});
});
oldThreads = newThreads;
});
}, sleep);
notifier.on('click', function(notifierObject, options){
open(options.open);
});
{
"name": "notifier",
"version": "1.0.0",
"description": "Czego tutaj szukasz cukiereczku?",
"main": "notifier.js",
"dependencies": {
"node-notifier": "^4.2.3",
"open": "^0.0.5",
"request": "^2.60.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Michał Białek",
"license": "ISC"
}
var request = require('request');
var http = require('http');
var fs = require('fs');
var download = function(url, dest, cb) {
if (url == false) {
cb();
return;
}
var file = fs.createWriteStream(dest);
var request = http.get(url, function(response) {
response.pipe(file);
file.on('finish', function() {
file.close(cb);
});
});
}
var getImageUrl = function(post, site, board){
if (post.tim == undefined || (post.ext != '.jpg' && post.ext != '.png' && post.ext != '.gif')) {
return false;
}
switch(site){
case '8chan':
return 'http://media.8ch.net/' + board + '/thumb/' + post.tim + '.jpg';
case '4chan':
return 'http://i.4cdn.org/' + board + '/' + post.tim + 's' + '.jpg';
}
}
var getThreadsUrl = function(site, board){
switch(site){
case '8chan':
return 'http://8ch.net/' + board + '/threads.json';
case '4chan':
return 'http://a.4cdn.org/' + board + '/threads.json';
}
}
var getThreadUrl = function(site, board, thread){
switch(site){
case '8chan':
return 'http://8ch.net/' + board + '/res/' + thread + '.json';
case '4chan':
return 'http://a.4cdn.org/' + board + '/thread/' + thread + '.json';
}
}
var getPostUrl = function(site, board, thread, post){
switch(site){
case '8chan':
return 'http://8ch.net/' + board + '/res/' + thread + '.html#' + post;
case '4chan':
return 'http://boards.4chan.org/' + board + '/thread/' + thread + '#p' + post;
}
}
var getThreads = function(url, cb, err) {
request({
url: url
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
threads = JSON.parse(body)[0]['threads'];
if (cb) {
cb(threads);
}
}
else {
if (err) {
err(error);
}
}
});
}
var getLastPost = function(url, cb, err) {
request({
url: url
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
post = JSON.parse(body)['posts'].slice(-1)[0];
if (cb) {
cb(post);
}
}
else {
if (err) {
err(error);
}
}
});
}
var updatedThreads = function(oldThreads, threads, cb) {
newThreads = threads.filter(function(thread) {
return oldThreads.some(function(oldThread) {
return (thread.no == oldThread.no && oldThread.last_modified != thread.last_modified) ;
}) + oldThreads.every(function(oldThread) {
return (thread.no != oldThread.no);
});
});
if (cb) {
cb(newThreads);
}
return newThreads;
}
var handleThreads = function(threads, cb) {
threads.forEach(function(thread){
cb(thread);
});
}
var getPost = function(html){
html = html || '';
html = html.replace(/<br>/ig,'\n').replace(/(<([^>]+)>)/ig,'').replace(/&amp;/g, '&').replace(/&quot;/g, '\"').replace(/&gt;/g, '>').replace(/&lt;/g, '<').replace(/&#039;/g, '\'');
return (html);
}
module.exports.getPostUrl = getPostUrl;
module.exports.getImageUrl = getImageUrl;
module.exports.getThreadsUrl = getThreadsUrl;
module.exports.getThreadUrl = getThreadUrl;
module.exports.getThreads = getThreads;
module.exports.getLastPost = getLastPost;
module.exports.updatedThreads = updatedThreads;
module.exports.handleThreads = handleThreads;
module.exports.getPost = getPost;
module.exports.download = download;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.