-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
174 lines (141 loc) · 4.77 KB
/
app.js
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
169
170
171
172
173
174
/**
* wpstubs
* Theopolisme <[email protected]>
*/
( function ( require ) {
var changes, client, twitter,
// modules
http = require( 'http' ),
he = require( 'he' ),
wikibot = require( 'nodemw' ),
wikichanges = require( 'wikichanges' ),
Twit = require( 'twit' ),
Mustache = require( 'mustache' ),
// const
CONST = {
linkLength: 22,
templateTitleRegex: /<template[^>]*>\s*<title[^>]*>\s*(.*?)\s*<\/title>/g
},
// config
CONFIG = require( './config.json' );
function getTemplateModel ( name, callback ) {
client.api.call( {
action: 'query',
prop: 'revisions',
rvprop: 'content',
rvgeneratexml: true,
format: 'json',
indexpageids: true,
titles: name
}, function ( data, next, raw ) {
var xml = data.pages[data.pageids[0]].revisions[0].parsetree;
callback && callback( xml );
}, 'POST' );
}
function getTemplateNames ( model ) {
var match, names = [],
titleRegex = CONST.templateTitleRegex;
match = titleRegex.exec( model );
while ( match !== null ) {
names.push( match[1] );
match = titleRegex.exec( model );
}
return names;
}
function getHashtags ( templateNames ) {
var hashtags = [],
i = 0;
function getHashtag ( name ) {
// screw it
if ( !/wikiproject/i.test( name ) ) {
return false;
}
if ( CONFIG.ignorelist.indexOf( name.toLowerCase() ) !== -1 ) {
return false;
}
// Convert html entities to normal characters
name = he.decode( name );
// Remove "WikiProject" from beginning of hashtag
name = name.replace( /^wikiproject/i, '' );
// Hashtags can only contain alphanumeric characters and underscores
name = name.replace( /[^a-z0-9_]/gi, '' );
return name;
}
for ( i; i < templateNames.length; i++ ) {
var hashtag = getHashtag( templateNames[i] );
if ( hashtag ) {
hashtags.push( hashtag );
}
}
return hashtags;
}
function makeTweet ( name, hashtags, url ) {
var template = CONFIG.tweet,
reducedHashtags = hashtags;
// Ensure the tweet is no more than 140 characters
function getLength () {
return Mustache.render( template, {
title: name,
hashtags: reducedHashtags,
link: ''
} ).length + CONST.linkLength;
}
while ( getLength() > 140 ) {
if ( reducedHashtags.length > 1 ) {
reducedHashtags.pop();
} else {
// no hashtags, no tweet. DUN DUN DUNNN
return false;
}
}
return Mustache.render( template, {
title: name,
hashtags: reducedHashtags,
link: url + ( url.indexOf( '?' ) === -1 ? '?' : '&' ) + CONFIG.urlparams
} );
}
function postTweet ( tweet ) {
if ( CONFIG.dry ) {
console.log( 'Would be tweeting: ' + tweet );
} else {
twitter.post( 'statuses/update', { status: tweet }, function ( err, data, response ) {
console.log( data );
} );
}
}
function handlePage ( name, url ) {
if ( CONFIG.debug ) {
console.log( 'handling page "' + name + '"' );
}
// Skip dabs
if ( name.indexOf( '(disambiguation)' ) !== -1 ) {
return;
}
// Strip Talk: stuff... -.-
articleName = he.decode( name.replace( /^Talk:/, '' ) );
articleUrl = url.replace( /Talk:/, '' );
getTemplateModel( name, function ( model ) {
var tweet, hashtags = getHashtags( getTemplateNames( model ) );
if ( CONFIG.debug ) {
console.log( 'hashtags: ' + hashtags );
}
if ( hashtags.length ) {
tweet = makeTweet( articleName, hashtags, articleUrl );
}
if ( tweet ) {
postTweet( tweet );
}
} );
}
twitter = new Twit( CONFIG.twitter );
client = new wikibot( CONFIG.wikipedia, { debug: CONFIG.debug } );
changes = new wikichanges.WikiChanges( {
ircNickname: CONFIG.nick,
wikipedias: [ '#en.wikipedia' ]
} );
changes.listen( function ( change ) {
if ( change.newPage && change.namespace === 'talk' ) {
handlePage( change.page, change.pageUrl );
}
} );
}( require ) );