|
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(/&/g, '&').replace(/"/g, '\"').replace(/>/g, '>').replace(/</g, '<').replace(/'/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; |