Skip to content

Instantly share code, notes, and snippets.

@miroslavradojevic
Created June 12, 2018 15:26
Show Gist options
  • Save miroslavradojevic/5532b5b807a4147550640fe1bd760057 to your computer and use it in GitHub Desktop.
Save miroslavradojevic/5532b5b807a4147550640fe1bd760057 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
namespace GetAudio
{
class Program
{
static int maxNrFiles = int.MaxValue;
static void Main(string[] args)
{
//
XmlDocument xml = new XmlDocument();
xml.Load("http://www.rts.rs/page/radio/sr/podcast/5433/govori-da-bih-te-video/audio.html");
string xml2 = xml.InnerXml;
//
string dirPath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + "stuff";
Directory.CreateDirectory(dirPath);
//
XDocument ttt = XDocument.Parse(xml.InnerXml);
int count = 0;
foreach (var t in ttt.Descendants().Where(a => a.Name == "enclosure"))
{
if (t.HasAttributes)
{
if (t.Attribute("url") != null)
{
string audioUrl = t.Attribute("url").Value;
string directory = Path.GetDirectoryName(audioUrl);
string output = Regex.Replace(directory, "[^0-9]+", "");
string filename = Path.GetFileName(audioUrl);
string filenameExt = Path.GetExtension(audioUrl);
string outFilePath = dirPath + Path.DirectorySeparatorChar + output + filenameExt;
Console.WriteLine(++count + ": " + audioUrl + "\n--> " + outFilePath);
if (count > maxNrFiles)
{
break;
}
using (WebClient wc = new WebClient())
{
//wc.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
wc.DownloadFile(new Uri(audioUrl), outFilePath); //DownloadFileAsync
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Done.\n");
Console.ResetColor();
}
}
}
}
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Exported to: " + dirPath);
Console.ResetColor();
Console.WriteLine("Press any key to finish...");
Console.ReadLine();
System.Diagnostics.Process.Start(dirPath);
}
//private static void Completed(object sender, AsyncCompletedEventArgs e)
//{
// Console.WriteLine("Download completed!");
//}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment