UNPKG

yandex-music

Version:

Creative and progressive Node.js framework for applications that interact with yandex music

209 lines (208 loc) 9.25 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.YandexMusicClient = void 0; const url_1 = require("url"); const log_decorator_1 = require("./decorators/log.decorator"); const request_1 = require("./request"); const exceptions_1 = require("./exceptions"); const account_1 = require("./account"); const albums_1 = require("./albums"); const artist_1 = require("./artist"); const feed_1 = require("./feed"); const landing_1 = require("./landing"); const rotor_1 = require("./rotor"); const track_1 = require("./track"); const users_1 = require("./users"); class YandexMusicClient { constructor(token, uid, lang) { this.token = token; this.uid = uid; this.lang = lang; this.url = "https://api.music.yandex.net:443"; this.request = new request_1.Requset(this, this.url); this.request.setAuthorization(); this.request.setLanguage(this.lang); this.account = new account_1.Account(this); this.albums = new albums_1.Albums(this); this.artist = new artist_1.Artist(this); this.feed = new feed_1.Feed(this); this.landing = new landing_1.Landing(this); this.rotor = new rotor_1.Rotor(this); this.track = new track_1.Track(this); this.users = new users_1.Users(this); } static async get(config) { const { auth } = config; const { username, password, token } = auth; const api = (0, request_1.baseClient)("https://api.music.yandex.net:443"); const init = (0, request_1.baseClient)("https://oauth.yandex.ru:443"); if (auth.type === "TOKEN" && token) { const headers = { Authorization: `OAuth ${token}` }; const account = await api.get("/account/status", { headers }); return { uid: account["data"]["result"]["account"]["uid"], language: config.language, token: token, }; } else if (config.auth.type === "LOGIN" && username && password) { const headers = { "Content-Type": "application/x-www-form-urlencoded" }; const params = new url_1.URLSearchParams({ grant_type: "password", client_id: "23cabbbdc6cd418abb4b39c32c41195d", client_secret: "53bc75238f0c4d08a118e51fe9203300", username: username, password: password, }); const { data } = await init.post("token", { headers, params }); return { uid: data["uid"], language: config.language, token: data["access_token"], }; } else throw new exceptions_1.YandexMusicError("No auth crenditials provided"); } /** * Loading the track cover. * @param {string} uri Image uri * @param {CoverSize} size Image Size * @returns Buffer */ async cover(uri, size) { return await this.request.directLink(`https://${uri.replace("%%", size)}`, null); } /** * Loading the track video. * @param {string} url Video uri * @returns Buffer */ async video(url) { return await this.request.directLink(url, null); } /** * Getting genres of music. * @returns Genres of music */ async genres(url) { return await this.request.get(url); } /** * Receiving purchase offers. There are no required parameters. * @returns Information about the products offered if the account is valid. */ async settings() { return await this.request.get("/settings"); } /** * Performing search by query and type, obtaining results. * @param {string} text The text of the request. * @param {boolean} nocorrect If аalse, the erroneous request will be corrected. * @param {SearchType} type Among what type to search (track, playlist, album, artist, user, podcast, all). * @param {number} page Page number. * @param {boolean} playlist_in_best Whether to give out playlists is the best search option. * @returns Search results. */ async search(text, nocorrect = false, type = "all", page = 0, playlist_in_best = true) { const params = { text, nocorrect, type, page, 'playlist-in-best': playlist_in_best }; return await this.request.get("/search", params); } /** * Getting hints for the entered part of the search query. * @param {string} part Part of the search query. * @returns Suggestions for the request. */ async suggest(part) { const params = { part }; return await this.request.get("/search/suggest", params); } /** * A method for sending the current state of the track being listened to. * @param {string} track_id The unique ID of the track. * @param {string} from The name of the client from which the listening takes place. * @param {string} album_id The unique ID of the album. * @param {string} playlist_id The unique ID of the playlist, if one is being listened to. * @param {boolean} from_cache Whether the track is played from the cache. * @param {string} play_id The unique ID of the playback. * @param {number} track_length_seconds Track duration in seconds. * @param {number} total_played_seconds How many tracks were played in total in seconds. * @param {number} end_position_seconds The final value of the seconds played. * @returns OK if the request is successful. */ async play(track_id, from, album_id, playlist_id = "", from_cache = false, play_id = "", track_length_seconds = 0, total_played_seconds = 0, end_position_seconds = 0) { const data = { "track-id": track_id, "from-cache": from_cache, from: from, "play-id": play_id, uid: this.uid, timestamp: new Date().toISOString(), "track-length-seconds": track_length_seconds, "total-played-seconds": total_played_seconds, "end-position-seconds": end_position_seconds, "album-id": album_id, "playlist-id": playlist_id, "client-now": new Date().toISOString(), }; return await this.request.post("/play-audio", null, data); } } __decorate([ (0, log_decorator_1.log)(), __metadata("design:type", Function), __metadata("design:paramtypes", [String, String]), __metadata("design:returntype", Promise) ], YandexMusicClient.prototype, "cover", null); __decorate([ (0, log_decorator_1.log)(), __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", Promise) ], YandexMusicClient.prototype, "video", null); __decorate([ (0, log_decorator_1.log)(), __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", Promise) ], YandexMusicClient.prototype, "genres", null); __decorate([ (0, log_decorator_1.log)(), __metadata("design:type", Function), __metadata("design:paramtypes", []), __metadata("design:returntype", Promise) ], YandexMusicClient.prototype, "settings", null); __decorate([ (0, log_decorator_1.log)(), __metadata("design:type", Function), __metadata("design:paramtypes", [String, Boolean, String, Number, Boolean]), __metadata("design:returntype", Promise) ], YandexMusicClient.prototype, "search", null); __decorate([ (0, log_decorator_1.log)(), __metadata("design:type", Function), __metadata("design:paramtypes", [String]), __metadata("design:returntype", Promise) ], YandexMusicClient.prototype, "suggest", null); __decorate([ (0, log_decorator_1.log)(), __metadata("design:type", Function), __metadata("design:paramtypes", [String, String, String, String, Boolean, String, Number, Number, Number]), __metadata("design:returntype", Promise) ], YandexMusicClient.prototype, "play", null); __decorate([ (0, log_decorator_1.log)(), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], YandexMusicClient, "get", null); exports.YandexMusicClient = YandexMusicClient;