public
Created

irc.js file 2 (forked from wikistream, working to modify).

  • Download Gist
irc2.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
//imports
 
var fs = require('fs'),
path = require('path'),
irc = require('irc-js'),
nodeio = require('node.io'),
//redis = require('redis').createClient();
mongo = require('mongodb'),
db = new mongo.Db('wikis', new mongo.Server('localhost', 27017, {}), {});
 
function listen(config, callback){
channels = [];
for (var channel in config.wikipedias){
channels.push(channel);
}
 
var client = new irc({
server: 'irc.wikimedia.org',
nick: config.ircNick,
log: config.log,
user: {
username: config.ircUserName,
realname: config.ircRealName
}
});
 
client.connect(function(){
client.join(channels);
client.on('privmsg', function(msg){
m = parse_msg(msg.params, config);
if (m){
callback(m);
saveRecs(m);
}
});
});
 
}
 
function parse_msg(msg, config){
//this regex is fairly impenetrable
//need to come back to this and figure out what it's doing
 
var m = /\x0314\[\[\x0307(.+?)\x0314\]\]\x034 (.*?)\x0310.*\x0302(.*?)\x03.+\x0303(.+?)\x03.+\x03 (.*) \x0310(.*)\x03?.*/.exec(msg[1]);
if (! m){
console.log("could not parse: " + msg);
return null;
}
 
//number of characters edited - to int
if (m[5]) {
var delta = parseInt(/([+-]\d+)/.exec(m[5])[1]);
 
} else {
var delta = null;
}
 
//anonymous edit check
var user = m[4];
//if user is any ip address, it's anonymous
var anonymous = user.match(/\d+.\d+.\d+.\d+/) ? true : false;
 
//parsing flags
var flag = m[2];
var isRobot = flag.match(/B/) ? true:false;
var isNewPage = flag.match(/N/) ? true:false;
var isUnpatrolled = flag.match(/!/) ? true:false;
var isMinor = flag.match(/M/) ? true:false;
var page = m[1];
var wikipedia = msg[0];
var wikipediaUrl = 'http://' + wikipedia.replace('#', '') + '.org';
var pageUrl = wikipediaUrl + '/wiki/' + page.replace(/ /g, '_');
var userUrl = wikipediaUrl + '/wiki/User:' + user;
var namespace = getNamespace(wikipedia, page, config);
var vandalContent;
 
//from https://github.com/chriso/node.io
//I get the feeling this is not going to work.
if (m[6].match(/vandal/) && namespace === "article"){
nodeio.scrape(function(){
this.getHtml(m[3], function(err, $){
//console.log('getting HTML, boss.');
console.log(err);
var output = [];
$('span.diffchange.diffchange-inline').each(function(scraped){
output.push(scraped.rawtext);
});
vandalContent = output.toString();
 
});
 
});
} else {
vandalContent = "no content";
}
return {
flag: flag,
page: page,
pageUrl: pageUrl,
url: m[3],
delta: delta,
comment: m[6],
wikipedia: wikipedia,
wikipediaUrl: wikipediaUrl,
wikipediaShort: config.wikipedias[msg[0]].short,
wikipediaLong: config.wikipedias[msg[0]].long,
user: user,
userUrl: userUrl,
unpatrolled: isUnpatrolled,
anonymous: anonymous,
robot: isRobot,
namespace: namespace,
minor: isMinor
//vandalContent: vandalContent
}
}
 
 
function getNamespace(wikipedia, page, config){
ns = null;
var parts = page.split(':');
if (parts.length > 1 && parts[1][0] != " "){
ns = config['wikipedias'][wikipedia]['namespaces'][parts[0]];
if (! ns) ns = "wikipedia";
} else {
ns = 'article';
}
return ns;
}
 
function dbOpenCheck(err, db){
console.log("db is open, boss!");
}
 
db.open(dbOpenCheck);
 
function saveRecs(msg){
saveVandals(msg);
}
 
//mongo business here, to store the data for later use.
function saveVandals(msg){
 
if (msg.comment.match(/vandal/)){
db.collection('wikiCollection', function(err, collection){
doc = {
"page": msg.page,
"url": msg.url,
"user": msg.user,
"comment": msg.comment
};
collection.insert(doc, function(){
console.log('Got a record, boss!');
});
});
}
}
 
 
exports.listen = listen;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.