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 🙏

© 2025 – Pkg Stats / Ryan Hefner

twx-media-scrapper

v0.1.5

Published

Twitter/x.com media content extractor via Puppeteer and TwitterDL

Readme

twx-media-scrapper

A professional Node.js module to extract direct video URLs from X (formerly Twitter), with automated authentication using Puppeteer. It supports headless login, session caching, and CLI-based two-factor code input (OTP) when required.


🚀 Features

  • ✅ Headless login to x.com
  • 🔐 Automatic token (Bearer) and cookie retrieval
  • 🧠 CLI prompt for verification code (if X.com asks for OTP)
  • 📦 Auth session cache saved in auth_cache.json
  • 🎥 Extracts all available video qualities using twitter-downloader
  • 🧰 Fully compatible with server/VPS environments

📦 Installation

npm install twx-media-scrapper puppeteer twitter-downloader

puppeteer and twitter-downloader are required as peer dependencies.


✨ Usage

const { listTwitterMediaPath } = require("twx-media-scrapper");

// Ganti dengan akun dummy Twitter/X kamu
const email = "[email protected]";
const username = "yourUsername";
const password = "yourPassword";

const twitterVideoUrl = "https://x.com/elonmusk/status/1911984444459667639";

listTwitterMediaPath(twitterVideoUrl, email, username, password)
  .then(videos => console.log("🎥 List Videos:", videos))
  .catch(err => console.error("❌ Error:", err.message));

📊 Sample Output

Here is an example of the JSON result returned by the listTwitterMediaPath() function:

[
  {
    "type": "video",
    "cover": "https://pbs.twimg.com/amplify_video_thumb/1911851777877487616/img/295hD94p38eqAET1.jpg",
    "duration": "1:18",
    "expandedUrl": "https://x.com/RedWave_Press/status/1911852100230771088/video/1",
    "videos": [
      {
        "bitrate": 288000,
        "content_type": "video/mp4",
        "quality": "482x270",
        "url": "https://video.twimg.com/amplify_video/1911851777877487616/vid/avc1/482x270/0P1rL4U7-Jm1PSk6.mp4?tag=16"
      },
      {
        "bitrate": 832000,
        "content_type": "video/mp4",
        "quality": "644x360",
        "url": "https://video.twimg.com/amplify_video/1911851777877487616/vid/avc1/644x360/CTT8V1u8HsSsauzc.mp4?tag=16"
      },
      {
        "bitrate": 2176000,
        "content_type": "video/mp4",
        "quality": "1288x720",
        "url": "https://video.twimg.com/amplify_video/1911851777877487616/vid/avc1/1288x720/_Uy9S8dPYHCzAVMK.mp4?tag=16"
      }
    ]
  }
]

This output includes:

  • type: Content type (e.g. video)
  • cover: Thumbnail image of the video
  • duration: Video length in minutes and seconds
  • expandedUrl: Direct link to the video on X.com
  • videos: List of available video streams with different bitrates and resolutions

💾 Session Caching

Upon successful login, a local cache file auth_cache.json is created, storing the session token and cookie:

{
  "authorization": "Bearer AAAAAAA...",
  "cookie": "auth_token=...; ct0=...; ..."
}

This file is reused automatically for subsequent requests.
If the token is invalid or expired, the module will re-authenticate.


🔐 OTP Handling (Two-Factor Auth)

When logging in from a new IP address or location, X.com may request an OTP sent to your email or phone.
The module automatically detects this challenge and prompts the user in the terminal:

Twitter requires a verification code.
Enter the OTP sent to your email/phone: ██████

The login process continues once the code is entered.


⚠️ Notes

  • Intended for use with non-private accounts.
  • Recommended to use a dummy account for scraping or automation purposes.
  • Not intended for downloading protected or copyrighted content.

📄 License

MIT License
© 2025 [Harris Munahar]


🙋 Support & Contribution

Feel free to open issues or pull requests for improvements or fixes.
If you encounter login issues due to layout changes on X.com, updates may be required in the login automation.