EasyVK-AudioAPI

An unofficial VK Audio API

MIT License

Stars
21

EasyVK AudioAPI

This is an unofficial Audio API for VK. This library uses in Meridius project as a core.

Installation

yarn:

yarn add https://github.com/PurpleHorrorRus/EasyVK-AudioAPI

npm:

npm install https://github.com/PurpleHorrorRus/EasyVK-AudioAPI

Recommend to use #meridius branch rather than master

Usage example

import AudioAPI from "easyvk-audio";

const token = "xxxxxxxxxxx";
const credits = {
    username: "xxxxxxxxxxx",
    password: "xxxxxxxxxxx"
};

const run = async () => {
    const API = await new AudioAPI(token).login({
        ...creidts,
        cookies: "./cookies.json"
    }).catch(e => {
        // Here you can catch 2fa or captcha
        console.error(e);
    });

    const { audios } = await API.audio.getAll();
    console.log(audios);
    console.log(`Wow, I have ${audios.length} songs!`);
};

run();

Handling TFA and Captcha

import AudioAPI from "easyvk-audio";
import readline from "readline";

let client = null;

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

const token = "xxxxxxxxxxx";
const credits = {
    username: "xxxxxxxxxxx",
    password: "xxxxxxxxxxx",
    user: 0123456789
};

const ask = question => new Promise(resolve => {
    return rl.question(question, resolve);
});

const handleError = async err => {
    console.log(err);
    if (err.tfa) {
        return await handleAuth(err);
    } else if (err.captcha) {
        return await handleCaptcha(err);
    }
};

const handleAuth = async err => {
    const code = await ask("Two factor code:");

    // Type "sms" to request code via SMS
    if (code === "sms") {
        const sms = await client.sms();
        return console.log(sms);
    }

    return await client.auth2FA(code, err.info)
        .catch(handleError);
};

const handleCaptcha = async () => {
    return await client.solveCaptcha(await ask("Solve captcha:")).catch(handleError);
};

const run = async () => {
    client = await new AudioAPI(token).login({
        ...credits,
        cookies: "./cookie.json"
    }).catch(handleError);
    
    if (client) {
        const response = await client.audio.get();
        console.log(response);
    }
};

run();

Contribution

You can feel free to open issues tickets or PR to help me with this API.

Conclusion

You can check jest tests to check more functional or see examples.