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

tweets-to-json

v0.2.0

Published

Save your tweets into a JSON file.

Downloads

129

Readme

Save your tweets into a JSON file using Twitter API. You can run it programatically (for example with GitLab or GitHub CI) to automatically keep a JSON archive of your tweets.

This was originally built for an easy way to provide external data to Hugo data templates.

Note: this script is not intended for huge datasets, it handles all tweets in memory. According to user_timeline docs it "can only return up to 3,200 of a user's most recent Tweets". YMMV. Read also about Twitter's rate limits.

Configuration

  1. Get Bearer token key for your app from Twitter Developer Portal and add it to TWITTER_BEARER_TOKEN environment variable (or to .env file in the root of your project)
  2. Create tweets-to-json-config.js with searchParams and exportFn (see example below)

Usage

When you run tweets-to-json first time, a tweets.json file (can be overrided) is created in the same directory and all your tweets are saved into it. Subsequent invocations will check the file, take the latest tweets ID, and only query for tweets after that ID. The tweets are saved in order, latest tweet first.

If you run this from CI, you might find --fail-when-zero flag handy as it returns error code 1 when there are no tweets.

Usage: tweets-to-json -o <filepath> [options]

Options:
  -o, --output-file <filepath>  specify where to output the tweets (default: "./tweets.json")
  --fail-when-zero              return exit status 1 if no new tweets are found
  -V, --version                 output the version number
  -h, --help                    display help for command

Example configuration

Full example of tweets-to-json.config.js:

const dayjs = require('dayjs')

module.exports = {
  searchParams: {
    screen_name: 'uninen',
    exclude_replies: true,
    trim_user: true,
    tweet_mode: 'extended',
    count: 100,
  },
  exportFn: (result) => {
    const tags = []
    const mentions = []
    const media = []
    const urls = []

    if (result.entities.hashtags && result.entities.hashtags.length > 0) {
      for (const tag of result.entities.hashtags) {
        tags.push(tag.text)
      }
    }
    if (
      result.entities.user_mentions &&
      result.entities.user_mentions.length > 0
    ) {
      for (const user of result.entities.user_mentions) {
        mentions.push(user.screen_name)
      }
    }
    if (result.entities.media && result.entities.media.length > 0) {
      for (const obj of result.entities.media) {
        media.push({
          url: obj.media_url_https,
          tco_url: obj.url,
        })
      }
    }
    if (result.entities.urls && result.entities.urls.length > 0) {
      for (const url of result.entities.urls) {
        urls.push({
          tco_url: url.url,
          url: url.expanded_url,
        })
      }
    }

    return {
      id: result.id_str,
      text: result.full_text,
      urls: urls,
      tags: tags,
      media: media,
      mentions: mentions,
      timestamp: dayjs(result.created_at).unix(),
    }
  },
}

Contributing

All contributions are welcome! Please follow the code of conduct when interacting with others.

This project lives on GitLab and is mirrored on GitHub.

Follow @Uninen on Twitter.