npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

twtcst

v2.0.0

Published

Twitter Broadcast

Downloads

5

Readme

Twitter Broadcast

NPM version Dependencies Status devDependencies Status

It’s a nodejs module provide you easy interface to get the stream of tweets.

Article about this in Russian in my blog.

Installation

$ npm install twtcst

Examples

var twtcst = require("twtcst");

// array of words you want to track
var words = ["javascript", "coffeescript"];

// object contains your oauth tokens
var oauth = {
  "consumer_key": "",
  "consumer_secret": "",
  "token": "",
  "token_secret": ""
};

var twitter = twtcst(words, oauth);

var validate = twitter.validate();
var beautify = twitter.beautify();

// twitter stream api
twitter.filter(validate, beautify, function(error, tweet) {
  console.log(tweet);
});

API

twtcst has two parameters. The first is words you want to search and the second is your oauth tokens.

var twitter = twtcst(words, oauth);

The twitter object has two methods: search and filter.

search implement Twitter Search API. It takes a three arguments: validate function, beautify function and a callback. Functions validate and beautify will be described below. The callback will be caused when all tweets are found. The first argument of callback is error (if it has occured) and the second is array of tweets.

twitter.search(validate, beautify, function(error, tweets) {
  if (tweets) {
    tweets.forEach(function(tweet) {
      console.log(tweet);
    });
  }
});

filter implement Twitter Streaming API. It has three arguments: validate, beautify and callback. Filter cause callback and passed new tweet to it every time new tweet appears in Twitter Stream. The filter pass to callback two options: error and tweet.

Warning: you can open only one stream per account! If you open the second stream, the first stream disconnects.

twitter.filter(validate, beautify, function(error, tweet) {
  if (tweet) {
    console.log(tweet);
  }
});

Format of tweets depends on the beautify function.

Words

words is array contains words you want to search. It also can contains hashtags started with hash, e.g.:

var words = [
  "#javascript",
  "#coffeescript"
];

OAuth tokens

oauth is an object in format required by Twitter API. You can get tokens by creating new app or from existing app.

var oauth = {
  "consumer_key": "",
  "consumer_secret": "",
  "token": "",
  "token_secret": ""
};

Validate

validate is a function to filter your tweets. Pass it an array of functions you want to filter your tweets.

var validate = twitter.validate([
  twitter.allowLangs(['en', 'ru']),
  twitter.blockUsers(['simonenko', 'isquariel']),
  twitter.blockWords(['test', 'word', 'array', '#php']),
  twitter.noRetweets(),
  twitter.noMentions(),
  twitter.noDefaults(),
  twitter.noQuoted(),
  twitter.maxHashtags(5),
  yourOwnFilter
]);

If tweet doesn’t match any of checks you define, it won’t pass on. Each of these functions takes a tweet as an argument and return true if tweet is valid and false otherwise. E.g.:

var noRetweets = function() {
  return function(tweet) {
    if (tweet.retweeted === true || tweet.text.indexOf('RT ') === 0) {
      return false;
    }

    return true;
  };
};

All tweets is in format served by Twitter: description.

There are some built-in functions to filter

blockUsers filter tweets posted by users you pass to the function. To don’t show tweets from users @simonenko and @isquariel just exec:

var validate = twitter.validate([
  twitter.blockUsers(['simonenko', 'isquariel'])
]);

blockWords filter tweets that contains specified words. To hide tweets that contains word ruby and hashtag #php write.

var validate = twitter.validate([
  twitter.blockWords(['#php', 'ruby'])
]);

maxHashtags do not skip tweets that contains more hashtags than you specify.

var validate = twitter.validate([
  twitter.maxHashtags(5)
]);

allowLangs show tweets written in specified languages only. E.g.:

var validate = twitter.validate([
  twitter.allowLangs(['en', 'ru'])
]);

noRetweets do not skip old-format retweets (RT @username). To use it exec:

var validate = twitter.validate([
  twitter.noRetweets()
]);

noMentions do not skip tweets start with @username and .@username. To use it exec:

var validate = twitter.validate([
  twitter.noMentions()
]);

noDefaults do not skip tweets posted by users with default userpic. To use it exec:

var validate = twitter.validate([
  twitter.noDefaults()
]);

Beautify

beautify is a function to format tweets you get. Pass it an array of functions you want to use for format.

var beautify = twitter.beautify([
  twitter.autoLink(false),
  twitter.expandEntities({
    "urls": true,
    "media": {
      "width": 500,
      "height": 500,
      "class": "tweet_image"
    }
  }),
  twitter.humanDate(),
  twitter.twtcstFormat(),
  yourAwesomeFunction
]);

The first function takes as an argument tweet in tweet format subscribed above. The next functions takes as an argument result of previous function. So you have to write functions in the order you want to work on tweets.

There are some built-in functions for beautify

autoLink modify tweet.text. The function automatically wrap links, hashtags and usernames with links. To wrap, use:

var beautify = twitter.beautify([
  twitter.autoLinks()
]);

to wrap links only (without usernames and hashtags) use:

var beautify = twitter.beautify([
  twitter.autoLinks(false)
]);

expandEntities modify tweet.text. It will expand links and images.

var beautify = twitter.beautify([
  twitter.expandEntities({
    "urls": true,
    "media": {
      "width": 500,
      "height": 500,
      "class": "tweet_image"
    }
  })
]);

set "urls": true to expand urls if you want to expand them and set "media": { ... } to expand images. Images will be wrapped in an a tag, class of this tag will be class you specified in media.class. If you specified media.width image width will be set to minimal value of media.width and width of the image. media.height property works analogously.

humanDate adds two fields to tweet: tweet.humanDate and tweet.isoDate. Human date is in format YYYY-MM-DD HH:MM and ISO date is Date.toISOString().

var beautify = twitter.beautify([
  twitter.humanDate()
]);

twtcstFormat is a function return tweet converted to format we find convenient for later use:

var beautify = twitter.beautify([
  twitter.twtcstFormat()
]);

The output format is:

{
  "id":     "Tweet id",
  "link":   "Link to user page on Twitter",
  "avatar": "Link to user profile image",
  "login":  "User login (@username without @)",
  "name":   "User name or login",
  "text":   "Improved text of the tweet",
  "date":   "YYYY-MM-DD HH:MM",
  "iso":    "Date in ISO"
}

Development

To get the source form Github execute:

$ git clone [email protected]:meritt/twtcst.git
$ cd twtcst

$ npm install

Then you should specify your access token in examples/staff/oauth.js. Now you have a working example.

To try twtcst.filter execute

$ node ./examples/filter.js

The script puts new tweet to console.

To try twtcst.search execute

$ node ./examples/search.js

First the scripts puts an array of tweets get from search to console and then it will output tweets from stream.

Finally, you can view the working html page with stream of tweets. Just execute:

$ node /examples/socket.js

and open the examples/index.html in your browser.

Contributors

License

The MIT License, see the included License.md file.