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

hashtag-count

v1.1.0

Published

Count hashtag occurrences over time using Twitter's Streaming API.

Downloads

22

Readme

hashtag-count

hashtag-count

Build Status

Count hashtag occurrences over time at a provided interval using Twitter's Streaming API. Multiple hashtags can be tracked simultaneously. It can be used to collect hashtag counts for a provided length of time, with the results analyzed after the process has finished, or it can be configured to run forever, analyzing the results in real-time at the end of every time interval.

Installation

npm install hashtag-count

Usage

Set up Twitter keys/tokens

You will need to log into Twitter's Application Management page to generate keys/tokens for your application so you can connect to Twitter's API.

var HashtagCount = require('hashtag-count');

var hc = new HashtagCount({
  'consumer_key': '...',
  'consumer_secret': '...',
  'access_token': '...',
  'access_token_secret': '...'
});

Choose hashtags and time interval

// Array of hashtags to tally. Do not include # prefix.
var hashtags = ['superbowl', 'pizza', 'beer'];

// Hashtag tallies for each time interval will be added to the results object.
var interval = '30 seconds';

Run for limited time and analyze results when finished

// Stop running after this amount of time has passed.
var limit = '5 minutes';

// Called after time limit has been reached.
var finishedCb = function (err, results) {
  if (err) {
    console.error(err);
  } else {
    console.log(results);
  }
};

// Open a connection to Twitter's Streaming API and start capturing tweets!
hc.start({
  hashtags: hashtags,       // required
  interval: interval,       // required
  limit: limit,             // optional
  finishedCb: finishedCb,   // optional
});

Run for unlimited time and analyze results after every interval

// Delete data older than this.
var history = '5 minutes';

// Called at the end of each time interval.
var intervalCb = function (err, results) {
  if (err) {
    console.error(err);
  } else {
    console.log(results);
  }
};

// Open a connection to Twitter's Streaming API and start capturing tweets!
hc.start({
  hashtags: hashtags,       // required
  interval: interval,       // required
  history: history,         // optional
  intervalCb: intervalCb,   // optional
});

Results object

The results object has the same structure regardless of whether it appears in the intervalCb or finishedCb callbacks. It takes the following form, where the time stamp is an ISO string in UTC time representing the beginning of each interval:

{
  '2017-01-16T00:00:10.606Z': { 'superbowl': 6, 'pizza': 1, 'beer': 8 },
  '2017-01-16T00:01:10.610Z': { 'superbowl': 7, 'pizza': 1, 'beer': 4 },
  '2017-01-16T00:02:10.612Z': { 'superbowl': 3, 'pizza': 1, 'beer': 0 }
}

Connection status callbacks and a warning about rate limiting

Although Twitter Streaming API connections are designed to remain open indefinitely, there are several reasons why your application may get disconnected. This module will attempt to reconnect automatically. How long it takes to reconnect largely depends on whether your application is being rate limited by Twitter.

You can set the optional connectingCb, reconnectingCb, and connectedCb callbacks to report and react to these events:

var connectingCb = function () {
  // Called when connecting to Twitter Streaming API for the first time.
};

var reconnectingCb = function () {
  // Called as soon as a failed connection is detected and a reschedule attempt
  // is scheduled.
};

var connectedCb = function () {
  // Called when a connection is established, either on the first connection
  // attempt or a later reconnection attempt.
};

hc.start({
  // ...
  connectingCb: connectingCb,       // optional
  reconnectingCb: reconnectingCb,   // optional
  connectedCb: connectedCb,         // optional
});

Example scripts

Two example scripts are included. limited.js demonstrates how to use this module to collect hashtag counts for a finite length of time, whereas unlimited.js demonstrates how to analyze hashtag counts at the end of every time interval without a time limit. You will need to be in the examples directory to run these scripts so they can read config.json properly.

Development and testing

If you would like to get involved in development for this module, first clone this repository, then add your Twitter application keys to config.json. The keys set in this file are read by both the example scripts and unit tests.

You will also need to install the module's dependencies and devDependencies with the following command:

npm install

Unit tests

Unit tests are located in the test directory and can be run from the hashtag-count root directory with the following command:

npm test

Your Twitter application keys can be set via the following environment variables for automated testing with Travis CI:

  • CONSUMER_KEY
  • CONSUMER_SECRET
  • ACCESS_TOKEN
  • ACCESS_TOKEN_SECRET