Moebits / soundcloud.ts

Wrapper for the Soundcloud API with typings
MIT License
61 stars 14 forks source link

can you download a track as if you clicked the button to download a track? #56

Closed chepe263 closed 2 months ago

chepe263 commented 8 months ago

If you go to a track page, and click the three dots, there is a download button.

It makes a request to

https://api-v2.soundcloud.com/tracks/{trackId}/download?client_id={clientID}app_version={Some number}&app_locale=en

and it responds with

{
    "redirectUri": "https://cf-media.sndcdn.com/3wuS31nQDMw8?Policy={something}&Signature={something}&Key-Pair-Id={something}"
}

Is there a method in this library to do the same?

I tried with your example

await soundcloud.util.downloadTrack("euphonicsessions/euphonic-sessions-january2024", "./tracks")

But the files are different. The "download button" file is about 292mb mp3 with album art, quite similar to the download episode in podcast rss; and using the library I obtain a 116mb mp3 without album art, probably the soundcloud streaming file.

Podcast feed item example

<item>
    <title>Euphonic Sessions with Kyau & Albert January 2024</title>
    <itunes:author>Kyau & Albert</itunes:author>
    <itunes:subtitle>January 2024 Edition</itunes:subtitle>
    <itunes:summary>Tracklist for the show @ https://soundcloud.com/euphonicsessions +++
        https://www.facebook.com/kyauandalbertofficial/ +++
        https://www.instagram.com/kyauandalbert/ +++ http://www.kyauandalbert.com</itunes:summary>
    <enclosure
        url="http://www.kyauandalbert.com/ES/EuphonicSessions-Kyau&Albert-January2024.mp3"
        length="292034425" type="audio/mpeg" />
    <guid isPermaLink="false">January 2024 Edition</guid>
    <pubDate>Mon, 01 Jan 2024 09:00:00 GMT</pubDate>
    <itunes:duration>2:01:31</itunes:duration>
    <itunes:keywords>euphonic, sessions, kyau, albert, radioshow, trance, progressive,
        podcast</itunes:keywords>
</item>
skick1234 commented 8 months ago

soundcloud.ts downloads the transcoded stream instead of the original downloadable file. Album art and other metadata are only available if the author adds metadata. So I don't recommend using it, just use SoundCloud data (artwork_url,...) if you need them.

If you still want to download it, you can use this following code:

import { Soundcloud } from "soundcloud.ts"
const sc = new Soundcloud()
const trackUrl = "your track url"

const track = await sc.tracks.getV2(trackUrl)
if (track.downloadable) {
    const trackId = track.id
    const { redirectUri: downloadUrl } = await sc.api.getV2(`/tracks/${trackId}/download`)

    console.log(downloadUrl) // Or download it
}
Moebits commented 2 months ago

It originally downloaded the original file if it was available, but a contributor removed it. I will reopen this to plan to revert that functionality.

Moebits commented 2 months ago

Added in 0.5.5 you have to provide oauth token or it won't work and default to the mp3 stream