Skip to content

Commit

Permalink
fix: static client id
Browse files Browse the repository at this point in the history
  • Loading branch information
kaopiz-thanhvc committed Jun 28, 2022
1 parent bb15a65 commit d170f2d
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 105 deletions.
46 changes: 16 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,31 @@
const fs = require("fs");
const { SoundCloud } = require("scdl-core");

const scdl = await SoundCloud.create();
const stream = await scdl.download(
await SoundCloud.connect();
const stream = await SoundCloud.download(
"https://soundcloud.com/martingarrix/martin-garrix-feat-bonn-no-sleep"
);
stream.pipe(fs.createWriteStream("song.mp3"));
```

# API

## Instantiate scdl object
## connect

```js
// 2 ways to instantiate scdl object

// #1
// Used to get the SoundCloud client_id. Call 1 time at the top of your app.
const { SoundCloud } = require("scdl-core");
const scdl = await SoundCloud.create();
const stream = await scdl.download(
await SoundCloud.connect();
await SoundCloud.download(
"https://soundcloud.com/martingarrix/martin-garrix-feat-bonn-no-sleep"
);
stream.pipe(fs.createWriteStream("song.mp3"));

// #2
const { SoundCloud } = require("scdl-core");
const scdl = new SoundCloud();
// Only need to call "connect" method once on initialization
scdl.connect().then(() => {
// Do something
const stream = await scdl.download(
"https://soundcloud.com/martingarrix/martin-garrix-feat-bonn-no-sleep"
);
stream.pipe(fs.createWriteStream("song.mp3"));
});
```

## search

```js
const result = await scdl.search({
const result = await SoundCloud.search({
query: string,
limit?: number, // Default: 20
offset?: number, // Default: 0
Expand All @@ -69,21 +55,21 @@ const result = await scdl.search({

```js
const ids = [578933490, 499766382];
const tracks = await scdl.tracks.getTracksByIds(ids);
const tracks = await SoundCloud.tracks.getTracksByIds(ids);
```

#### getTrack

```js
const permalink =
"https://soundcloud.com/martingarrix/martin-garrix-feat-bonn-no-sleep";
const track = await scdl.tracks.getTrack(permalink);
const track = await SoundCloud.tracks.getTrack(permalink);
```

#### getTrending

```js
const trendingTracks = await scdl.tracks.getTrending({
const trendingTracks = await SoundCloud.tracks.getTrending({
limit?: number, // Default: 20
offset?: number // Default: 0
});
Expand All @@ -96,7 +82,7 @@ const trendingTracks = await scdl.tracks.getTrending({
```js
const permalink =
"https://soundcloud.com/martingarrix/sets/martin-garrix-matisse-sadko";
const playlist = await scdl.playlists.getPlaylist(permalink);
const playlist = await SoundCloud.playlists.getPlaylist(permalink);
```

## users
Expand All @@ -105,20 +91,20 @@ const playlist = await scdl.playlists.getPlaylist(permalink);

```js
const permalink = "https://soundcloud.com/martingarrix";
const user = await scdl.users.getUser(permalink);
const user = await SoundCloud.users.getUser(permalink);
```

## download

```js
const permalink =
"https://soundcloud.com/martingarrix/martin-garrix-feat-bonn-no-sleep";
const stream = await scdl.download(permalink);
const stream = await SoundCloud.download(permalink);
stream.pipe(fs.createWriteStream("song.mp3"));

// For streaming, you can customize the `highWaterMark` value to reduce lag if the internet is not good.
// Example:
const stream = await scdl.download(permalink, {
const stream = await SoundCloud.download(permalink, {
highWaterMark: 1 << 25, // 32Mb, default is 16kb
});
```
Expand All @@ -131,7 +117,7 @@ const voiceChannel = message.member.voiceChannel;
voiceChannel
.join()
.then((connection) => {
scdl.download(trackPermalink).then((stream) => {
SoundCloud.download(trackPermalink).then((stream) => {
connection.play(stream);
});
})
Expand All @@ -147,7 +133,7 @@ const voiceConnection = joinVoiceChannel({
adapterCreator,
});
voiceConnection.subscribe(audioPlayer);
const stream = await scdl.download(SONG_URL);
const stream = await SoundCloud.download(SONG_URL);
const audioResource = createAudioResource(stream);
audioPlayer.play(audioResource);
```
Expand Down
28 changes: 18 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scdl-core",
"version": "1.0.20",
"version": "1.1.0",
"description": "SoundCloud downloader",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
54 changes: 22 additions & 32 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,73 +12,63 @@ import { getTrack, getTracksByIds, getTrending } from "./services/tracks";
import { getUser } from "./services/user";

export class SoundCloud {
private clientId: string;
private static clientId: string | null = null;

constructor() {
this.clientId = "";
}

public static async create(): Promise<SoundCloud> {
const scdl = new SoundCloud();
await scdl.connect();
return scdl;
}

public connect = async (): Promise<void> => {
public static connect = async (): Promise<void> => {
const clientId = await getClientId();
this.clientId = clientId;
SoundCloud.clientId = clientId;
};

public search = async (
public static search = async (
searchOptions: SearchOptions
): Promise<SearchResponse> => {
this.checkClientId();
return search(this.clientId, searchOptions);
return search(SoundCloud.clientId as string, searchOptions);
};

public users = {
public static users = {
getUser: async (url: string): Promise<User> => {
this.checkClientId();
return await getUser(this.clientId, url);
return await getUser(SoundCloud.clientId as string, url);
},
};

public tracks = {
public static tracks = {
getTracksByIds: async (ids: number[]): Promise<Track[]> => {
this.checkClientId();
return await getTracksByIds(this.clientId, ids);
SoundCloud.checkClientId();
return await getTracksByIds(SoundCloud.clientId as string, ids);
},

getTrack: async (url: string): Promise<Track> => {
this.checkClientId();
return await getTrack(this.clientId, url);
SoundCloud.checkClientId();
return await getTrack(SoundCloud.clientId as string, url);
},

getTrending: async (
options?: TrendingOptions
): Promise<TrendingTrackResponse> => {
this.checkClientId();
return await getTrending(this.clientId, options);
SoundCloud.checkClientId();
return await getTrending(SoundCloud.clientId as string, options);
},
};

public playlists = {
public static playlists = {
getPlaylist: async (url: string): Promise<Playlist> => {
this.checkClientId();
return await getPlaylist(this.clientId, url);
SoundCloud.checkClientId();
return await getPlaylist(SoundCloud.clientId as string, url);
},
};

public download = async (
public static download = async (
url: string,
downloadOptions?: DownloadOptions
): Promise<m3u8stream.Stream> => {
this.checkClientId();
return await download(this.clientId, url, downloadOptions);
SoundCloud.checkClientId();
return await download(SoundCloud.clientId as string, url, downloadOptions);
};

private checkClientId() {
if (!this.clientId)
private static checkClientId() {
if (!SoundCloud.clientId)
throw Error("Require client_id. Run .connect() firstly");
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/create.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ describe("Create", () => {
const limit = 5;

it("create a valid SoundCloud object that can return result when search", async () => {
const soundCloud = await SoundCloud.create();
const result = await soundCloud.search({
await SoundCloud.connect();
const result = await SoundCloud.search({
query,
limit,
});
Expand Down
6 changes: 2 additions & 4 deletions test/download.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ const concat = (stream, callback) => {

describe("Download tracks", () => {
it("Stream is readable", async () => {
const scdl = new SoundCloud();
await scdl.connect();

await SoundCloud.connect();
const permalink =
"https://soundcloud.com/martingarrix/martin-garrix-feat-bonn-no-sleep";
const stream = await scdl.download(permalink);
const stream = await SoundCloud.download(permalink);
concat(stream, (err, body) => {
expect(err).toBeNull();
expect(Boolean(body)).toEqual(true);
Expand Down
10 changes: 3 additions & 7 deletions test/playlist-album.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ const { SoundCloud } = require("../dist");

describe("Get playlist/album", () => {
it("Returns a playlist/album with valid url", async () => {
const scdl = new SoundCloud();
await scdl.connect();

await SoundCloud.connect();
const permalink =
// "https://soundcloud.com/martingarrix/sets/martin-garrix-matisse-sadko";
"https://soundcloud.com/sertac-sayrin/sets/pisiko-trap-elektro";
const playlist = await scdl.playlists.getPlaylist(permalink);
console.log(playlist.tracks.length);
"https://soundcloud.com/martingarrix/sets/martin-garrix-matisse-sadko";
const playlist = await SoundCloud.playlists.getPlaylist(permalink);
expect(playlist.permalink_url).toEqual(permalink);
});
});
6 changes: 2 additions & 4 deletions test/search.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ describe("Connect", () => {
const limit = 5;

it("Returns a valid object with a maximum collection length of <limit>", async () => {
const soundCloud = new SoundCloud();
await soundCloud.connect();

await SoundCloud.connect();
for (const filter of filters) {
const result = await soundCloud.search({
const result = await SoundCloud.search({
query,
filter,
limit,
Expand Down
17 changes: 6 additions & 11 deletions test/track.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ const { SoundCloud } = require("../dist");

describe("Get tracks by ids", () => {
it("Returns an array of objects with a maximum length of <ids.length> and tracks[0] has valid url", async () => {
const scdl = new SoundCloud();
await scdl.connect();

await SoundCloud.connect();
const ids = [578933490, 499766382];
const tracks = await scdl.tracks.getTracksByIds(ids);
const tracks = await SoundCloud.tracks.getTracksByIds(ids);
expect(tracks.length).toBeLessThanOrEqual(ids.length);
expect(tracks[0].permalink_url).toEqual(
"https://soundcloud.com/martingarrix/martin-garrix-feat-bonn-no-sleep"
Expand All @@ -19,20 +17,17 @@ describe("Get track by permalink", () => {
"https://soundcloud.com/martingarrix/martin-garrix-feat-bonn-no-sleep";

it("Returns a track with valid url", async () => {
const scdl = new SoundCloud();
await scdl.connect();
const track = await scdl.tracks.getTrack(permalink);
await SoundCloud.connect();
const track = await SoundCloud.tracks.getTrack(permalink);
expect(track.permalink_url).toEqual(permalink);
});
});

describe("Get trending tracks", () => {
it("Returns a valid object with a maximum collection length of <limit>", async () => {
const scdl = new SoundCloud();
await scdl.connect();

await SoundCloud.connect();
const limit = 10;
const tracks = await scdl.tracks.getTrending({
const tracks = await SoundCloud.tracks.getTrending({
limit: limit,
});
expect(tracks.collection.length).toEqual(limit);
Expand Down
6 changes: 2 additions & 4 deletions test/users.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ const { SoundCloud } = require("../dist");

describe("Get user", () => {
it("Returns a user with valid url", async () => {
const scdl = new SoundCloud();
await scdl.connect();

await SoundCloud.connect();
const permalink = "https://soundcloud.com/martingarrix";
const user = await scdl.users.getUser(permalink);
const user = await SoundCloud.users.getUser(permalink);
expect(user.permalink_url).toEqual(permalink);
});
});

0 comments on commit d170f2d

Please sign in to comment.