jerry08 / SpotifyExplode

Spotify downloader and metadata parser
GNU General Public License v3.0
27 stars 5 forks source link

GetSpotifyDownloaderUrlAsync - returns 400 error. #4

Closed J0nathan550 closed 7 months ago

J0nathan550 commented 11 months ago

I really like your API, and I wanted to try download some songs using track.ID, I tried to convert to YouTube ID and everything. But at the end this API's return either 400 or null (Spotify Mate). Hope you will respond soon :)

An error occurred: Response status code does not indicate success: 400 (BadRequest).
Request:
Method: POST, RequestUri: 'https://api.spotify-downloader.com/', Version: 1.1, Content: System.Net.Http.FormUrlEncodedContent, Headers:
{
  User-Agent: Other
  Content-Type: application/x-www-form-urlencoded
  Content-Length: 68
}

Code behind:

using SpotifyExplode;

var spotify = new SpotifyClient();

var playlist = await spotify.Playlists.GetAsync(
    "https://open.spotify.com/playlist/3Ld1jIr0oUCt99s7UGOiD1?si=fba1876ec87742a2"
);

var tracks = playlist.Tracks;

int tracksDownloading = 0;

if (!Directory.Exists("Music"))
{
    Directory.CreateDirectory("Music");
}

foreach (var track in tracks)
{
    tracksDownloading++;
    string author = track.Artists[0].Name;
    string title = track.Title;
    Console.WriteLine("Currently downloaded tracks: " + tracksDownloading + "\n" + "Downloading: " + author + " - " + title); // XXX - XXX

    using (HttpClient client = new())
    {
        try
        {
            HttpResponseMessage response = await client.GetAsync(spotify.Tracks.GetSpotifyDownloaderUrlAsync(track.Id).Result);

            response.EnsureSuccessStatusCode();

            byte[] fileBytes = await response.Content.ReadAsByteArrayAsync();

            await File.WriteAllBytesAsync("Music/" + author + " - " + title + ".mp3", fileBytes);
        }
        catch (HttpRequestException ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}
jerry08 commented 11 months ago

I noticed that almost every spotify downloader website has been taken down recently. That would affect the downloading part in my api, unfortunately. I will try to find another solution but it will take some time.

J0nathan550 commented 11 months ago

Well, as I read your API, I tried to use just those sites, and they are working perfectly. But they use a strange method of downloading (not giving you a straight link of audio file stream). Like you wait on a site when the progress bar will be 100%, and then you get a file already with full cached data.

Yesterday at night, I tried another method of downloading. Using again, YoutubeExplode. It is working, but the format is WEBM or MP4, and some of the files can be broken. Also, the music itself can contain the ID of the video with the song that you like, but for example, that video that you listen to sounds like a CS:GO frag movie or some shit.

I think if you want, you can try to add FFMPEG to your API to get that .WEBM file or .MP4 and convert it to .MP3

Here is the code that grabs the SpotifyExplode YouTubeID and then using YouTubeExplode downloads the audio stream of that file.

using SpotifyExplode;
using YoutubeExplode;
using YoutubeExplode.Videos.Streams;

var spotify = new SpotifyClient();

var playlist = await spotify.Playlists.GetAsync(
    "https://open.spotify.com/playlist/3Ld1jIr0oUCt99s7UGOiD1?si=fba1876ec87742a2"
);

var tracks = playlist.Tracks;

int tracksDownloading = 0;

string couldNotInstall = string.Empty;

if (!Directory.Exists("Music"))
{
    Directory.CreateDirectory("Music");
}

foreach (var track in tracks)
{

    tracksDownloading++;
    string author = track.Artists[0].Name;
    string title = track.Title;
    Console.WriteLine("Currently downloaded tracks: " + tracksDownloading + "\n" + "Downloading: " + author + " - " + title); // XXX - XXX

    using (HttpClient client = new())
    {
        try
        {
            var youtube = new YoutubeClient();

            var id = spotify.Tracks.GetYoutubeIdAsync(track.Id).Result; // getting the ID of the video from youtube 

            var streamManifest = await youtube.Videos.Streams.GetManifestAsync("https://youtube.com/watch?v=" + id); // trying to get stream audio that we will download

            var streamInfo = streamManifest.GetAudioOnlyStreams().GetWithHighestBitrate(); // getting only audio stream 

            await youtube.Videos.Streams.DownloadAsync(streamInfo, "Music/" + author + " - " + title + $".{streamInfo.Container}"); // downloading using YouTubeExplode.
        }
        catch (HttpRequestException ex) // internet problems or whatever. 
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
        catch (Exception) // sometimes the ID of the video that your API gives can be null inside of YouTubeExplode (video doesn't exist or whatever), so better to catch also some exception to stop downloading and skip to another one.
        { 
            Console.WriteLine("Could not download: " + author + " - " + title);
            couldNotInstall += author + " - " + title + "\n";
        }
    }

    Console.WriteLine("Downloading is finished, files that we couldn't download: \n" + couldNotInstall);
}
J0nathan550 commented 11 months ago

Also checkout someday with help of your API Free Spotify, I really work hard on it, I hope you will like it 😄

J0nathan550 commented 11 months ago

By the way, here is the project that I did. To download the music from playlist. It's a console app, you parse inside the playlist link, it downloads the files as .webp and using FFMPEG converts everything to .mp3. Download Music Online

jerry08 commented 11 months ago

This Spotify API doesn't use ffmpeg to convert a YouTube video. The rest is up to the user to decided how to download from YouTube since this project is only spotify-related. For that, Tyrrrz/YoutubeDownloader handles the job, or my project Yosu will download everything on mobile devices. Using your method can work too.

J0nathan550 commented 11 months ago

This Spotify API doesn't use ffmpeg to convert a YouTube video. The rest is up to the user to decided how to download from YouTube since this project is only spotify-related. For that, Tyrrrz/YoutubeDownloader handles the job, or my project Yosu will download everything on mobile devices. Using your method can work too.

I just say if you want to convert later using your API the audio or whatever you can use some of my code 😄

J0nathan550 commented 11 months ago

Hey, why did you delete the comment in github?

пт, 17 нояб. 2023 г., 12:44 dario krieger @.***>:

@J0nathan550 https://github.com/J0nathan550 I have also added a similar method:

` public async Task ConvertSpotifyTrack(string trackUrl) { var spotify = new SpotifyClient(); var track = await spotify.Tracks.GetAsync(trackUrl);

using (HttpClient client = new())
{
    try
    {
        var youtube = new YoutubeClient();
        var youtubeId = spotify.Tracks.GetYoutubeIdAsync(track.Id).Result;

        var streamManifest = await youtube.Videos.Streams.GetManifestAsync($"https://youtube.com/watch?v={youtubeId}");
        var streamInfo = streamManifest.GetAudioOnlyStreams().GetWithHighestBitrate();

        await youtube.Videos.Streams.DownloadAsync(streamInfo, SavePathConstants.FILE_SAVE_PATH_CDRIVE + track.Artists[0].Name + " - " + track.Title + $".{streamInfo.Container}");
    }

    catch (Exception exception)
    {
        Console.WriteLine(exception.Message);
    }
}

}`

But however, it doesnt save the file. Have you encountered a similar issue?

— Reply to this email directly, view it on GitHub https://github.com/jerry08/SpotifyExplode/issues/4#issuecomment-1816242566, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVGDTR7FBDFSXW6PZ64GS7TYE5EZFAVCNFSM6AAAAAA7NGMORSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQMJWGI2DENJWGY . You are receiving this because you were mentioned.Message ID: @.***>