This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
515 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
SOUNDCLOUD_CLIENT_ID_V1= | ||
SOUNDCLOUD_CLIENT_ID_V2= | ||
SOUNDCLOUD_OAUTH_TOKEN= | ||
SOUNDCLOUD_OAUTH_TOKEN= | ||
|
||
EXAMPLE_PLAYLIST_ID=620756469 | ||
EXAMPLE_PLAYLIST_URL=https://soundcloud.com/vaaski/sets/rrrrrrrrr | ||
EXAMPLE_USER_ID=51999125 | ||
EXAMPLE_USER_URL=https://soundcloud.com/vaaski | ||
EXAMPLE_TRACK_ID=734769850 | ||
EXAMPLE_TRACK_URL=https://soundcloud.com/vaaski/slow-night |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,18 @@ | ||
{ | ||
"cSpell.words": [ | ||
"Playlistv", | ||
"lcov", | ||
"sndcdn", | ||
"urlify" | ||
], | ||
"cSpell.ignorePaths": [ | ||
"**/package-lock.json", | ||
"**/node_modules/**", | ||
"**/vscode-extension/**", | ||
"**/.git/objects/**", | ||
".vscode", | ||
".vscode-insiders", | ||
"demo_out.json", | ||
"*.d.ts" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import resolve from "./resolve" | ||
import playlist from "./playlist" | ||
|
||
export default { resolve } | ||
export { resolve } | ||
export default { resolve, playlist } | ||
export { resolve, playlist } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type { ClientIDv2, URLorID } from "../types" | ||
import type { Playlistv2 } from "../types/playlist" | ||
|
||
import ky from "ky-universal" | ||
import { APIv2, getClientIDv2, scrapeData, ScrapeIDs, urlify } from "./util" | ||
|
||
/** | ||
* Get a playlist using the APIv2 with a playlist ID | ||
* @param id A playlist ID | ||
* @param client_id client_id for APIv2 | ||
*/ | ||
const byID = async (id: number, client_id: ClientIDv2): Promise<Playlistv2> => { | ||
const url = urlify(`playlists/${id}`, APIv2) | ||
const searchParams = { client_id } | ||
|
||
const data = await ky(url, { searchParams }).json() | ||
return data as Playlistv2 | ||
} | ||
|
||
/** | ||
* Get a playlist using the APIv2 with a playlist URL | ||
* @param url A playlist URL | ||
*/ | ||
const byURL = async (url: string): Promise<Playlistv2> => { | ||
const scraped = await scrapeData(urlify(url)) | ||
const playlistData = scraped.find(({ id }) => id === ScrapeIDs.playlist) | ||
if (!playlistData) throw new Error("No playlist data found.") | ||
|
||
const { data } = playlistData | ||
return data[0] as Playlistv2 | ||
} | ||
|
||
/** | ||
* Get a playlist using the APIv2 with either a playlist URL or its ID. | ||
* | ||
* If you use a playlist ID, you can provide a v2 client_id (recommended). | ||
* | ||
* Uses `util.getClientIDv2` to find a client_id if none is provided. | ||
* @param source A playlist URL or ID | ||
* @param client_id Optional. | ||
*/ | ||
const playlist = async (source: URLorID, client_id?: string): Promise<Playlistv2> => { | ||
if (typeof source === "string") return await byURL(source) | ||
|
||
if (typeof source === "number") { | ||
if (!client_id) client_id = await getClientIDv2() | ||
return await byID(source, client_id) | ||
} | ||
|
||
throw new Error("Source must be a string (URL) or a number (ID)") | ||
} | ||
|
||
playlist.id = byID | ||
playlist.url = byURL | ||
|
||
// TODO browser equivalents with pagination | ||
|
||
export default playlist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import test from "ava" | ||
|
||
import { playlist } from "../src" | ||
import { getClientIDv2 } from "../src/util" | ||
|
||
const examplePlaylistURL = process.env.EXAMPLE_PLAYLIST_URL || "" | ||
const examplePlaylistID = Number(process.env.EXAMPLE_PLAYLIST_ID) || 0 | ||
|
||
let client_id2: string | ||
|
||
test.before(async () => { | ||
client_id2 = await getClientIDv2() | ||
}) | ||
|
||
test("get playlist using URL", async t => { | ||
const data = await playlist(examplePlaylistURL) | ||
t.is(data.kind, "playlist") | ||
t.truthy(data.id) | ||
}) | ||
|
||
test("get playlist using ID", async t => { | ||
const data = await playlist(examplePlaylistID) | ||
t.is(data.kind, "playlist") | ||
t.truthy(data.id) | ||
}) | ||
|
||
test("get playlist using ID and client_id", async t => { | ||
const data = await playlist(examplePlaylistID, client_id2) | ||
t.is(data.kind, "playlist") | ||
t.truthy(data.id) | ||
}) | ||
|
||
test("get playlist throws using wrong input", async t => { | ||
// @ts-expect-error intentionally wrong input | ||
await t.throwsAsync(playlist([])) | ||
}) | ||
|
||
test("get playlist by URL throws when not found", async t => { | ||
await t.throwsAsync(playlist("")) | ||
}) | ||
|
||
test("get playlist by ID throws when not found", async t => { | ||
await t.throwsAsync(playlist(0, client_id2)) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,56 @@ | ||
import test from "ava" | ||
|
||
import resolve from "../src/resolve" | ||
import { resolve } from "../src" | ||
|
||
const client_id = process.env.SOUNDCLOUD_CLIENT_ID_V1 || "client_id" | ||
|
||
// const examplePlaylist = "https://soundcloud.com/vaaski/sets/rrrrrrrrr" | ||
const exampleUser = "https://soundcloud.com/vaaski" | ||
// const exampleTrack = "https://soundcloud.com/vaaski/slow-night" | ||
const examplePlaylistURL = process.env.EXAMPLE_PLAYLIST_URL || "" | ||
const exampleUserURL = process.env.EXAMPLE_USER_URL || "" | ||
const exampleTrackURL = process.env.EXAMPLE_TRACK_URL || "" | ||
|
||
test("resolve user with scraping", async t => { | ||
const data = await resolve(exampleUser) | ||
test("resolve user", async t => { | ||
const data = await resolve(exampleUserURL) | ||
t.is(data.kind, "user") | ||
t.truthy(data.username) | ||
t.is(data.id, 51999125) | ||
t.truthy(data.id) | ||
}) | ||
|
||
test("resolve user using invalid URL with scraping", async t => { | ||
test("resolve playlist", async t => { | ||
const data = await resolve(examplePlaylistURL) | ||
t.is(data.kind, "playlist") | ||
t.truthy(data.id) | ||
}) | ||
|
||
test("resolve track", async t => { | ||
const data = await resolve(exampleTrackURL) | ||
t.is(data.kind, "track") | ||
t.truthy(data.id) | ||
}) | ||
|
||
test("resolve user using invalid URL", async t => { | ||
await t.throwsAsync(resolve("https://vaaski.dev")) | ||
}) | ||
|
||
test("resolve user with APIv1", async t => { | ||
const data = await resolve.browser(exampleUser, client_id) | ||
test("resolve.browser user", async t => { | ||
const data = await resolve.browser(exampleUserURL, client_id) | ||
t.is(data.kind, "user") | ||
t.truthy(data.username) | ||
t.is(data.id, 51999125) | ||
t.truthy(data.id) | ||
}) | ||
|
||
test("resolve.browser playlist", async t => { | ||
const data = await resolve.browser(examplePlaylistURL, client_id) | ||
t.is(data.kind, "playlist") | ||
t.truthy(data.id) | ||
}) | ||
|
||
test("resolve.browser track", async t => { | ||
const data = await resolve.browser(exampleTrackURL, client_id) | ||
t.is(data.kind, "track") | ||
t.truthy(data.id) | ||
}) | ||
|
||
test("resolve.browser user throws without client_id", async t => { | ||
// @ts-expect-error intentionally without client_id | ||
await t.throwsAsync(resolve.browser(exampleUserURL)) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type URLorID = string | number | ||
export type ClientIDv1 = string | ||
export type ClientIDv2 = string |
Oops, something went wrong.