Skip to content

Instantly share code, notes, and snippets.

@thquinn
Last active July 16, 2023 21:59
Show Gist options
  • Save thquinn/952c523a0b0d9c20a3a392c1a09b4ced to your computer and use it in GitHub Desktop.
Save thquinn/952c523a0b0d9c20a3a392c1a09b4ced to your computer and use it in GitHub Desktop.
Quick 'n' dirty Stitcher Premium bulk downloader. Windows build: https://github.com/thquinn/thquinn.github.io/releases/tag/Santaman
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Xml;
namespace Santaman
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" __ \n (__/ ) \n / _ __ _/_ _ ___ _ __ \n ) / (_(_/ (_(__(_(_// (_(_(_/ (_\n(_/\n");
string[] temps = System.IO.Directory.GetFiles(".", "*.tmp");
foreach (string temp in temps)
{
File.Delete(temp);
}
File.Delete("_log.txt");
string feedURL = "";
string[] userTokens = new string[0];
while (userTokens.Length != 2)
{
Console.WriteLine("Enter the feed URL (ex: http://12345:[email protected]/shows/123456/feed):");
feedURL = Console.ReadLine();
Uri uri = new UriBuilder(feedURL).Uri;
userTokens = uri.UserInfo.Split(':');
}
string feedString = "";
WebClient client = new WebClient();
client.Credentials = new NetworkCredential(userTokens[0], userTokens[1]);
try
{
feedString = client.DownloadString(feedURL);
}
catch {
Console.WriteLine("Feed not found. Closing...");
Thread.Sleep(1000);
return;
}
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(feedString);
Console.WriteLine("Loaded RSS for {0}.", xmlDoc.SelectSingleNode("rss").SelectSingleNode("channel").SelectSingleNode("title").InnerText);
foreach (XmlNode item in xmlDoc.SelectSingleNode("rss").SelectSingleNode("channel").SelectNodes("item"))
{
DateTime dt = DateTime.Parse(item.SelectSingleNode("pubDate").InnerText);
string name = item.SelectSingleNode("title").InnerText;
string epURL = item.SelectSingleNode("enclosure").Attributes["url"].Value;
string filename = string.Format("{0} {1}.mp3", dt.ToString("yyyy-MM-dd"), name);
string tempFilename = string.Format("{0} {1}.tmp", dt.ToString("yyyy-MM-dd"), name);
filename = MakeValidFileName(filename);
tempFilename = MakeValidFileName(tempFilename);
if (File.Exists(filename))
{
Console.WriteLine("Already downloaded: {0}", filename);
continue;
}
Console.WriteLine("Downloading: {0}", filename);
HttpWebRequest request = HttpWebRequest.Create(epURL) as HttpWebRequest;
request.Method = WebRequestMethods.Http.Get;
request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0";
request.Credentials = new NetworkCredential(userTokens[0], userTokens[1]);
HttpWebResponse httpResponse;
try
{
httpResponse = (HttpWebResponse)request.GetResponse();
Stream httpResponseStream = httpResponse.GetResponseStream();
// create and open a FileStream, using calls dispose when done
using (var fs = File.Create(tempFilename))
{
httpResponseStream.CopyTo(fs);
}
File.Move(tempFilename, filename);
}
catch (WebException e)
{
if ((e.Response as HttpWebResponse).StatusCode == HttpStatusCode.NotFound)
{
Console.WriteLine("File not found: {0}", filename);
File.AppendAllText("_log.txt", string.Format("File not found: {0}\n", filename));
}
else
{
Console.WriteLine("Unhandled exception: {0}", e);
}
}
}
Console.WriteLine("Done! Press Enter to exit.");
Console.ReadLine();
}
private static string MakeValidFileName(string filename)
{
return string.Concat(filename.Split(Path.GetInvalidFileNameChars()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment