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

@the-convocation/twitter-scraper

v0.14.1

Published

A port of n0madic/twitter-scraper to Node.js.

Downloads

6,897

Readme

twitter-scraper

Documentation badge

A port of n0madic/twitter-scraper to Node.js.

Twitter's API is annoying to work with, and has lots of limitations — luckily their frontend (JavaScript) has it's own API, which I reverse-engineered. No API rate limits. No tokens needed. No restrictions. Extremely fast.

You can use this library to get the text of any user's Tweets trivially.

Known limitations:

  • Search operations require logging in with a real user account via scraper.login().
  • Twitter's frontend API does in fact have rate limits (#11)

Installation

This package requires Node.js v16.0.0 or greater.

NPM:

npm install @the-convocation/twitter-scraper

Yarn:

yarn add @the-convocation/twitter-scraper

TypeScript types have been bundled with the distribution.

Usage

Most use cases are exactly the same as in n0madic/twitter-scraper. Channel iterators have been translated into AsyncGenerator instances, and can be consumed with the corresponding for await (const x of y) { ... } syntax.

Browser usage

This package directly invokes the Twitter API, which does not have permissive CORS headers. With the default settings, requests will fail unless you disable CORS checks, which is not advised. Instead, applications must provide a CORS proxy and configure it in the Scraper options.

Proxies (and other request mutations) can be configured with the request interceptor transform:

const scraper = new Scraper({
  transform: {
    request(input: RequestInfo | URL, init?: RequestInit) {
      // The arguments here are the same as the parameters to fetch(), and
      // are kept as-is for flexibility of both the library and applications.
      if (input instanceof URL) {
        const proxy = "https://corsproxy.io/?" +
          encodeURIComponent(input.toString());
        return [proxy, init];
      } else if (typeof input === "string") {
        const proxy = "https://corsproxy.io/?" + encodeURIComponent(input);
        return [proxy, init];
      } else {
        // Omitting handling for example
        throw new Error("Unexpected request input type");
      }
    },
  },
});

corsproxy.io is a public CORS proxy that works correctly with this package.

The public CORS proxy corsproxy.org does not work at the time of writing (at least not using their recommended integration on the front page).

Next.js 13.x example:

"use client";

import { Scraper, Tweet } from "@the-convocation/twitter-scraper";
import { useEffect, useMemo, useState } from "react";

export default function Home() {
  const scraper = useMemo(
    () =>
      new Scraper({
        transform: {
          request(input: RequestInfo | URL, init?: RequestInit) {
            if (input instanceof URL) {
              const proxy = "https://corsproxy.io/?" +
                encodeURIComponent(input.toString());
              return [proxy, init];
            } else if (typeof input === "string") {
              const proxy = "https://corsproxy.io/?" +
                encodeURIComponent(input);
              return [proxy, init];
            } else {
              throw new Error("Unexpected request input type");
            }
          },
        },
      }),
    [],
  );
  const [tweet, setTweet] = useState<Tweet | null>(null);

  useEffect(() => {
    async function getTweet() {
      const latestTweet = await scraper.getLatestTweet("twitter");
      if (latestTweet) {
        setTweet(latestTweet);
      }
    }

    getTweet();
  }, [scraper]);

  return (
    <main className="flex min-h-screen flex-col items-center justify-between p-24">
      {tweet?.text}
    </main>
  );
}

Edge runtimes

This package currently uses cross-fetch as a portable fetch. Edge runtimes such as CloudFlare Workers sometimes have fetch functions that behave differently from the web standard, so you may need to override the fetch function the scraper uses. If so, a custom fetch can be provided in the options:

const scraper = new Scraper({
  fetch: fetch,
});

Note that this does not change the arguments passed to the function, or the expected return type. If the custom fetch function produces runtime errors related to incorrect types, be sure to wrap it in a shim (not currently supported directly by interceptors):

const scraper = new Scraper({
  fetch: (input, init) => {
    // Transform input and init into your function's expected types...
    return fetch(input, init)
      .then((res) => {
        // Transform res into a web-compliant response...
        return res;
      });
  },
});

Contributing

Setup

This project currently requires Node 18.x for development and uses Yarn for package management. Corepack is configured for this project, so you don't need to install a particular package manager version manually.

The project supports Node 16.x at runtime, but requires Node 18.x to run its build tools.

Just run corepack enable to turn on the shims, then run yarn to install the dependencies.

Basic scripts

  • yarn build: Builds the project into the dist folder
  • yarn test: Runs the package tests (see Testing first)

Run yarn help for general yarn usage information.

Testing

This package includes unit tests for all major functionality. Given the speed at which Twitter's private API changes, failing tests are to be expected.

yarn test

Before running tests, you should configure environment variables for authentication.

TWITTER_USERNAME=    # Account username
TWITTER_PASSWORD=    # Account password
TWITTER_EMAIL=       # Account email
TWITTER_COOKIES=     # JSON-serialized array of cookies of an authenticated session
PROXY_URL=           # HTTP(s) proxy for requests (optional)

Commit message format

We use Conventional Commits, and enforce this with precommit checks. Please refer to the Git history for real examples of the commit message format.