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

@idavide94/trakt.tv-userscript

v1.0.2

Published

Interact with Trakt.tv API in userscripts

Downloads

9

Readme

@idavide94/trakt.tv-userscript

forked from trakt.tv

Trakt.tv API wrapper for userscripts, featuring:

For more information about the trakt.tv API, read http://docs.trakt.apiary.io/

Example usage

Setup

Add to a userscript via @require:

// @require   https://cdn.jsdelivr.net/npm/@idavide94/trakt.tv-userscript/dist/index.min.js
// @grant     GM.xmlHttpRequest

attention: for the proper working of the library the use of GM.xmlHttpRequest via @grant is required!

Initialize

const options = {
  client_id: '<the_client_id>',
  client_secret: '<the_client_secret>',
  redirect_uri: null, // defaults to 'urn:ietf:wg:oauth:2.0:oob'
  api_url: null, // defaults to 'https://api.trakt.tv'
  useragent: null, // defaults to 'trakt.tv/<version>'
  pagination: true // defaults to false, global pagination (see below)
}
const trakt = new Trakt(options)

note: add debug: true to the options object to get debug logs of the requests executed in your console

OAUTH

  1. Generate Auth URL
const traktAuthUrl = trakt.get_url()
  1. Authentication is done at that URL, it redirects to the provided uri with a code and a state

  2. Verify code (and optionally state for better security) from returned auth, and get a token in exchange

trakt.exchange_code('code', 'csrf token (state)').then((result) => {
  // contains tokens & session information
  // API can now be used with authorized requests
})

Alternate OAUTH "device" method

trakt
  .get_codes()
  .then((poll) => {
    // poll.verification_url: url to visit in a browser
    // poll.user_code: the code the user needs to enter on trakt

    // verify if app was authorized
    return trakt.poll_access(poll)
  })
  .catch((error) => {
    // error.message == 'Expired' will be thrown if timeout is reached
  })

Refresh token

trakt.refresh_token().then((results) => {
  // results are auto-injected in the main module cache
})

Storing token over sessions

// get token, store it safely.
const token = trakt.export_token()

// injects back stored token on new session.
trakt.import_token(token).then((newTokens) => {
  // Contains token, refreshed if needed (store it back)
})

Revoke token

trakt.revoke_token()

Actual API requests

See methods in methods.json or the docs.

trakt.calendars.all
  .shows({
    start_date: '2015-11-13',
    days: '7',
    extended: 'full'
  })
  .then((shows) => {
    // Contains Object{} response from API (show data)
  })
trakt.search
  .text({
    query: 'tron',
    type: 'movie,person'
  })
  .then((response) => {
    // Contains Array[] response from API (search data)
  })
trakt.search
  .id({
    id_type: 'imdb',
    id: 'tt0084827'
  })
  .then((response) => {
    // Contains Array[] response from API (imdb data)
  })

Using pagination

You can extend your calls with pagination: true to get the extra pagination info from headers.

trakt.movies
  .popular({
    pagination: true
  })
  .then((movies) => {
    /**
    movies = Object {
      data: [<actual data from API>],
        pagination: {
          item-count: "80349",
            limit: "10",
            page: "1",
            page-count: "8035"
        }
    }
    **/
  })

note: this will contain data and pagination for all calls, even if no pagination is available (result.pagination will be false). it's typically for advanced use only

Load plugins[^wip]

When calling new Trakt(), include desired plugins in an object (must be installed from npm):

const trakt = new Trakt({
  client_id: '<the_client_id>',
    client_secret: '<the_client_secret>',
    plugins: {  // load plugins
        images: require('trakt.tv-images')
    }
    options: {  // pass options to plugins
        images: {
          smallerImages: true
        }
    }
});

The plugin can be accessed with the key you specify. For example trakt.images.get().

Write plugins[^wip]

See the documentation.

Notes

  • You can use 'me' as username if the user is authenticated.
  • Timestamps (such as token expires property) are Epoch in milliseconds.

[^wip]: plugins, and related docs, are currently a work in progress for this fork