Skip to content

Commit

Permalink
use alternate API request for fetching playlist, added searching for …
Browse files Browse the repository at this point in the history
…related tracks
  • Loading branch information
zackradisic committed Aug 24, 2020
1 parent 56d90c8 commit 84d8d9a
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 120 deletions.
27 changes: 13 additions & 14 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,22 +170,21 @@ var SCDL = /** @class */ (function () {
});
};
/**
* Returns info about a given track.
* @param id - The track ID
* Returns info about the given track(s) specified by ID.
* @param ids - The ID(s) of the tracks
* @param clientID - A Soundcloud Client ID, will find one if not provided
* @returns Info about the track
*/
SCDL.prototype.getTrackInfoByID = function (id, clientID) {
SCDL.prototype.getTrackInfoByID = function (ids, clientID) {
return __awaiter(this, void 0, void 0, function () {
var _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
var _a;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = info_1.getTrackInfoByID;
_b = [id];
return [4 /*yield*/, this._assignClientID(clientID)];
case 1: return [4 /*yield*/, _a.apply(void 0, _b.concat([_c.sent()]))];
case 2: return [2 /*return*/, _c.sent()];
case 1: return [4 /*yield*/, _a.apply(void 0, [_b.sent(), ids])];
case 2: return [2 /*return*/, _b.sent()];
}
});
});
Expand All @@ -197,8 +196,7 @@ var SCDL = /** @class */ (function () {
* @param clientID - A Soundcloud Client ID, will find one if not provided
* @returns Info about the set
*/
SCDL.prototype.getSetInfo = function (url, full, clientID) {
if (full === void 0) { full = false; }
SCDL.prototype.getSetInfo = function (url, clientID) {
return __awaiter(this, void 0, void 0, function () {
var _a, _b;
return __generator(this, function (_c) {
Expand All @@ -207,25 +205,26 @@ var SCDL = /** @class */ (function () {
_a = info_1.getSetInfo;
_b = [url];
return [4 /*yield*/, this._assignClientID(clientID)];
case 1: return [2 /*return*/, _a.apply(void 0, _b.concat([_c.sent(), full]))];
case 1: return [2 /*return*/, _a.apply(void 0, _b.concat([_c.sent()]))];
}
});
});
};
/**
* Searches for tracks/playlists for the given query
* @param type - The type of resource, one of: 'tracks', 'people', 'albums', 'sets', 'all'
* @param query - The keywords for the search
* @param clientID - A Soundcloud Client ID, will find one if not provided
* @returns SearchResponse
*/
SCDL.prototype.search = function (query, clientID) {
SCDL.prototype.search = function (type, query, clientID) {
return __awaiter(this, void 0, void 0, function () {
var _a, _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0:
_a = search_1.search;
_b = [query];
_b = [type, query];
return [4 /*yield*/, this._assignClientID(clientID)];
case 1: return [2 /*return*/, _a.apply(void 0, _b.concat([_c.sent()]))];
}
Expand Down
65 changes: 21 additions & 44 deletions dist/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ exports.getTrackInfoByID = exports.getSetInfo = exports.getInfoBase = void 0;
var axios_1 = __importDefault(require("axios"));
var util_1 = require("./util");
/** @internal */
var getTrackInfoBase = function (trackID, clientID, axiosRef) { return __awaiter(void 0, void 0, void 0, function () {
var getTrackInfoBase = function (clientID, axiosRef, ids) { return __awaiter(void 0, void 0, void 0, function () {
var data, err_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, axiosRef.get("https://api-v2.soundcloud.com/tracks/" + trackID + "?client_id=" + clientID)];
return [4 /*yield*/, axiosRef.get("https://api-v2.soundcloud.com/tracks?ids=" + ids.join(',') + "&client_id=" + clientID)];
case 1:
data = (_a.sent()).data;
return [2 /*return*/, data];
Expand Down Expand Up @@ -83,40 +83,20 @@ exports.getInfoBase = function (url, clientID, axiosRef) { return __awaiter(void
});
}); };
/** @internal */
var getSetInfoBase = function (url, clientID, full, axiosRef) { return __awaiter(void 0, void 0, void 0, function () {
var setInfo, incompleteTracks, completeTracks, _i, incompleteTracks_1, track, info, err_3;
var getSetInfoBase = function (url, clientID, axiosRef) { return __awaiter(void 0, void 0, void 0, function () {
var setInfo, incompleteTracks, completeTracks, ids, info;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, exports.getInfoBase(url, clientID, axiosRef)];
case 1:
setInfo = _a.sent();
if (!full)
return [2 /*return*/, setInfo];
incompleteTracks = setInfo.tracks.filter(function (track) { return !track.title; });
completeTracks = setInfo.tracks.filter(function (track) { return track.title; });
_i = 0, incompleteTracks_1 = incompleteTracks;
_a.label = 2;
ids = incompleteTracks.map(function (t) { return t.id; });
return [4 /*yield*/, exports.getTrackInfoByID(clientID, ids)];
case 2:
if (!(_i < incompleteTracks_1.length)) return [3 /*break*/, 7];
track = incompleteTracks_1[_i];
_a.label = 3;
case 3:
_a.trys.push([3, 5, , 6]);
return [4 /*yield*/, getTrackInfoBase(track.id, clientID, axiosRef)];
case 4:
info = _a.sent();
completeTracks.push(info);
return [3 /*break*/, 6];
case 5:
err_3 = _a.sent();
console.log(err_3);
completeTracks.push(track);
return [3 /*break*/, 6];
case 6:
_i++;
return [3 /*break*/, 2];
case 7:
setInfo.tracks = completeTracks;
setInfo.tracks = completeTracks.concat(info);
return [2 /*return*/, setInfo];
}
});
Expand All @@ -136,27 +116,24 @@ var getInfo = function (url, clientID) { return __awaiter(void 0, void 0, void 0
});
}); };
/** @internal */
exports.getSetInfo = function (url, clientID, full) {
if (full === void 0) { full = false; }
return __awaiter(void 0, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, getSetInfoBase(url, clientID, full, axios_1["default"])];
case 1:
data = _a.sent();
if (!data.tracks)
throw new Error('The given URL does not link to a Soundcloud set');
return [2 /*return*/, data];
}
});
exports.getSetInfo = function (url, clientID) { return __awaiter(void 0, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, getSetInfoBase(url, clientID, axios_1["default"])];
case 1:
data = _a.sent();
if (!data.tracks)
throw new Error('The given URL does not link to a Soundcloud set');
return [2 /*return*/, data];
}
});
};
}); };
/** @intenral */
exports.getTrackInfoByID = function (id, clientID) { return __awaiter(void 0, void 0, void 0, function () {
exports.getTrackInfoByID = function (clientID, ids) { return __awaiter(void 0, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, getTrackInfoBase(id, clientID, axios_1["default"])];
case 0: return [4 /*yield*/, getTrackInfoBase(clientID, axios_1["default"], ids)];
case 1: return [2 /*return*/, _a.sent()];
}
});
Expand Down
22 changes: 19 additions & 3 deletions dist/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,36 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
exports.search = void 0;
exports.related = exports.search = void 0;
/* eslint-disable camelcase */
var axios_1 = __importDefault(require("axios"));
/** @internal */
var baseURL = 'https://api-v2.soundcloud.com/search';
/** @internal */
exports.search = function (query, clientID) { return __awaiter(void 0, void 0, void 0, function () {
exports.search = function (type, query, clientID) { return __awaiter(void 0, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, axios_1["default"].get(baseURL + "?client_id=" + clientID + "&q=" + query)];
case 0: return [4 /*yield*/, axios_1["default"].get("" + baseURL + (type === 'all' ? '' : "/" + type + "/") + "?client_id=" + clientID + "&q=" + query)];
case 1:
data = (_a.sent()).data;
return [2 /*return*/, data];
}
});
}); };
/** @internal */
exports.related = function (type, id, limit, offset, clientID) {
if (limit === void 0) { limit = 10; }
if (offset === void 0) { offset = 0; }
return __awaiter(void 0, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, axios_1["default"].get("https://api-v2.soundcloud.com/" + type + "/" + id + "/related?client_id=" + clientID + "&offset=" + offset + "&limit=" + limit)];
case 1:
data = (_a.sent()).data;
return [2 /*return*/, data];
}
});
});
};
19 changes: 10 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import isValidURL from './is-url'

import STREAMING_PROTOCOLS, { _PROTOCOLS } from './protocols'
import FORMATS, { _FORMATS } from './formats'
import { search } from './search'
import { search, SoundcloudResource } from './search'

/** @internal */
const download = async (url: string, clientID: string) => {
Expand Down Expand Up @@ -73,13 +73,13 @@ export class SCDL {
}

/**
* Returns info about a given track.
* @param id - The track ID
* Returns info about the given track(s) specified by ID.
* @param ids - The ID(s) of the tracks
* @param clientID - A Soundcloud Client ID, will find one if not provided
* @returns Info about the track
*/
async getTrackInfoByID (id: number, clientID?: string) {
return await getTrackInfoByID(id, await this._assignClientID(clientID))
async getTrackInfoByID (ids: number[], clientID?: string) {
return await getTrackInfoByID(await this._assignClientID(clientID), ids)
}

/**
Expand All @@ -89,18 +89,19 @@ export class SCDL {
* @param clientID - A Soundcloud Client ID, will find one if not provided
* @returns Info about the set
*/
async getSetInfo (url: string, full = false, clientID?: string) {
return getSetInfo(url, await this._assignClientID(clientID), full)
async getSetInfo (url: string, clientID?: string) {
return getSetInfo(url, await this._assignClientID(clientID))
}

/**
* Searches for tracks/playlists for the given query
* @param type - The type of resource, one of: 'tracks', 'people', 'albums', 'sets', 'all'
* @param query - The keywords for the search
* @param clientID - A Soundcloud Client ID, will find one if not provided
* @returns SearchResponse
*/
async search (query: string, clientID?: string) {
return search(query, await this._assignClientID(clientID))
async search (type: SoundcloudResource | 'all', query: string, clientID?: string) {
return search(type, query, await this._assignClientID(clientID))
}

/**
Expand Down
28 changes: 10 additions & 18 deletions src/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ export interface Transcoding {
}

/** @internal */
const getTrackInfoBase = async (trackID: number, clientID: string, axiosRef: AxiosInstance): Promise<TrackInfo> => {
const getTrackInfoBase = async (clientID: string, axiosRef: AxiosInstance, ids: number[]): Promise<TrackInfo> => {
try {
const { data } = await axiosRef.get(`https://api-v2.soundcloud.com/tracks/${trackID}?client_id=${clientID}`)
const { data } = await axiosRef.get(`https://api-v2.soundcloud.com/tracks?ids=${ids.join(',')}&client_id=${clientID}`)

return data as TrackInfo
} catch (err) {
Expand All @@ -141,24 +141,16 @@ export const getInfoBase = async <T extends TrackInfo | SetInfo>(url: string, cl
}

/** @internal */
const getSetInfoBase = async (url: string, clientID: string, full: boolean, axiosRef: AxiosInstance): Promise<SetInfo> => {
const getSetInfoBase = async (url: string, clientID: string, axiosRef: AxiosInstance): Promise<SetInfo> => {
const setInfo = await getInfoBase<SetInfo>(url, clientID, axiosRef)
if (!full) return setInfo

const incompleteTracks = setInfo.tracks.filter(track => !track.title)
const completeTracks = setInfo.tracks.filter(track => track.title)

for (const track of incompleteTracks) {
try {
const info = await getTrackInfoBase(track.id, clientID, axiosRef)
completeTracks.push(info)
} catch (err) {
console.log(err)
completeTracks.push(track)
}
}
const ids = incompleteTracks.map(t => t.id)
const info = await getTrackInfoByID(clientID, ids)

setInfo.tracks = completeTracks
setInfo.tracks = completeTracks.concat(info)
return setInfo
}

Expand All @@ -170,14 +162,14 @@ const getInfo = async (url: string, clientID: string): Promise<TrackInfo> => {
}

/** @internal */
export const getSetInfo = async (url: string, clientID: string, full = false): Promise<SetInfo> => {
const data = await getSetInfoBase(url, clientID, full, axios)
export const getSetInfo = async (url: string, clientID: string): Promise<SetInfo> => {
const data = await getSetInfoBase(url, clientID, axios)
if (!data.tracks) throw new Error('The given URL does not link to a Soundcloud set')
return data
}

/** @intenral */
export const getTrackInfoByID = async (id: number, clientID: string) => {
return await getTrackInfoBase(id, clientID, axios)
export const getTrackInfoByID = async (clientID: string, ids: number[]) => {
return await getTrackInfoBase(clientID, axios, ids)
}
export default getInfo
22 changes: 16 additions & 6 deletions src/search.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
/* eslint-disable camelcase */
import axios from 'axios'
import { TrackInfo } from './info'
import { TrackInfo, User, SetInfo } from './info'

/** @internal */
const baseURL = 'https://api-v2.soundcloud.com/search'

export type SearchResponse = {
collection: TrackInfo[]
export type SearchResponse<T> = {
collection: T[]
total_results: number,
next_href: string,
query_urn: string
}

export type SearchResponseAll = SearchResponse<User | SetInfo | TrackInfo>

export type SoundcloudResource = 'tracks' | 'people' | 'albums' | 'playlists'

/** @internal */
export const search = async (type: SoundcloudResource | 'all', query: string, clientID?: string): Promise<SearchResponseAll> => {
const { data } = await axios.get(`${baseURL}${type === 'all' ? '' : `/${type}/`}?client_id=${clientID}&q=${query}`)
return data as SearchResponseAll
}

/** @internal */
export const search = async (query: string, clientID?: string): Promise<SearchResponse> => {
const { data } = await axios.get(`${baseURL}?client_id=${clientID}&q=${query}`)
return data as SearchResponse
export const related = async <T extends TrackInfo | User | SetInfo> (type: SoundcloudResource, id: number, limit = 10, offset = 0, clientID: string): Promise<SearchResponse<T>> => {
const { data } = await axios.get(`https://api-v2.soundcloud.com/${type}/${id}/related?client_id=${clientID}&offset=${offset}&limit=${limit}`)
return data as SearchResponse<T>
}
Loading

0 comments on commit 84d8d9a

Please sign in to comment.