gotsentimental
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -25,2 +25,3 @@ "use strict"; | ||
url: url, | ||
gzip: true, | ||
agent: requestAgent, | ||
@@ -27,0 +28,0 @@ timeout: cfg.twitter.timeout |
{ | ||
"port": "1337", | ||
"mongodb": { | ||
@@ -4,0 +3,0 @@ "uri": "mongodb://localhost/gotsentimental" |
@@ -6,2 +6,3 @@ "use strict"; | ||
// gotsentimental package | ||
const gotsent = require('../../'); | ||
@@ -11,3 +12,6 @@ | ||
// adjust config by passing an object with attributes that should be overwritten | ||
gotsent.cfg.extend(cfg.gotsent); | ||
// initialize the gotsentimental package | ||
gotsent.init(); | ||
@@ -24,4 +28,22 @@ | ||
// register routes | ||
app.get('/', function(req, res) { | ||
res.render("home"); | ||
app.get('/', function(req, resp) { | ||
// get top lists | ||
// those are async funcs to we have to wait until the Promises are resolved. | ||
// but we can do it in parallel and sync them with Promise.all: | ||
var ps = []; | ||
ps[0] = gotsent.mostPopular(); | ||
ps[1] = gotsent.mostHated(); | ||
ps[2] = gotsent.mostDiscussed(); | ||
// wait for all Promises to be resolved | ||
Promise.all(ps).then(function(results) { | ||
// use the results of all 3 Promises | ||
resp.render("top", { | ||
mostPopular: results[0], | ||
mostHated: results[1], | ||
mostDiscussed: results[2] | ||
}); | ||
}) | ||
}); | ||
@@ -28,0 +50,0 @@ app.use('/csv/:slug.csv', ctrData); |
116
index.js
@@ -13,6 +13,22 @@ "use strict"; | ||
/** | ||
* gotsentimental - GoT Twitter Sentiment Analysis | ||
* @exports gotsentimental | ||
*/ | ||
var pkg = module.exports = {}; | ||
/** | ||
* Object containing the package configuration. | ||
* The config can be changed by directly overwriting attributes or using | ||
* cfg.extend(object). | ||
* See defaults.json for default values. | ||
* @type {Object} | ||
* @property {function} extend Merge object into config | ||
*/ | ||
pkg.cfg = cfg; | ||
/** | ||
* Initilaize the package. | ||
* Opens the MongoDB connection and initializes the Twitter client. | ||
*/ | ||
pkg.init = function() { | ||
@@ -24,2 +40,5 @@ db.connect(); | ||
/** | ||
* Close any open resources like the database connection. | ||
*/ | ||
pkg.shutdown = function() { | ||
@@ -29,20 +48,33 @@ db.close(); | ||
/** | ||
* Update data by crawling for new tweets and generating new CSV files. | ||
* @param {boolean} [full=false] full rebuild or incremental update | ||
* @return {Promise<Object>} A promise to the update results | ||
*/ | ||
pkg.update = function(full) { | ||
got.updateCharacters().then(function(res) { | ||
debug.info("Characters updated", res); | ||
return new Promise(function(resolve, reject) { | ||
got.updateCharacters().then(function(res) { | ||
debug.info("Characters updated", res); | ||
mobile.crawlAll(full).then(function(res) { | ||
debug.info("MCRAWL FINISHED: ", res); | ||
}, debug.error); | ||
mobile.crawlAll(full).then(function(res) { | ||
resolve(res); | ||
}, reject); | ||
// twitter.crawlAll().then(function(res) { | ||
// debug.info("ACRAWL FINISHED: ", res); | ||
// }).catch(debug.eror); | ||
// twitter.crawlAll().then(function(res) { | ||
// debug.info("ACRAWL FINISHED: ", res); | ||
// }).catch(debug.eror); | ||
// TODO: Analyze all | ||
}, function(err) { | ||
debug.error("Updating Characters: ", err); | ||
// TODO: Analyze all | ||
}, reject); | ||
}); | ||
}; | ||
/** | ||
* Update data for given character by crawling for new tweets and generating | ||
* new CSV files. | ||
* @param {string} id ID of the character | ||
* @param {boolean} [full=false] full rebuild or incremental update | ||
* @return {Promise<Object>} A promise to the update results | ||
*/ | ||
pkg.updateCharacter = function(id, full) { | ||
@@ -62,2 +94,18 @@ return new Promise(function(resolve, reject) { | ||
/** | ||
* @typedef Character | ||
* @property {string} name name of the character | ||
* @property {string} slug human-readale URL-identifier for the character | ||
* @property {string} _id unique ID | ||
* @property {number} total total number of tweets in database | ||
* @property {number} heat how controverse is the character | ||
* @property {number} popularity how much is the character is discussed | ||
* @property {Date} updated date when the document was last updated | ||
*/ | ||
/** | ||
* Consume a token | ||
* @param {string} id ID of the character | ||
* @return {Promise<Character>} A promise to the token. | ||
*/ | ||
pkg.character = function(id) { | ||
@@ -67,2 +115,7 @@ return Character.byID(id); | ||
/** | ||
* Get the most popular Characters | ||
* @param {number} [n=10] Number of Characters to return | ||
* @return {Promise<Array.<Character>>} A promise to the array of characters | ||
*/ | ||
pkg.mostPopular = function(n) { | ||
@@ -72,5 +125,10 @@ if(n === undefined) { | ||
} | ||
return Character.find().sort({popularity:-1}).limit(n).exec; | ||
return Character.find().sort({popularity:-1}).limit(n).exec(); | ||
}; | ||
/** | ||
* Get the most hated characters. | ||
* @param {number} [n=10] Number of characters to return | ||
* @return {Promise<Array.<Character>>} A promise to the array of characters | ||
*/ | ||
pkg.mostHated = function(n) { | ||
@@ -80,5 +138,10 @@ if(n === undefined) { | ||
} | ||
return Character.find().sort({popularity:1}).limit(n).exec; | ||
return Character.find().sort({popularity:1}).limit(n).exec(); | ||
}; | ||
/** | ||
* Get the most discussed characters. | ||
* @param {number} [n=10] Number of characters to return | ||
* @return {Promise<Array.<Character>>} A promise to the array of characters | ||
*/ | ||
pkg.mostDiscussed = function(n) { | ||
@@ -88,21 +151,18 @@ if(n === undefined) { | ||
} | ||
return Character.find().sort({heat:-1}).limit(n).exec; | ||
return Character.find().sort({heat:-1}).limit(n).exec(); | ||
}; | ||
/** | ||
* The Chart CSS file | ||
* @property {string} path Absolute path to file | ||
* @property {function} serve HTTP handler to serve file | ||
*/ | ||
pkg.css = asset('public/chart.css'); | ||
/** | ||
* The Chart JS file | ||
* @property {string} path Absolute path to file | ||
* @property {function} serve HTTP handler to serve file | ||
*/ | ||
pkg.js = asset('public/chart.js'); | ||
/* | ||
(function test() { | ||
pkg.cfg.extend(require('./config.json')); | ||
pkg.init(); | ||
pkg.updateCharacter("56ea4cfe8c27d7c6375059a1", false).then(function(res) { | ||
debug.log(res); | ||
pkg.shutdown(); | ||
}, function(err) { | ||
debug.error(err); | ||
pkg.shutdown(); | ||
}); | ||
})(); | ||
*/ |
{ | ||
"name": "gotsentimental", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "GoT Twitter Sentiment Analysis", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
200
README.md
@@ -1,5 +0,9 @@ | ||
[![Build Status](https://travis-ci.org/Rostlab/JS16_ProjectD_Group4.svg?branch=develop)](https://travis-ci.org/Rostlab/JS16_ProjectD_Group4) | ||
# GoT Twitter Sentiment Analysis | ||
# gotsentimental [![Build Status](https://travis-ci.org/Rostlab/JS16_ProjectD_Group4.svg?branch=develop)](https://travis-ci.org/Rostlab/JS16_ProjectD_Group4) [![npm](https://img.shields.io/npm/v/gotsentimental.svg)](https://www.npmjs.com/package/gotsentimental) | ||
GoT Twitter Sentiment Analysis | ||
## Installing | ||
```sh | ||
$ npm install gotsentimental --save | ||
``` | ||
Dependencies: | ||
@@ -9,33 +13,187 @@ - recent node.js + npm | ||
Install npm dependencies: | ||
```sh | ||
npm install -g gulp | ||
npm install | ||
``` | ||
## Usage | ||
You need to [create a Twitter API key](https://apps.twitter.com/) for the crawler. | ||
Afterwards adjust `config.json`. You need to [create a Twitter API key](https://apps.twitter.com/) for the crawler. | ||
Example: | ||
```js | ||
const gotsent = require('gotsentimental'); | ||
## Run | ||
Currently the package has two main files: `app.js` (web app) and `crawler.js` for the crawler. | ||
// adjust config | ||
gotsent.cfg.extend({ | ||
"mongodb": { | ||
"uri": "mongodb://example/gotsentimental" | ||
}, | ||
"twitter": { | ||
"access_token": "xxx", | ||
"access_token_secret": "xxx", | ||
"consumer_key": "xxx", | ||
"consumer_secret": "xxx" | ||
} | ||
}); | ||
### WebApp | ||
```sh | ||
node app | ||
// initilize | ||
gotsent.init(); | ||
// update DB - this might take a few hours | ||
gotsent.update().then(function(res) { | ||
// print some update stats | ||
console.log(res); | ||
// get top5 most popular characters | ||
gotsent.mostPopular(5).then(function(res) { | ||
res.forEach(function(character) { | ||
console.log(character.name); | ||
}); | ||
}, console.error); | ||
// gracefully shut down | ||
gotsent.shutdown(); | ||
}, function(err) { | ||
console.error(err); | ||
gotsent.shutdown(); | ||
}); | ||
``` | ||
`/` Will provide an overview with the most popular characters | ||
## API | ||
### Types | ||
`/character-name` shows the graph for a character | ||
#### Character | ||
`/character-name.csv` returns the raw data as CSV | ||
| Name | Type | Description | | ||
| --- | --- | --- | | ||
| name | <code>string</code> | name of the character | | ||
| slug | <code>string</code> | human-readale URL-identifier for the character | | ||
| _id | <code>string</code> | unique ID | | ||
| total | <code>number</code> | total number of tweets in database | | ||
| heat | <code>number</code> | how controverse is the character | | ||
| popularity | <code>number</code> | how much is the character is discussed | | ||
| updated | <code>Date</code> | date when the document was last updated | | ||
### Crawler | ||
```sh | ||
node crawler | ||
``` | ||
### Methods and Attributes | ||
You can set the tasks that the crawler runs in `crawler.js`. | ||
* [.cfg](#gotsentimental.cfg) : <code>Object</code> | ||
* [.cfg.extend(json)](#gotsentimental.cfg.extend) | ||
* [.css](#gotsentimental.css) | ||
* [.js](#gotsentimental.js) | ||
* [.init()](#gotsentimental.init) | ||
* [.shutdown()](#gotsentimental.shutdown) | ||
* [.update([full])](#gotsentimental.update) ⇒ <code>Promise.<Object></code> | ||
* [.updateCharacter(id, [full])](#gotsentimental.updateCharacter) ⇒ <code>Promise.<Object></code> | ||
* [.character(id)](#gotsentimental.character) ⇒ <code>Promise.<Character></code> | ||
* [.mostPopular([n])](#gotsentimental.mostPopular) ⇒ <code>Promise.<Array.<Character>></code> | ||
* [.mostHated([n])](#gotsentimental.mostHated) ⇒ <code>Promise.<Array.<Character>></code> | ||
* [.mostDiscussed([n])](#gotsentimental.mostDiscussed) ⇒ <code>Promise.<Array.<Character>></code> | ||
<a name="gotsentimental.cfg"></a> | ||
#### gotsentimental.cfg : <code>Object</code> | ||
Object containing the package configuration. | ||
The config can be changed by directly overwriting attributes or using | ||
[.cfg.extend(json)](#gotsentimental.cfg.extend). | ||
<a name="gotsentimental.cfg.extend"></a> | ||
#### gotsentimental.cfg.extend(json) | ||
Merges the given Object into the config by overwriting attributes. Arrays are concatenated. | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| json | <code>Object</code> | Config Object | | ||
<a name="gotsentimental.init"></a> | ||
#### gotsentimental.init() | ||
Initilaize the package. | ||
Opens the MongoDB connection and initializes the Twitter client. | ||
<a name="gotsentimental.shutdown"></a> | ||
#### gotsentimental.shutdown() | ||
Close any open resources like the database connection. | ||
<a name="gotsentimental.update"></a> | ||
#### gotsentimental.update([full]) ⇒ <code>Promise.<Object></code> | ||
Update data by crawling for new tweets and generating new CSV files. | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| [full] | <code>boolean</code> | <code>false</code> | full rebuild or incremental update | | ||
**Returns**: <code>Promise.<Object></code> - A promise to the update results. | ||
<a name="gotsentimental.updateCharacter"></a> | ||
#### gotsentimental.updateCharacter(id, [full]) ⇒ <code>Promise.<Object></code> | ||
Update data for given [character](#character) by crawling for new tweets and generating | ||
new CSV files. | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| id | <code>string</code> | | ID of the character | | ||
| [full] | <code>boolean</code> | <code>false</code> | full rebuild or incremental update | | ||
**Returns**: <code>Promise.<Object></code> - A promise to the update results. | ||
<a name="gotsentimental.character"></a> | ||
#### gotsentimental.character(id) ⇒ <code>Promise.<Character></code> | ||
Get a [character](#character) by ID. | ||
**Returns**: <code>Promise.<Character></code> - A promise to the [character](#character). | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| id | <code>string</code> | ID of the character | | ||
<a name="gotsentimental.mostPopular"></a> | ||
#### gotsentimental.mostPopular([n]) ⇒ <code>Promise.<Array.<Character>></code> | ||
Get the most popular [characters](#character). | ||
**Returns**: <code>Promise.<Array.<Character>></code> - A promise to the array of [characters](#character) | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| [n] | <code>number</code> | <code>10</code> | Number of Characters to return | | ||
<a name="gotsentimental.mostHated"></a> | ||
#### gotsentimental.mostHated([n]) ⇒ <code>Promise.<Array.<Character>></code> | ||
Get the most hated [characters](#character). | ||
**Returns**: <code>Promise.<Array.<Character>></code> - A promise to the array of [characters](#character) | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| [n] | <code>number</code> | <code>10</code> | Number of characters to return | | ||
<a name="gotsentimental.mostDiscussed"></a> | ||
#### gotsentimental.mostDiscussed([n]) ⇒ <code>Promise.<Array.<Character>></code> | ||
Get the most discussed [characters](#character). | ||
**Returns**: <code>Promise.<Array.<Character>></code> - A promise to the array of [characters](#character) | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| [n] | <code>number</code> | <code>10</code> | Number of Characters to return | | ||
<a name="gotsentimental.css"></a> | ||
#### gotsentimental.css | ||
The Chart CSS file | ||
| Name | Type | Description | | ||
| --- | --- | --- | | ||
| path | <code>string</code> | Absolute path to file | | ||
| serve | <code>function</code> | HTTP handler to serve file | | ||
<a name="gotsentimental.js"></a> | ||
#### gotsentimental.js | ||
The Chart JS file | ||
| Name | Type | Description | | ||
| --- | --- | --- | | ||
| path | <code>string</code> | Absolute path to file | | ||
| serve | <code>function</code> | HTTP handler to serve file | | ||
## Testing | ||
Install Gulp: | ||
```sh | ||
npm install -g gulp | ||
``` | ||
```sh | ||
npm test | ||
@@ -42,0 +200,0 @@ ``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
247097
2146
209