Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

soundcloud.ts

Package Overview
Dependencies
Maintainers
1
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

soundcloud.ts - npm Package Compare versions

Comparing version 0.0.3 to 0.0.4

2

API.ts

@@ -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.

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc