Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@the-convocation/twitter-scraper

Package Overview
Dependencies
Maintainers
4
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@the-convocation/twitter-scraper

[![Documentation badge](https://img.shields.io/badge/docs-here-informational)](https://the-convocation.github.io/twitter-scraper/)

  • 0.6.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
526
decreased by-31.78%
Maintainers
4
Weekly downloads
 
Created
Source

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

We use Conventional Commits, and enforce this with precommit checks.

FAQs

Package last updated on 26 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc