Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Commit

Permalink
feat: add track.stream method
Browse files Browse the repository at this point in the history
  • Loading branch information
vaaski committed Mar 5, 2021
1 parent 2203cc1 commit 2c9f1a6
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 14 deletions.
14 changes: 7 additions & 7 deletions demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ const exampleSearchTerm = "noisia"

// const out = await util.ensureMin(await user.tracks("space-laces", { limit: 10 }), 10)

const out = await track(exampleTrackID)
const out = await track.stream("https://soundcloud.com/noisia/tentacles-1")
// const out = await track(exampleTrackID)
// const out = await track(exampleTrackURL)
// const out = await search(exampleSearchTerm, { limit: 2 })
// const out = await search.users(exampleSearchTerm)
Expand All @@ -65,13 +66,12 @@ const exampleSearchTerm = "noisia"
// const out = await resolve(exampleUserURL)
// const out = await resolve(exampleTrackURL)
// const out = await resolve(examplePlaylistURL)
// const out = await resolve.browser(exampleUserURL, client_id)
// const out = await resolve.browser(exampleTrackURL, client_id)
// const out = await resolve.browser(examplePlaylistURL, client_id)

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if ("collection" in out) console.log(`got ${out.collection.length} items in collection`)
if (typeof out === "object" && "collection" in out) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
console.log(`got ${out.collection.length} items in collection`)
}

console.log(
`fetched ${formatSize(byteLength(out))} of data in ${Date.now() - startTime}ms\n` +
Expand Down
4 changes: 2 additions & 2 deletions src/playlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ClientIDv2, URLorID } from "../types"
import type { Playlistv2 } from "../types/playlist"

import got from "got"
import { APIv2, getClientIDv2, scrapeData, ScrapeIDs, urlify } from "./util"
import { APIv2, getClientIDv2, scrapeData, SCRAPE_ID, urlify } from "./util"

/**
* Get a playlist by ID using the APIv2.
Expand All @@ -22,7 +22,7 @@ const byID = async (id: number, client_id: ClientIDv2) => {
*/
const byURL = async (url: string) => {
const scraped = await scrapeData(urlify(url))
const playlistData = scraped.find(({ id }) => id === ScrapeIDs.playlist)
const playlistData = scraped.find(({ id }) => id === SCRAPE_ID.playlist)
if (!playlistData) throw new Error("No playlist data found.")

const [data] = playlistData.data
Expand Down
21 changes: 19 additions & 2 deletions src/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Trackv2 } from "../types/track"
import type { ClientIDv2, URLorID } from "../types"

import got from "got"
import { APIv2, getClientIDv2, scrapeData, ScrapeIDs, urlify } from "./util"
import { APIv2, getClientIDv2, scrapeData, SCRAPE_ID, urlify } from "./util"

/**
* Get a track by ID using the APIv2
Expand All @@ -22,7 +22,7 @@ const getByID = async (id: number, client_id: ClientIDv2): Promise<Trackv2> => {
*/
const getByURL = async (url: string) => {
const scraped = await scrapeData(urlify(url))
const trackData = scraped.find(({ id }) => id === ScrapeIDs.trackWithTranscodings)
const trackData = scraped.find(({ id }) => id === SCRAPE_ID.trackWithTranscodings)
if (!trackData) throw new Error("No track data found.")

const [data] = trackData.data
Expand All @@ -48,4 +48,21 @@ const track = async (identifier: URLorID, client_id?: ClientIDv2): Promise<Track
throw new Error("Source must be a string (URL) or a number (ID)")
}

track.stream = async (identifier: URLorID | Trackv2, client_id?: ClientIDv2) => {
if (!client_id) client_id = await getClientIDv2()

if (typeof identifier !== "object") identifier = await track(identifier, client_id)

const { transcodings } = identifier.media
const progressive = transcodings.find(t => t.format.protocol === "progressive")

/* istanbul ignore next */
if (!progressive) throw new Error("no progressive format found")

const searchParams = { client_id }
const { url } = await got(progressive.url, { searchParams }).json<{ url: string }>()

return url
}

export default track
4 changes: 2 additions & 2 deletions src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
PaginatedResponse,
paginateNext,
scrapeData,
ScrapeIDs,
SCRAPE_ID,
urlify,
} from "./util"

Expand All @@ -38,7 +38,7 @@ const getByID = async (id: number, client_id: ClientIDv2) => {
*/
const getByURL = async (url: string) => {
const scraped = await scrapeData(urlify(url))
const userData = scraped.find(({ id }) => id === ScrapeIDs.user)
const userData = scraped.find(({ id }) => id === SCRAPE_ID.user)
if (!userData) throw new Error("No user data found.")

const [data] = userData.data
Expand Down
2 changes: 1 addition & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const scrapeData = async (url: string): Promise<ScrapedData> => {
return JSON.parse(match) as ScrapedData
}

export const ScrapeIDs = {
export const SCRAPE_ID = {
user: 73324,
playlist: 62261,
trackWithTranscodings: 70925,
Expand Down
16 changes: 16 additions & 0 deletions tests/track.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,19 @@ test("get track by URL throws when not found", async t => {
test("get track by ID throws when not found", async t => {
await t.throwsAsync(track(0, client_id2))
})

test("get track stream using ID", async t => {
const data = await track.stream(exampleTrackID)
t.truthy(data)
})

test("get track stream using Trackv2 object", async t => {
const trackData = await track(exampleTrackURL)
const data = await track.stream(trackData)
t.truthy(data)
})

test("get track stream using ID and client_id", async t => {
const data = await track.stream(exampleTrackID, client_id2)
t.truthy(data)
})

0 comments on commit 2c9f1a6

Please sign in to comment.