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
12 changed files
with
575 additions
and
687 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,2 +1,3 @@ | ||
SOUNDCLOUD_CLIENT_ID= | ||
SOUNDCLOUD_CLIENT_ID_V1= | ||
SOUNDCLOUD_CLIENT_ID_V2= | ||
SOUNDCLOUD_OAUTH_TOKEN= |
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,4 @@ | ||
.env | ||
node_modules | ||
lib | ||
lib | ||
demo_out.json |
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,5 @@ | ||
{ | ||
"cSpell.words": [ | ||
"urlify" | ||
] | ||
} |
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,35 @@ | ||
import { resolve } from "./src" | ||
import { writeFileSync } from "fs" | ||
|
||
const byteLength = (input: any) => Buffer.byteLength(JSON.stringify(input), "utf8") | ||
const formatSize = (bytes: number, decimals = 2) => { | ||
if (bytes === 0) return "0 Bytes" | ||
|
||
const k = 1024 | ||
const dm = decimals < 0 ? 0 : decimals | ||
const sizes = ["Bytes", "KB", "MB", "GB"] | ||
|
||
const i = Math.floor(Math.log(bytes) / Math.log(k)) | ||
|
||
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i] | ||
} | ||
|
||
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" | ||
|
||
!(async () => { | ||
const startTime = Date.now() | ||
|
||
const out = await resolve(exampleUser) | ||
// const out = await resolve(exampleTrack) | ||
// const out = await resolve(examplePlaylist) | ||
// const out = await resolve.browser(exampleUser, client_id) | ||
// const out = await resolve.browser(exampleTrack, client_id) | ||
// const out = await resolve.browser(examplePlaylist, client_id) | ||
|
||
console.log(`fetched ${formatSize(byteLength(out))} in ${Date.now() - startTime}ms`) | ||
|
||
writeFileSync("demo_out.json", JSON.stringify(out, null, 2)) | ||
})() |
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,3 +1 @@ | ||
import ky from "ky-universal" | ||
|
||
ky("https://httpbin.org/anything").json().then(console.log) | ||
export * from "./resolve" |
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,36 @@ | ||
import { APIv1Resolve, ScrapeResolve } from "../types/resolve" | ||
|
||
import { APIv1, at, urlify } from "./util" | ||
import ky from "ky-universal" | ||
|
||
const htmlDataReg = /(\[{"id")(.*?)(?=\);)/i | ||
|
||
/** | ||
* Resolve a track, user or playlist using web scraping. | ||
* This doesn't work in browsers, as CORS is disabled. | ||
* In browsers you have to use `resolve.browser` instead. | ||
* @param url | ||
*/ | ||
export const resolve = async (url: string): Promise<ScrapeResolve> => { | ||
const html = await ky(urlify(url)).text() | ||
const [match] = html.match(htmlDataReg) || [] | ||
const parsed = JSON.parse(match) | ||
const { data } = at(parsed, -1) || at(parsed, -2) | ||
|
||
return data[0] as ScrapeResolve | ||
} | ||
|
||
/** | ||
* Resolve a track, user or playlist using the public APIv1. | ||
* This works in browsers, as CORS is enabled. | ||
* @param url the url to resolve | ||
* @param client_id a APIv1 client_id | ||
*/ | ||
resolve.browser = async (url: string, client_id: string): Promise<APIv1Resolve> => { | ||
url = urlify(url) | ||
|
||
const searchParams = { url, client_id } | ||
const data = await ky(`${APIv1}/resolve`, { searchParams }).json() | ||
|
||
return data as APIv1Resolve | ||
} |
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,12 @@ | ||
// cspell:ignore urlify | ||
|
||
export const scrapeURL = "https://soundcloud.com" | ||
export const APIv1 = "https://api.soundcloud.com" | ||
export const APIv2 = "https://api-v2.soundcloud.com" | ||
export const urlify = (url: string, base = scrapeURL): string => | ||
new URL(url, base).toString() | ||
|
||
export const at = <T>(arr: Array<T>, pos: number): T => { | ||
if (pos >= 0) return arr[pos] | ||
return arr[arr.length + pos] | ||
} |
Oops, something went wrong.