2ch
Version:
A JavaScript library for comfortable 2ch watching.
144 lines (114 loc) • 3.9 kB
Markdown
A JavaScript library for comfortable 2ch watching.
Install the module with: `npm install 2ch --save`
```javascript
var watcher = new require('2ch').ThreadWatcher('iPhone', /パズル&ドラゴンズ/);
watcher.on('update', function(messages) {
// do something.
});
watcher.start();
```
```javascript
new ThreadWatcher(bbsName, query, interval [, bbsMenu ])
```
#### bbsName
A string which is a valid board name in 2ch.
#### query
A string or RegExp which is used to find the watched thread.
#### interval (default: 600000)
A number determining the update interval. It is at least 5000.
#### bbsMenu (default: null)
A BbsMenu instance object. In most cases, there is no need to specify.
### Methods
The ThreadWatcher class inherited from [EventEmitter2](https://github.com/hij1nx/EventEmitter2).
So you can use EventEmitter2 methods basically.
#### start
Start watching 2ch.
#### stop
Stop watching 2ch.
#### isWaching
Return `true`, when waching is started. Return `false`, otherwise.
### Events
#### update
The `update` event is emitted, when the watched thread has new messages.
The callback take the `messages` argument.
```javascript
watcher.on('update', function(messages) {
messages.forEach(function(message) {
console.log(
message.number,
message.postedAt.format('YYYY/MM/DD HH:mm:ss'), // moment object.
message.name,
message.mail,
message.tripId,
message.body
);
});
});
```
The `error` event is emitted, when a error occured in the library.
The callback take the `error` argument.
If there is no listener for it, then the default action is to print a stack trace and exit the program.
The `reload` event is emitted, when the watched thread is reloaded for deleting a message.
The callback take the `title` argument.
The `begin` event is emitted, when the watched thread is found.
The callback take the `title` argument.
The `end` event is emitted, when the watched thread is ended.
The callback take the `title` argument.
The `notfound` event is emitted, when the watched thread is not found by specified query.
```javascript
var ThreadWatcher = require('2ch').ThreadWatcher,
watcher = new ThreadWatcher('番組ch(NHK)', /NHK総合を常に実況し続けるスレ/, 5000),
loaded = false;
watcher.on('update', function(messages) {
if (!loaded) {
loaded = true;
return;
}
messages.forEach(function(message) {
console.log(
message.number,
message.name.replace(/<[^<>]+>/g, ''),
message.postedAt.format('YYYY/MM/DD HH:mm:ss'), // moment object.
message.tripId,
message.body.replace(/<[^<>]+>/g, '')
);
});
});
watcher.on('error', function(error) {
console.log(error);
watcher.stop();
});
watcher.on('reload', function(title) {
console.log(title, 'のスレッドが再読込されました');
loaded = false;
});
watcher.on('begin', function(title) {
console.log(title, 'のスレッドが開始しました');
});
watcher.on('end', function(title) {
console.log(title, 'のスレッドが終了しました');
});
watcher.start();
```
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [grunt](https://github.com/gruntjs/grunt).
* Browserify
* 2013-11-20 v0.1.3 Fix deleted messages bug.
* 2013-09-14 v0.1.2 Remove cheerio.
* 2013-06-19 v0.1.1 Modified it.
* 2013-06-17 v0.1.0 First release.
Copyright (c) 2013 Shogo Iwano
Licensed under the MIT license.