Skip to content

Instantly share code, notes, and snippets.

@praeclarum
Created April 7, 2012 16:34
Show Gist options
  • Select an option

  • Save praeclarum/2330176 to your computer and use it in GitHub Desktop.

Select an option

Save praeclarum/2330176 to your computer and use it in GitHub Desktop.
Downloads Kevin's music from SoundCloud
using System;
using System.Net;
using System.IO;
namespace DLKevin
{
class App
{
public static void Main (string[] args)
{
new App ().Run ();
}
CookieContainer _cookies = new CookieContainer ();
void Run ()
{
Get ("http://soundcloud.com/echuir/tracks", s => {
var html = new StreamReader (s).ReadToEnd ();
var p = 0;
Func<string, string> ReadJsonValue = (key) => {
var i = html.IndexOf ("\"" + key + "\":", p);
if (i >= 0) {
var start = i + 4 + key.Length;
var end = html.IndexOf ("\"", start);
p = end;
return html.Substring (start, end - start);
}
else {
return "";
}
};
for (;;) {
var title = ReadJsonValue ("title");
var streamUrl = ReadJsonValue ("streamUrl");
if (streamUrl.StartsWith ("http")) {
Get (streamUrl, (ss) => {
Console.WriteLine ("Downloading " + title);
ss.CopyTo (File.OpenWrite ("/Users/fak/Downloads/" + title + ".mp3"));
});
}
else {
break;
}
}
});
}
void Get (string url, Action<Stream> k)
{
var req = (HttpWebRequest)WebRequest.Create (url);
req.CookieContainer = _cookies;
var res = req.GetResponse ();
using (var s = res.GetResponseStream ()) {
k (s);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment