Created
January 20, 2016 19:32
-
-
Save mindingdata/6f796ab511db3faf65e6 to your computer and use it in GitHub Desktop.
Easy C# Code to pull TheRock.net.nz playlist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
using System.Threading.Tasks; | |
using HtmlAgilityPack; | |
using System.Text.RegularExpressions; | |
using System.IO; | |
namespace TheRockNoRepeat | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Download the webpage contents. | |
WebClient client = new WebClient(); | |
var webpageContents = client.DownloadString("http://www.therock.net.nz/Shows/Workday.aspx"); | |
//Store it in an HTML document for easier access. | |
HtmlDocument webpageHtml = new HtmlDocument(); | |
webpageHtml.LoadHtml(webpageContents); | |
Regex timeRegex = new Regex("var fullday = \"(.*)\";"); | |
Regex titleRegex = new Regex("var tit = \"(.*)\";"); | |
Regex artistRegex = new Regex("var art = \"(.*)\";"); | |
var timeMatches = timeRegex.Matches(webpageContents); | |
var titleMatches = titleRegex.Matches(webpageContents); | |
var artistMatches = artistRegex.Matches(webpageContents); | |
if(timeMatches.Count != titleMatches.Count || titleMatches.Count != artistMatches.Count) | |
throw new Exception("Unable to find the correct amount of matches"); | |
List<string> outputLines = new List<string>(); | |
for(int i =0; i < timeMatches.Count; i++) | |
{ | |
outputLines.Add(string.Format("{0},\"{1}\",\"{2}\"", timeMatches[i].Groups[1].Value, titleMatches[i].Groups[1].Value, artistMatches[i].Groups[1].Value)); | |
} | |
//Just reverse them so oldest is at the bottom. | |
File.AppendAllLines("output.csv", outputLines.AsEnumerable().Reverse()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment