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

pr0gramm-api

v4.2.1

Published

Node.js pr0gramm API

Downloads

114

Readme

pr0gramm-api Build Status

A Node.js API for pr0gramm written in TypeScript.

npm install -S pr0gramm-api

Usage

Login with username/password:

import { Pr0grammAPI, NodeRequester, ItemFlags } from "pr0gramm-api";

main();
async function main() {
    const requester = NodeRequester.create();
    // When using this library in the browser, use this requester:
    // const requester = BrowserRequester.create();

    const api = Pr0grammAPI.create(requester);

    const mainItems = await api.items.getItems({
        promoted: true,
        flags: ItemFlags.SFW
    });

    console.log(mainItems.items);

    const captchaData = await api.user.requestCaptcha();
    // captchaData.captcha contains the image as a data URI

    const loginResponse = await api.user.login("cha0s", "stahl0fen80", captchaData.token, "aaaaa");
    if(!loginResponse.success) {
        console.log("Could not log in :(");
        if(loginResponse.ban !== null) {
            console.log("You are banned. Reason:");
            console.log(loginResponse.ban.reason);
            return;
        }
    }
}

Login with oAuth:

import * as readline from "node:readline/promises";
import { AuthorizationCode } from "simple-oauth2";
import { Pr0grammAPI, NodeRequester, ItemFlags } from "pr0gramm-api";

const oAuthAccessCodeClient = new AuthorizationCode({
    client: {
        // See above
        id: "<client_id>",
        secret: "<client_secret>",
    },
    auth: {
        tokenHost: "https://pr0gramm.com/",
        tokenPath: "/api/oauth/createAccessToken",
        authorizePath: "/oauth/authorize",
    }
});

main();
async function main() {
    const authorizationUri = oAuthAccessCodeClient.authorizeURL({
        redirect_uri: authCallbackUrl,
        scope: "items.get",
        state: "<state>",
    });

    console.log("Go to this URL and enter the auth code from ?code=<auth code> from the callback URL:");
    console.log(authorizationUri);

    const rl = readline.createInterface({
        input: process.stdin,
        output: process.stdin,
    });
    const authCode = await rl.question("Auth-Code: ");
    rl.close();

    const tokenHandler = await oAuthAccessCodeClient.getToken({
        code: authCode,
    });

    const requester = NodeRequester.create();
    requester.setOAuthAccessToken(tokenHandler.token.access_token);

    const api = Pr0grammAPI.create(requester);

    const mainItems = await api.items.getItems({
        promoted: true,
        flags: ItemFlags.All
    });

    console.log(mainItems.items);
}

Stream Walker

The item stream requires you to call the next page of elements. Because it is a common operation to just walk over all items in the stream, there is a stream walker api for convenience:

import { Pr0grammAPI, NodeRequester, ItemFlags } from "pr0gramm-api";

main();
async function main() {
    const api = Pr0grammAPI.create(NodeRequester.create());

    // Create a walker that iterates through the entire stream of elements
    // starting at item 0, going upwards
    const itemStream = api.items.walkStreamNewer({
        newer: 0,
        flags: ItemFlags.SFW,
        promoted: false,
    });

    // Asynchronous iteration over all items on pr0gramm
    // automatically requests next items
    for await (const item of itemStream) {
        console.log(item.id + ": " + item.user);
    }
}

Important:

  • This approach uses async generators, which are currently hidden behind node's --harmony flag. To use this API, you need to start node with --harmony.
  • If you are using TypeScript, you need to have "esnext.asynciterable" and "es6" in your lib entry in tsconfig.json.
  • The module is exposed as CommonJS, not (yet) ES modules. If you use plain JavaScript, keep in mind using CommonJS imports instead of ES imports: const { Pr0grammAPI, ItemFlags } = require("pr0gramm-api");