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

opentdb-api

v1.0.0

Published

Easily retrive random trivia questions using the Open Trivia DB API.

Downloads

20

Readme

About

Easily retrieve random trivia questions using the Open Trivia DB API.

Usage

const opentdb = require('opentdb-api');

var options = {
    amount: 2,
    category: 'science',
    difficulty: 'easy',
    type: 'multiple'
}

opentdb.getTrivia(options).then(result => {
  console.log(result);
});
// =>[ { category: 'Science & Nature',
// =>    type: 'multiple',
// =>    difficulty: 'easy',
// =>    question: 'Alzheimer\'s disease primarily affects which part of the human body?',
// =>    correct_answer: 'Brain',
// =>    incorrect_answers: [ 'Lungs', 'Skin', 'Heart' ] },
// =>  { category: 'Science & Nature',
// =>    type: 'multiple',
// =>    difficulty: 'easy',
// =>    question: 'What animal takes part in Schrödinger\'s most famous thought experiment?',
// =>    correct_answer: 'Cat',
// =>    incorrect_answers: [ 'Dog', 'Bat', 'Butterfly' ] } ]

Examples of using a token to prevent duplicates

Using async/await

const opentdb = require('opentdb-api');

const newToken = await opentdb.getToken();

var options = {
    amount: 2,
    category: 'science',
    difficulty: 'easy',
    type: 'multiple',
    token: newToken
}

const uniqueTrivia = await opentdb.getTrivia(options);

console.log(uniqueTrivia);

Using .then

const opentdb = require('opentdb-api');

opentdb.getToken().then(newToken => {

  var options = {
      amount: 2,
      category: 'science',
      difficulty: 'easy',
      type: 'multiple',
      token: newToken
  }

  opentdb.getTrivia(options).then(uniqueTrivia => {
    console.log(uniqueTrivia);
  });
});

Functions

getTrivia(options)

Retrieves random trivia.

options

  • amount - Amount of questions to retrieve
  • difficulty - Difficulty of questions (easy/medium/hard)
  • category - Category string or ID. Possible strings can be found below. Possible IDs can be found on the OpenTriviaDB website.
  • type - Type of questions to retrieve (multiple or boolean). multiple - Multiple choice. boolean - True/False questions.
  • token - Token to request questions with. Tokens prevent the API from sending the same question twice. Use the getToken() function to retrieve a token.

getToken()

Returns promise with a new session token.

Token can be used to prevent duplicate questions when using the getTrivia() function.

resetToken(token)

Resolves into boolean value if reset was successful or not.

Resets the memory for a given token. Can be used once a token has queried all possible questions.

getCategories()

Returns promise with all categories in the api and their respective IDs

getQuestionCount(category)

Returns promise with total amount of questions for a given category. Possible category can be an ID or string found below.

Possible categories

any general books film music theatre television videogames boardgames science computers mathematics mythology sports geography history politics art celebrities animals vehicles comics gadgets anime cartoons You can also just use the ID of the category found on their website