soundcloud.ts
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -7,3 +7,3 @@ import axios from "axios" | ||
export default class API { | ||
public constructor(private readonly clientID: string, private readonly oauthToken: string) {} | ||
public constructor(public clientID: string, public oauthToken: string) {} | ||
@@ -10,0 +10,0 @@ /** |
export default class API { | ||
private readonly clientID; | ||
private readonly oauthToken; | ||
clientID: string; | ||
oauthToken: string; | ||
constructor(clientID: string, oauthToken: string); | ||
@@ -5,0 +5,0 @@ /** |
@@ -7,2 +7,4 @@ /// <reference types="node" /> | ||
private readonly api; | ||
private readonly playlists; | ||
private readonly users; | ||
private readonly tracks; | ||
@@ -15,2 +17,6 @@ constructor(api: api); | ||
downloadTrack: (trackResolvable: string | number | SoundCloudTrack, folder?: string) => Promise<never>; | ||
downloadTracks: (tracks: SoundCloudTrack[], dest?: string) => void; | ||
downloadSearch: (query: string, dest?: string) => Promise<void>; | ||
downloadFavorites: (userResolvable: string | number, dest?: string) => Promise<void>; | ||
downloadPlaylist: (playlistResolvable: string | number, dest?: string) => Promise<void>; | ||
} |
@@ -39,2 +39,3 @@ "use strict"; | ||
exports.__esModule = true; | ||
var axios_1 = require("axios"); | ||
var fs = require("fs"); | ||
@@ -47,2 +48,4 @@ var path = require("path"); | ||
this.api = api; | ||
this.playlists = new index_1.Playlists(this.api); | ||
this.users = new index_1.Users(this.api); | ||
this.tracks = new index_1.Tracks(this.api); | ||
@@ -61,3 +64,3 @@ /** | ||
this.downloadTrack = function (trackResolvable, folder) { return __awaiter(_this, void 0, void 0, function () { | ||
var track; | ||
var track, result, dest; | ||
return __generator(this, function (_a) { | ||
@@ -79,15 +82,7 @@ switch (_a.label) { | ||
fs.mkdirSync(folder); | ||
return [4 /*yield*/, this.api.getURI(track.download_url, { responseType: "arraybuffer" }) | ||
.then(function (r) { | ||
var dest; | ||
if (r.headers["content-type"] === "audio/x-wav") { | ||
dest = path.join(folder, track.title + ".wav"); | ||
} | ||
else { | ||
dest = path.join(folder, track.title + ".mp3"); | ||
} | ||
fs.writeFileSync(dest, r.data); | ||
})]; | ||
return [4 /*yield*/, axios_1["default"].get(track.download_url, { responseType: "arraybuffer", params: { client_id: this.api.clientID, oauth_token: this.api.oauthToken } })]; | ||
case 4: | ||
_a.sent(); | ||
result = _a.sent(); | ||
dest = path.join(folder, track.title + "." + result.headers["x-amz-meta-file-type"]); | ||
fs.writeFileSync(dest, Buffer.from(result.data, "binary")); | ||
return [3 /*break*/, 6]; | ||
@@ -99,2 +94,48 @@ case 5: return [2 /*return*/, Promise.reject("This track doesn't have downloads enabled.")]; | ||
}); }; | ||
this.downloadTracks = function (tracks, dest) { | ||
for (var i = 0; i < tracks.length; i++) { | ||
try { | ||
_this.downloadTrack(tracks[i], dest); | ||
} | ||
catch (_a) { | ||
continue; | ||
} | ||
} | ||
}; | ||
this.downloadSearch = function (query, dest) { return __awaiter(_this, void 0, void 0, function () { | ||
var tracks; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.tracks.search({ q: query })]; | ||
case 1: | ||
tracks = _a.sent(); | ||
this.downloadTracks(tracks, dest); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; | ||
this.downloadFavorites = function (userResolvable, dest) { return __awaiter(_this, void 0, void 0, function () { | ||
var tracks; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.users.favorites(userResolvable)]; | ||
case 1: | ||
tracks = _a.sent(); | ||
this.downloadTracks(tracks, dest); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; | ||
this.downloadPlaylist = function (playlistResolvable, dest) { return __awaiter(_this, void 0, void 0, function () { | ||
var playlist; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.playlists.get(playlistResolvable)]; | ||
case 1: | ||
playlist = _a.sent(); | ||
this.downloadTracks(playlist.tracks); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; | ||
} | ||
@@ -101,0 +142,0 @@ return Util; |
@@ -47,5 +47,7 @@ "use strict"; | ||
// const result = await soundcloud.tracks.search({q: "virtual riot"}) | ||
return [4 /*yield*/, soundcloud.util.downloadTrack("https://soundcloud.com/tenpimusic/starstruck", "./tracks")]; | ||
// await soundcloud.util.downloadTrack("https://soundcloud.com/tenpimusic/snowflake", "./tracks") | ||
return [4 /*yield*/, soundcloud.util.downloadPlaylist("tenpi", "./tracks/tenpi-tracks")]; | ||
case 1: | ||
// const result = await soundcloud.tracks.search({q: "virtual riot"}) | ||
// await soundcloud.util.downloadTrack("https://soundcloud.com/tenpimusic/snowflake", "./tracks") | ||
_a.sent(); | ||
@@ -52,0 +54,0 @@ return [2 /*return*/]; |
@@ -0,1 +1,2 @@ | ||
import axios from "axios" | ||
import * as fs from "fs" | ||
@@ -6,5 +7,7 @@ import * as path from "path" | ||
import {SoundCloudTrack} from "../types" | ||
import {Tracks} from "./index" | ||
import {Playlists, Tracks, Users} from "./index" | ||
export class Util { | ||
private readonly playlists = new Playlists(this.api) | ||
private readonly users = new Users(this.api) | ||
private readonly tracks = new Tracks(this.api) | ||
@@ -33,12 +36,5 @@ constructor(private readonly api: api) {} | ||
if (!fs.existsSync(folder)) fs.mkdirSync(folder) | ||
await this.api.getURI(track.download_url, {responseType: "arraybuffer"}) | ||
.then((r) => { | ||
let dest: string | ||
if (r.headers["content-type"] === "audio/x-wav") { | ||
dest = path.join(folder, `${track.title}.wav`) | ||
} else { | ||
dest = path.join(folder, `${track.title}.mp3`) | ||
} | ||
fs.writeFileSync(dest, r.data) | ||
}) | ||
const result = await axios.get(track.download_url, {responseType: "arraybuffer", params: {client_id: this.api.clientID, oauth_token: this.api.oauthToken}}) | ||
const dest = path.join(folder, `${track.title}.${result.headers["x-amz-meta-file-type"]}`) | ||
fs.writeFileSync(dest, Buffer.from(result.data, "binary")) | ||
} else { | ||
@@ -48,2 +44,27 @@ return Promise.reject("This track doesn't have downloads enabled.") | ||
} | ||
public downloadTracks = (tracks: SoundCloudTrack[], dest?: string) => { | ||
for (let i = 0; i < tracks.length; i++) { | ||
try { | ||
this.downloadTrack(tracks[i], dest) | ||
} catch { | ||
continue | ||
} | ||
} | ||
} | ||
public downloadSearch = async (query: string, dest?: string) => { | ||
const tracks = await this.tracks.search({q: query}) | ||
this.downloadTracks(tracks, dest) | ||
} | ||
public downloadFavorites = async (userResolvable: string | number, dest?: string) => { | ||
const tracks = await this.users.favorites(userResolvable) | ||
this.downloadTracks(tracks, dest) | ||
} | ||
public downloadPlaylist = async (playlistResolvable: string | number, dest?: string) => { | ||
const playlist = await this.playlists.get(playlistResolvable) | ||
this.downloadTracks(playlist.tracks) | ||
} | ||
} |
@@ -7,3 +7,4 @@ import SoundCloud from "./Soundcloud" | ||
// const result = await soundcloud.tracks.search({q: "virtual riot"}) | ||
await soundcloud.util.downloadTrack("https://soundcloud.com/tenpimusic/starstruck", "./tracks") | ||
// await soundcloud.util.downloadTrack("https://soundcloud.com/tenpimusic/snowflake", "./tracks") | ||
await soundcloud.util.downloadPlaylist("tenpi", "./tracks/tenpi-tracks") | ||
})() |
{ | ||
"name": "soundcloud.ts", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Wrapper for the Soundcloud API with typings", | ||
@@ -39,4 +39,5 @@ "main": "dist/soundcloud.js", | ||
"dependencies": { | ||
"axios": "^0.19.0" | ||
"axios": "^0.19.0", | ||
"request": "^2.88.0" | ||
} | ||
} |
@@ -65,3 +65,3 @@ <div align="left"> | ||
const user = await soundcloud.users.get("https://soundcloud.com/tenpimusic") | ||
const userSearch = await soundcloud.users.get({q: "some user"}) | ||
const userSearch = await soundcloud.users.search({q: "some user"}) | ||
@@ -86,2 +86,20 @@ /*You can get the followers and following of a user.*/ | ||
``` | ||
#### Downloading tracks | ||
```ts | ||
async function useAPI() { | ||
/*You can download a track as long as it has downloads enabled on Soundcloud.*/ | ||
await soundcloud.util.downloadTrack("https://soundcloud.com/tenpimusic/snowflake", "./tracks") | ||
/*You can download multiple tracks by passing them as an array to downloadTracks(). All tracks | ||
without downloads enabled will get skipped.*/ | ||
const tracks = await soundcloud.tracks.search({q: "cool track"}) | ||
await soundcloud.util.downloadTracks(tracks, "./tracks") | ||
/*In addition, there are a bunch of utilities that do the above automatically for convenience.*/ | ||
await soundcloud.util.downloadSearch("cool track", "./tracks") | ||
await soundcloud.util.downloadFavorites("tenpimusic", "./tracks") | ||
await soundcloud.util.downloadPlaylist("https://soundcloud.com/tenpimusic/sets/my-songs", "./tracks") | ||
} | ||
``` | ||
### Other Endpoints | ||
@@ -88,0 +106,0 @@ There are more less commonly used endpoints such as **me**, **apps**, and **oembed**. Refer to the [**SoundCloud API Documentation**](https://developers.soundcloud.com/docs/api/reference) for their usage. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
180198
3801
282
2
+ Addedrequest@^2.88.0
+ Addedajv@6.12.6(transitive)
+ Addedasn1@0.2.6(transitive)
+ Addedassert-plus@1.0.0(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaws-sign2@0.7.0(transitive)
+ Addedaws4@1.13.2(transitive)
+ Addedbcrypt-pbkdf@1.0.2(transitive)
+ Addedcaseless@0.12.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedcore-util-is@1.0.2(transitive)
+ Addeddashdash@1.14.1(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedecc-jsbn@0.1.2(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedextsprintf@1.3.0(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedforever-agent@0.6.1(transitive)
+ Addedform-data@2.3.3(transitive)
+ Addedgetpass@0.1.7(transitive)
+ Addedhar-schema@2.0.0(transitive)
+ Addedhar-validator@5.1.5(transitive)
+ Addedhttp-signature@1.2.0(transitive)
+ Addedis-typedarray@1.0.0(transitive)
+ Addedisstream@0.1.2(transitive)
+ Addedjsbn@0.1.1(transitive)
+ Addedjson-schema@0.4.0(transitive)
+ Addedjson-schema-traverse@0.4.1(transitive)
+ Addedjson-stringify-safe@5.0.1(transitive)
+ Addedjsprim@1.4.2(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedoauth-sign@0.9.0(transitive)
+ Addedperformance-now@2.1.0(transitive)
+ Addedpsl@1.10.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedqs@6.5.3(transitive)
+ Addedrequest@2.88.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsshpk@1.18.0(transitive)
+ Addedtough-cookie@2.5.0(transitive)
+ Addedtunnel-agent@0.6.0(transitive)
+ Addedtweetnacl@0.14.5(transitive)
+ Addeduri-js@4.4.1(transitive)
+ Addeduuid@3.4.0(transitive)
+ Addedverror@1.10.0(transitive)