通过API下载声云数据

问题描述 投票:0回答:4

我尝试通过他们的 API 从 soundcloud 检索 mp3 数据,我已经有了 download_url 属性,我想将其直接保存到 KnownFolders.MusicLibrary。 问题是当我尝试使用浏览器下载它时,链接要求保存/播放。 我是否可以绕过这个并获得直接下载链接,而不必提示选择保存或播放。

c# windows-phone-8.1 windows-phone
4个回答
4
投票

我自己没有尝试过,但“download_url”正如您所说的原始文件的网址。尝试使用此代码并查看您得到什么,然后您可以修改请求,直到获得数据流。您还可以尝试使用 Burp 或 Fiddler 等代理来检查传递给 Soundcloud 的内容,然后创建类似的请求。

public static string GetSoundCloudData()
{
    var request = (HttpWebRequest)WebRequest.Create("http://api.soundcloud.com/tracks/3/download");
    request.Method = "GET";
    request.UserAgent =
        "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0";
    // Get the response.
    WebResponse response = request.GetResponse();
    // Display the status.
    if (((HttpWebResponse)response).StatusCode == HttpStatusCode.OK)
    {
        // Get the stream containing content returned by the server.
        var dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream, Encoding.UTF8);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        // Display the content.
        Debug.WriteLine(responseFromServer);

        // Clean up the streams.
        reader.Close();
        dataStream.Close();
        response.Close();
        return responseFromServer;

    }
    else
    {
        response.Close();
        return null;
    }
}

1
投票

尝试使用 HttpClient 或 WebClient。 据我记得 WebClient 有一个 OpenReadAsync 方法。

使用此方法并将 mp3 url 作为参数传递。

您将返回的是 mp3 的流,然后您可以使用 StreamWriter 将其作为文件保存到本地文件夹中。


0
投票

谢谢@Ogglas,我使用了你的一些代码并成功检索了 cdn url :

var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
var response = await request.GetResponseAsync();
var downloadurl = response.ResponseUri.AbsoluteUri;

0
投票

根据上面的 Ogglas 答案,我还添加了一个过滤器,仅将开放图元属性(名称、描述、封面图像、url)作为字典返回。如果您的目标只是获取有关赛道的信息,这会消除很多不需要的干扰。

您将需要用于 DOM 节点解析的 HTML Agility pack 来实现此功能。

      [HttpGet]
        public async Task<IActionResult> GetSoundCloudData(string path)
        {
            var userId = Security.GetAuthenticatedUserId(User);

            var request = (HttpWebRequest)WebRequest.Create("https://soundcloud.com/" + path);
            request.Method = "GET";
            request.UserAgent =
                "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0";
            // Get the response.
            WebResponse response = request.GetResponse();
            // Display the status.
            if (((HttpWebResponse)response).StatusCode == HttpStatusCode.OK)
            {
                // Get the stream containing content returned by the server.
                var dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream, Encoding.UTF8);
                // Read the content.
                string responseFromServer = reader.ReadToEnd();

                // Clean up the streams.
                reader.Close();
                dataStream.Close();
                response.Close();

                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(responseFromServer);
                HtmlNodeCollection Nodes = doc.DocumentNode.SelectNodes("//meta[@property]");
                var openGraph = Nodes.Where(n => n.Attributes.Any(a => a.Value.StartsWith("og:")));

                var d = new Dictionary<string, string>();
                foreach (var node in openGraph)
                {
                    d.Add(node.Attributes["property"].Value, node.Attributes["content"].Value);
                }

                return Ok(d);

            }
            else
            {
                response.Close();
                return null;
            }
        }

返回 JSON

{
    "og:site_name": "SoundCloud",
    "og:type": "music.song",
    "og:url": "https://soundcloud.com/malcolm-swaine/deep-house-334800-32",
    "og:title": "Deep House .3.3.4800 32",
    "og:image": "https://i1.sndcdn.com/artworks-000180487874-c36txi-t500x500.jpg",
    "og:image:width": "500",
    "og:image:height": "500",
    "og:description": "Made following a tutorial from Production Music Live"
}
© www.soinside.com 2019 - 2024. All rights reserved.