Skip to content

Instantly share code, notes, and snippets.

@mikezila
Created January 29, 2016 01:16
Show Gist options
  • Save mikezila/cbf0288093a402085ed9 to your computer and use it in GitHub Desktop.
Save mikezila/cbf0288093a402085ed9 to your computer and use it in GitHub Desktop.
Rip albums from khinsider's collection
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
namespace music_get
{
static class Program
{
// Consts
const string requiredURL = "http://downloads.khinsider.com";
static int Main(string[] args)
{
// Options
bool createDirectory = false;
bool verboseOutput = false;
if (args.Length < 1)
{
Console.WriteLine("Usage:\nmusic-get url -[options]\nOptions:\n-d : Create enclosing directory\n-v : Verbose output");
return 1;
}
Console.WriteLine("music-get v0.3");
// Check if our argument is a valid url
if (!args[0].Contains(requiredURL))
{
Console.WriteLine("Bad URL provided. Must be a khinsider album page.");
return 1;
}
// See if we have any options set
if (args.Length == 2)
{
if (args[1].StartsWith("-"))
{
verboseOutput = args[1].Contains("v");
createDirectory = args[1].Contains("d");
}
else
{
Console.WriteLine("Options block must begin with a dash.");
return 1;
}
}
// Get html of album listing page
string pageHTML;
using (var client = new WebClient())
{
pageHTML = client.DownloadString(args[0]);
}
HtmlDocument albumpageDog = new HtmlDocument();
albumpageDog.LoadHtml(pageHTML);
// Get list of urls for song pages
List<string> songPageURLs = new List<string>();
foreach (var node in albumpageDog.DocumentNode.SelectNodes("//a[.='Download']"))
{
songPageURLs.Add(node.Attributes["href"].Value);
if (verboseOutput)
Console.WriteLine(node.Attributes["href"].Value);
}
// Download the HTML for each song page
// Then pluck the URL for the actual mp3 out of the page and toss it in our list
List<string> mp3urls = new List<string>();
using (var client = new WebClient())
{
foreach (string pageURL in songPageURLs)
{
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(client.DownloadString(pageURL));
mp3urls.Add(doc.DocumentNode.SelectSingleNode("//a[.='Click here to download']").Attributes["href"].Value);
}
}
// Create directory (if option is set)
if (createDirectory)
{
// Use last part of URL slug as folder name
string albumName = args[0].LastSlice();
if (verboseOutput)
{
Console.WriteLine("Creating directory " + albumName);
}
Directory.CreateDirectory(albumName);
Directory.SetCurrentDirectory(albumName);
}
// Download individual tracks
Console.WriteLine("album: " + args[0].LastSlice() + "\nsongs: " + songPageURLs.Count);
using (var client = new WebClient())
{
foreach (string song in mp3urls)
{
string songName = song.LastSlice();
Console.Write(songName + "...");
client.DownloadFile(song, songName);
Console.WriteLine("done");
}
}
return 0;
}
// Quick extension method to grab the final segment of a url
private static string LastSlice(this string url)
{
var slices = url.Split('/');
return slices[slices.Length - 1];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment