3.0.17 • Published 4 months ago

osu-api-extended v3.0.17

Weekly downloads
48
License
MIT
Repository
github
Last release
4 months ago

npm.io npm.io npm.io npm.io

osu-api-extended

Quick Links: Features / Usage / Install / Quickstart / Tools

Features

  • Api
  • Auto session refresh
  • Does not require you to login for each action
  • Built-in Tools
    • tools.build_url - Create link for user, score, editor_timing and others
    • tools.calculate_accuracy - Calculate accuracy from play hits
    • tools.calculate_hits - Calculate hits if play was an FC
    • tools.calculate_mods - Calculate mods Number/Name from Number/Name
    • tools.calculate_net_pp - Calculate how much pp would you gain from a play
    • tools.calculate_pp - Create link for user, score, editor_timing and others
    • tools.calculate_rank - Calculate rank from play hits
    • tools.calculate_total_passed_objects - Calculate total passed objects
    • tools.country_details - Get country name and code by providing country name/code
    • tools.download_beatmaps - Downloads a beatmap or beatmap set by given ID. (Supports different hosts)
    • See documentation
  • Setting to prevent throw, instead send .error

Installation

npm i osu-api-extended
yarn install osu-api-extended
pnpm install osu-api-extended
bun install osu-api-extended

Quickstart

Links: create your client here / get your api key here

Quick Links: v2 - client auth / v2 - lazer auth / v2 - cli auth / v2 - Discord Verify / v2 - Website login / v1 usage / v2 - prevent throw errors

Client Auth

import { auth } from 'osu-api-extended';


async function main() {
  try {
    await auth.login({
      type: 'v2',
      client_id: CLIENT_ID,
      client_secret: CLIENT_SECRET,
      cachedTokenPath: './client.json' // path to the file your auth token will be saved (to prevent osu!api spam)
    });

    const result = await v2.users.details({ user: 'mrekk', mode: 'osu', key: '@' });
    if (result.error != null) {
      console.log(result.error);
      return;
    };


    console.log(result);
  } catch (error) {
    console.log(error);
  };
};

main();

Lazer Auth

Authorization though your account

import { auth, v2 } from 'osu-api-extended';


async function main() {
  try {
    await auth.login({
      type: 'lazer',
      login: LOGIN,
      password: PASSWORD,
      cachedTokenPath: './lazer.json' // path to the file your auth token will be saved (to prevent osu!api spam)
    });

    const result = await v2.me.details();
    if (result.error != null) {
      console.log(result.error);
      return;
    };


    console.log(result);
  } catch (error) {
    console.log(error);
  };
};

main();

CLI Auth

import { auth } from 'osu-api-extended';


async function main() {
  try {
    await auth.login({
      type: 'cli',
      client_id: CLIENT_ID,
      client_secret: CLIENT_SECRET,
      redirect_url: REDIRECT_URL,
      scopes: ['public'],
      cachedTokenPath: './cli.json' // path to the file your auth token will be saved (to prevent osu!api spam)
    });

    const result = await v2.me.details();
    if (result.error != null) {
      console.log(result.error);
      return;
    };


    console.log(result);
  } catch (error) {
    console.log(error);
  };
};

main();

Discord Verify

import { Client } from 'discord.js';
import { auth } from 'osu-api-extended';

const discord_bot = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages] }); // no clue why i need those intents


// send this link to user
discord_bot.on('messageCreate', async message => {
  if (message.author.bot) return;
  if (!message.content.startsWith('!')) return;

  const [command, value1, value2] = message.content.split('!').join('').split(' ');
  logger('command', { _c: 'yellowBright', v: command }, value1, value2);

  if (command == 'verify') {
    const url = const url = auth.build_url({
      client_id: CLIENT_ID,
      redirect_url: REDIRECT_URL,
      scopes: ['identify'],
      state: message.author.id
    });


    message.reply(`Verify via this link: ${url}`);
    return;
  };
});



// somewhere else verify this
function verifyUser(code, state) {
  // it returns user object
  const verify = await auth.authorize({
    code: code,
    client_id: CLIENT_ID,
    client_secret: CLIENT_SECRET,
    redirect_url: REDIRECT_URL
  });


  console.log(verify);
};

Website login

Nitro framework example

import { auth } from 'osu-api-extended';


// /auth page
export default defineEventHandler(event => {
  const build_url = auth.build_url({
    client_id: CLIENT_ID,
    redirect_uri: REDIRECT_URL,
    scopes: ['identify'],
  });


  return sendRedirect(event, build_url);
});


// /callback page
export default defineEventHandler(async event => {
  const { code } = getQuery(event);
  const verify = await auth.authorize({
    code: code.toString(),

    client_id: CLIENT_ID,
    client_secret: CLIENT_SECRET,
    redirect_url: REDIRECT_URL,
  });


  setCookie(event, 'user_id', verify.id.toString());
  setCookie(event, 'user_name', verify.username.toString());

  setCookie(event, 'access_token', verify.access_token);
  setCookie(event, 'refresh_token', verify.refresh_token);


  return sendRedirect(event, '/');
});

V1 usage

import { auth, v1 } from 'osu-api-extended';


async function main() {
  try {
    auth.login({
      type: 'v1',
      api_key: API_KEY,
    });


    const beatmap = await v1.beatmap.diff(3798013);
    console.log(beatmap);
    
  } catch (error) {
    console.log(error);
  };
};

main();

Prevent throw errors

import { auth } from 'osu-api-extended';

auth.settings.throwErrors = false;


async function main() {
  try {
    await auth.login({
      type: 'v2',
      client_id: CLIENT_ID,
      client_secret: CLIENT_SECRET,
    });

    const result = await v2.beatmaps.events.list({ types: ['approve'] });
    if (result.error instanceof Error) {
    // or
    if (result.error != null) {
      console.log(result.error);
      return;
    };

    console.log(result);
  } catch (error) {
    console.log(error);
  };
};


main();

Tools

import { tools } from 'osu-api-extended';


function build_url() {
  try {
    const result = tools.build_url({ type: 'beatmap', value: 4397592 });
    if (result.error != null) {
      console.log(result.error);
      return;
    };


    console.log(result);
  } catch (error) {
    console.log(error);
  };
};


function download_beatmaps() {
  try {
    const set_id = 320118;
    const progress_update = (...args) => {
      console.log(args);
    };


    const result = await tools.download_beatmaps({
      type: 'set',
      host: 'gatari',
      id: set_id,
      file_path: `./cache/${set_id}.osz`,
      progress_log_fn: progress_update
    });
    if (result.error != null) {
      console.log(result.error);
      return;
    };


    console.log(result);
  } catch (error) {
    console.log(error);
  };
};


function calculate_accuracy() {
  try {
    const hits = { 300: 123, 100: 12, 50: 1, 0: 1 };
    const result = tools.calculate_accuracy(hits, 'osu');
    if (result.error != null) {
      console.log(result.error);
      return;
    };


    console.log(result);
  } catch (error) {
    console.log(error);
  };
};


function calculate_mods() {
  try {
    const result = tools.calculate_mods('HDDT');
    // or
    const result = tools.calculate_mods(72);
    // or
    const result = tools.calculate_mods([{ acronym: "EZ" }]);
    if (result.error != null) {
      console.log(result.error);
      return;
    };


    console.log(result);
  } catch (error) {
    console.log(error);
  };
};


function country_details() {
  try {
    const result = tools.country_details('US');
    // or
    const result = tools.country_details('United States');
    if (result.error != null) {
      console.log(result.error);
      return;
    };


    console.log(result);
  } catch (error) {
    console.log(error);
  };
};


function calculate_rank() {
  try {
    const hits = { 300: 123, 100: 12, 50: 1, 0: 1 };
    const result = tools.calculate_rank(hits, 72, 'osu');
    if (result.error != null) {
      console.log(result.error);
      return;
    };


    console.log(result);
  } catch (error) {
    console.log(error);
  };
};


function calculate_total_passed_objects() {
  try {
    const hits = { 300: 123, 100: 12, 50: 1, 0: 1 };
    const result = tools.calculate_total_passed_objects(hits, 'osu');
    if (result.error != null) {
      console.log(result.error);
      return;
    };


    console.log(result);
  } catch (error) {
    console.log(error);
  };
};
  
  
function calculate_hits() {
  try {
    const hits = { 300: 123, 100: 12, 50: 1, 0: 1 };
    const result = tools.calculate_hits(hits, 'osu');
    if (result.error != null) {
      console.log(result.error);
      return;
    };


    console.log(result);
  } catch (error) {
    console.log(error);
  };
};


function calculate_net_pp() {
  try {
    const plays = [1000, 900, 800, 700];
    const scores = [{ id: 123, pp: 1000 }, { id: 123, pp: 555 }, { id: 123, pp: 234 }, { id: 123, pp: 100 }];
    const result = tools.calculate_net_pp(plays, 400);
    // or 
    const result = tools.calculate_net_pp(scores, 400);
    if (result.error != null) {
      console.log(result.error);
      return;
    };


    console.log(result);
  } catch (error) {
    console.log(error);
  };
};

Dependencies

zero

Credits

3.0.12

4 months ago

3.0.13

4 months ago

3.0.10

6 months ago

3.0.11

5 months ago

3.0.16

4 months ago

3.0.17

4 months ago

3.0.14

4 months ago

3.0.15

4 months ago

3.0.8

6 months ago

3.0.7

6 months ago

3.0.6

7 months ago

3.0.9

6 months ago

3.0.5

8 months ago

3.0.0-beta.36

9 months ago

3.0.0-beta.37

8 months ago

3.0.0-beta.38

8 months ago

3.0.4

8 months ago

3.0.3

8 months ago

3.0.2

8 months ago

3.0.1

8 months ago

3.0.0

8 months ago

2.8.52

11 months ago

2.8.51

1 year ago

2.8.5

1 year ago

3.0.0-beta.34

1 year ago

3.0.0-beta.35

11 months ago

3.0.0-beta.33

1 year ago

3.0.0-beta.31

1 year ago

3.0.0-beta.32

1 year ago

3.0.0-beta.30

1 year ago

2.8.4

1 year ago

3.0.0-beta.27

1 year ago

2.8.3

1 year ago

2.8.2

1 year ago

2.8.1

1 year ago

2.8.0

1 year ago

2.7.69

1 year ago

2.7.68

1 year ago

3.0.0-beta.21

1 year ago

3.0.0-beta.22

1 year ago

3.0.0-beta.23

1 year ago

3.0.0-beta.24

1 year ago

3.0.0-beta.25

1 year ago

3.0.0-beta.26

1 year ago

3.0.0-beta.20

1 year ago

3.0.0-beta.19

1 year ago

3.0.0-beta.17

1 year ago

3.0.0-beta.18

1 year ago

3.0.0-beta.16

1 year ago

3.0.0-beta.15

1 year ago

3.0.0-beta.12

1 year ago

3.0.0-beta.13

1 year ago

3.0.0-beta.14

1 year ago

3.0.0-beta.9

2 years ago

3.0.0-beta.8

2 years ago

3.0.0-beta.10

2 years ago

3.0.0-beta.11

2 years ago

3.0.0-beta.1

2 years ago

3.0.0-beta.3

2 years ago

3.0.0-beta.2

2 years ago

3.0.0-beta.5

2 years ago

3.0.0-beta.4

2 years ago

3.0.0-beta.7

2 years ago

2.7.66

2 years ago

2.7.67

2 years ago

3.0.0-beta.0

2 years ago

2.7.62

2 years ago

2.7.61

2 years ago

2.7.65

2 years ago

2.7.64

2 years ago

2.7.63

2 years ago

2.7.0

2 years ago

2.7.2

2 years ago

2.7.1

2 years ago

2.7.4

2 years ago

2.7.3

2 years ago

2.7.6

2 years ago

2.7.5

2 years ago

2.7.51

2 years ago

2.5.18

2 years ago

2.5.19

2 years ago

2.5.16

2 years ago

2.5.17

2 years ago

2.5.25

2 years ago

2.5.21

2 years ago

2.5.22

2 years ago

2.5.23

2 years ago

2.5.24

2 years ago

2.5.20

2 years ago

2.5.30

2 years ago

2.5.14

2 years ago

2.5.15

2 years ago

2.5.11

3 years ago

2.5.12

3 years ago

2.5.6

3 years ago

2.5.5

3 years ago

2.5.8

3 years ago

2.5.7

3 years ago

2.5.9

3 years ago

2.5.10

3 years ago

2.5.0

3 years ago

2.5.2

3 years ago

2.5.1

3 years ago

2.1.5

3 years ago

2.5.4

3 years ago

2.5.3

3 years ago

2.1.2

3 years ago

2.1.4

3 years ago

2.1.3

3 years ago

2.1.0

3 years ago

2.0.11

3 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.5

4 years ago

2.0.4

4 years ago

2.0.7

4 years ago

2.0.6

4 years ago

2.0.9

3 years ago

2.0.8

3 years ago

2.0.1

4 years ago

2.0.10

3 years ago

2.0.0

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.10

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.4.9

5 years ago

0.4.8

5 years ago

0.4.7

5 years ago

0.4.6

5 years ago

0.4.5

5 years ago

0.3.9

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.8

5 years ago

0.3.7

5 years ago

0.3.6

5 years ago

0.3.5

5 years ago

0.3.4

5 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.0

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.26

5 years ago

0.0.25

5 years ago

0.0.23

5 years ago

0.0.22

5 years ago

0.0.21

5 years ago

0.0.2-alpha-4

5 years ago

0.0.2-alpha-3

5 years ago

0.0.2-alpha-2

5 years ago

0.0.2-alpha-1

5 years ago

0.0.2-alpha

5 years ago

0.0.2

5 years ago

0.0.11

5 years ago

0.0.1

5 years ago