@zibot/scdl 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.d.ts +60 -65
  2. package/index.js +20 -8
  3. package/package.json +2 -1
package/index.d.ts CHANGED
@@ -1,78 +1,73 @@
1
- declare module "@zibot/scdl" {
2
- import { Readable } from "stream";
1
+ import { Readable } from "stream";
3
2
 
4
- // Types
5
- export interface SearchOptions {
6
- query: string;
7
- limit?: number;
8
- offset?: number;
9
- type?: "all" | "tracks" | "playlists" | "users";
10
- }
11
-
12
- export interface DownloadOptions {
13
- quality?: "high" | "low";
14
- }
3
+ // Types
4
+ interface SearchOptions {
5
+ query: string;
6
+ limit?: number;
7
+ offset?: number;
8
+ type?: "all" | "tracks" | "playlists" | "users";
9
+ }
15
10
 
16
- export interface Track {
17
- id: number;
18
- title: string;
19
- url: string;
20
- user: { id: number; username: string };
21
- media: {
22
- transcodings: {
23
- url: string;
24
- format: { protocol: string; mime_type: string };
25
- }[];
26
- };
27
- }
11
+ interface DownloadOptions {
12
+ quality?: "high" | "low";
13
+ }
28
14
 
29
- export interface Playlist {
30
- id: number;
31
- title: string;
32
- tracks: Track[];
33
- }
15
+ interface Track {
16
+ id: number;
17
+ title: string;
18
+ url: string;
19
+ user: { id: number; username: string };
20
+ media: {
21
+ transcodings: {
22
+ url: string;
23
+ format: { protocol: string; mime_type: string };
24
+ }[];
25
+ };
26
+ }
34
27
 
35
- export interface User {
36
- id: number;
37
- username: string;
38
- followers_count: number;
39
- track_count: number;
40
- }
28
+ interface Playlist {
29
+ id: number;
30
+ title: string;
31
+ tracks: Track[];
32
+ }
41
33
 
42
- export interface SearchResponse {
43
- collection: (Track | Playlist | User)[];
44
- next_href?: string;
45
- }
34
+ interface User {
35
+ id: number;
36
+ username: string;
37
+ followers_count: number;
38
+ track_count: number;
39
+ }
46
40
 
47
- export class SoundCloud {
48
- clientId: string | null;
49
- apiBaseUrl: string;
41
+ declare class SoundCloud {
42
+ clientId: string | null;
43
+ apiBaseUrl: string;
50
44
 
51
- constructor(options?: { init?: boolean });
45
+ constructor(options?: { init?: boolean });
52
46
 
53
- /**
54
- * Initialize the SoundCloud client to retrieve clientId.
55
- */
56
- init(): Promise<void>;
47
+ /**
48
+ * Initialize the SoundCloud client to retrieve clientId.
49
+ */
50
+ init(): Promise<void>;
57
51
 
58
- /**
59
- * Search for tracks, playlists, or users on SoundCloud.
60
- */
61
- searchTracks(options: SearchOptions): Promise<SearchResponse>;
52
+ /**
53
+ * Search for tracks, playlists, or users on SoundCloud.
54
+ */
55
+ searchTracks(options: SearchOptions): Promise<(Track | Playlist | User)[]>;
62
56
 
63
- /**
64
- * Retrieve detailed information about a single track.
65
- */
66
- getTrack(url: string): Promise<Track>;
57
+ /**
58
+ * Retrieve detailed information about a single track.
59
+ */
60
+ getTrackDetails(url: string): Promise<Track>;
67
61
 
68
- /**
69
- * Retrieve detailed information about a playlist.
70
- */
71
- getPlaylist(url: string): Promise<Playlist>;
62
+ /**
63
+ * Retrieve detailed information about a playlist.
64
+ */
65
+ getPlaylistDetails(url: string): Promise<Playlist>;
72
66
 
73
- /**
74
- * Download a track as a stream.
75
- */
76
- downloadTrack(url: string, options?: DownloadOptions): Promise<Readable>;
77
- }
67
+ /**
68
+ * Download a track as a stream.
69
+ */
70
+ downloadTrack(url: string, options?: DownloadOptions): Promise<Readable>;
78
71
  }
72
+
73
+ export = SoundCloud;
package/index.js CHANGED
@@ -31,15 +31,22 @@ class SoundCloud {
31
31
  }
32
32
 
33
33
  // Search SoundCloud
34
- async searchTracks({ query, limit = 20, offset = 0, type = "all" }) {
34
+ async searchTracks({ query, limit = 30, offset = 0, type = "all" }) {
35
35
  const path = type === "all" ? "" : `/${type}`;
36
36
  const url = `${this.apiBaseUrl}/search${path}?q=${encodeURIComponent(
37
37
  query,
38
38
  )}&limit=${limit}&offset=${offset}&access=playable&client_id=${this.clientId}`;
39
- console.log(url);
40
39
  try {
41
40
  const { data } = await axios.get(url);
42
- return data;
41
+
42
+ if (!data || !data?.collection?.length) {
43
+ return [];
44
+ }
45
+
46
+ return data.collection.filter((track) => {
47
+ if (!track.permalink_url || !track.title || !track.duration) return false;
48
+ return true;
49
+ });
43
50
  } catch (error) {
44
51
  console.error("Search error:", error.message || error);
45
52
  throw new Error("Search failed");
@@ -77,13 +84,18 @@ class SoundCloud {
77
84
 
78
85
  // Download track stream
79
86
  async downloadTrack(trackUrl, options = {}) {
80
- const track = await this.getTrackDetails(trackUrl);
81
- const transcoding = track.media.transcodings.find((t) => t.format.protocol === "hls");
87
+ try {
88
+ const track = await this.getTrackDetails(trackUrl);
89
+ const transcoding = track?.media?.transcodings?.find((t) => t.format.protocol === "hls");
82
90
 
83
- if (!transcoding) throw new Error("No valid HLS stream found");
91
+ if (!transcoding) throw new Error("No valid HLS stream found");
84
92
 
85
- const m3u8Url = await this.getStreamUrl(transcoding.url);
86
- return m3u8stream(m3u8Url, options);
93
+ const m3u8Url = await this.getStreamUrl(transcoding.url);
94
+ return m3u8stream(m3u8Url, options);
95
+ } catch (e) {
96
+ console.error("Failed to download track");
97
+ return null;
98
+ }
87
99
  }
88
100
 
89
101
  // Fetch single item (track/playlist/user)
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@zibot/scdl",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "Soucloud download",
5
5
  "main": "index.js",
6
+ "types": "index.d.ts",
6
7
  "scripts": {
7
8
  "test": "echo \"Error: no test specified\" && exit 1"
8
9
  },