项目作者: rrwen

项目描述 :
Module for extracting Twitter data using option objects
高级语言: JavaScript
项目地址: git://github.com/rrwen/twitter2return.git
创建时间: 2017-12-11T02:58:55Z
项目社区:https://github.com/rrwen/twitter2return

开源协议:MIT License

下载


twitter2return

Richard Wen
rrwen.dev@gmail.com

Module for extracting Twitter data using option objects

npm version
Build Status
Coverage Status
npm
GitHub license
Donarbox Donate
PayPal Donate
Twitter

Install

  1. Install Node.js
  2. Install twitter2return via npm
  3. Recommended: Install dotenv via npm
  1. npm install --save twitter2return
  2. npm install --save dotenv

For the latest developer version, see Developer Install.

Usage

It is recommended to use a .env file at the root of your project directory with the following contents:

  • Obtain the keys below from https://apps.twitter.com/
  • TWITTER_CONSUMER_KEY: Consumer key (API Key)
  • TWITTER_CONSUMER_SECRET: Consumer secret (API secret)
  • TWITTER_ACCESS_TOKEN_KEY: Access token
  • TWITTER_ACCESS_TOKEN_SECRET: Access token secret
  1. TWITTER_CONSUMER_KEY=***
  2. TWITTER_CONSUMER_SECRET=***
  3. TWITTER_ACCESS_TOKEN_KEY=***
  4. TWITTER_ACCESS_TOKEN_SECRET=***

The .env file above can be loaded using dotenv:

  1. require('dotenv').config();

See Documentation for more details.

REST API

  1. Load .env file variables
  2. Load twitter2return
  3. Create options object
  4. Optionally define Twitter API keys
  5. Search keyword twitter from GET search/tweets
  6. Apply a jsonata filter for statuses key only
  7. Execute twitter2return with the REST API options
  1. require('dotenv').config();
  2. var twitter2return = require('twitter2return');
  3. // (options) Initialize options object
  4. var options = {twitter: {}};
  5. // (options_twitter_rest) Search for keyword 'twitter' in path 'GET search/tweets'
  6. options.twitter.method = 'get'; // get, post, or stream
  7. options.twitter.path = 'search/tweets'; // api path
  8. options.twitter.params = {q: 'twitter'}; // query tweets
  9. // (options_jsonata) Filter for statuses array using jsonata
  10. options.jsonata = 'statuses';
  11. // (twitter2return_rest) Query tweets using REST API
  12. twitter2return(options)
  13. .then(data => {
  14. console.log(data);
  15. }).catch(err => {
  16. console.error(err.message);
  17. });

Stream API

  1. Load .env file variables
  2. Load twitter2return
  3. Create options object
  4. Optionally define Twitter API keys
  5. Track keyword twitter from POST statuses/filter
  6. Log the tweets when they are received
  7. Execute twitter2return with the Stream API options
  1. require('dotenv').config();
  2. var twitter2return = require('twitter2return');
  3. // (options) Initialize options object
  4. var options = {twitter: {}};
  5. // (options_twitter_connection) Track keyword 'twitter' in path 'POST statuses/filter'
  6. options.twitter.method = 'stream'; // get, post, or stream
  7. options.twitter.path = 'statuses/filter'; // api path
  8. options.twitter.params = {track: 'twitter'}; // query tweets
  9. // (options_twitter_stream) Log the tweets when received
  10. options.twitter.stream = function(err, data) {
  11. if (err) {console.error(err)};
  12. console.log(data.twitter.tweets);
  13. };
  14. // (twitter2return_stream) Stream tweets
  15. var stream = twitter2return(options);
  16. stream.on('error', function(error) {
  17. console.error(error.message);
  18. });

Contributions

  1. Reports for issues and suggestions can be made using the issue submission interface.
  2. Code contributions are submitted via pull requests

See CONTRIBUTING.md for more details.

Developer Notes

Developer Install

Install the latest developer version with npm from github:

  1. npm install git+https://github.com/rrwen/twitter2return

Install from git cloned source:

  1. Ensure git is installed
  2. Clone into current path
  3. Install via npm
  1. git clone https://github.com/rrwen/twitter2return
  2. cd twitter2return
  3. npm install

Tests

  1. Clone into current path git clone https://github.com/rrwen/twitter2return
  2. Enter into folder cd twitter2return
  3. Ensure devDependencies are installed and available
  4. Run tests with a .env file (see tests/README.md)
  5. Results are saved to tests/log with each file corresponding to a version tested
  1. npm install
  2. npm test

Documentation

Use documentationjs to generate html documentation in the docs folder:

  1. npm run docs

See JSDoc style for formatting syntax.

Upload to Github

  1. Ensure git is installed
  2. Inside the twitter2return folder, add all files and commit changes
  3. Push to github
  1. git add .
  2. git commit -a -m "Generic update"
  3. git push

Upload to npm

  1. Update the version in package.json
  2. Run tests and check for OK status (see tests/README.md)
  3. Generate documentation
  4. Login to npm
  5. Publish to npm
  1. npm test
  2. npm run docs
  3. npm login
  4. npm publish

Implementation

The module twitter2return uses the following npm packages for its implementation:

npm Purpose
twitter Connections to the Twitter API REST and Streaming Application Programming Interfaces (APIs)
jsonata Query language to filter Twitter JSON data
  1. twitter <-- Extract Twitter data from API
  2. |
  3. jsonata <-- Filter Twitter JSON data