From 0500e734cc9d3f527638d7c451259e1eb5aa8928 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Thu, 26 Sep 2019 21:08:51 -0500 Subject: [PATCH] Signed-off-by: Matt Bruce --- BillboardPlaylistUpdater/Program.cs | 65 ++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/BillboardPlaylistUpdater/Program.cs b/BillboardPlaylistUpdater/Program.cs index 2805308..850e1e6 100644 --- a/BillboardPlaylistUpdater/Program.cs +++ b/BillboardPlaylistUpdater/Program.cs @@ -4,6 +4,7 @@ using FireSharp.Config; using FireSharp.Interfaces; using Herse.Models; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Configuration; @@ -23,7 +24,7 @@ namespace BillboardPlaylistUpdater static void Main(string[] args) { - //args = new string[] { "bsully5150" }; + //args = new string[] { "mbrucedogs" }; if (args.Length != 1) { Console.WriteLine("usage: songcrawler partyid songspath"); @@ -74,7 +75,7 @@ namespace BillboardPlaylistUpdater static void UpdateCurrentCharts() { - SongList hot100 = Download("Hot 100", "https://www.billboard.com/charts/hot-100"); + SongList hot100 = DownloadHot100("Hot 100", "https://www.billboard.com/charts/hot-100"); SongList pop = Download("Pop-Songs", "https://www.billboard.com/charts/pop-songs"); SongList rock = Download("Rock-Songs", "https://www.billboard.com/charts/rock-songs"); SongList country = Download("Country-Songs", "https://www.billboard.com/charts/country-songs"); @@ -226,6 +227,66 @@ namespace BillboardPlaylistUpdater return songs; } + static SongList DownloadHot100(string listName, string url) + { + DateTime now = DateTime.Now; + string title = now.Year + " - " + listName; + + Console.WriteLine("Downloading " + title); + + string html = DownloadHtml(url); + + SongList list = null; + List songs = ParseHot100(title, html); + if (songs != null) + { + list = new SongList(); + list.Title = title; + list.Songs = songs; + } + return list; + } + + static List ParseHot100(string name, string html) + { + List songs = null; + var parser = new HtmlParser(); + var document = parser.Parse(html); + //2-? + var cs = "data-charts=\""; + var ce = "data-icons=\"https:"; + var ics = html.IndexOf(cs) + cs.Length; + var ice = html.IndexOf(ce); + var json = html.Substring(ics, ice - ics); + json = json.Replace("\"", "").Replace(""", "\"").Replace(""quot;", "\"").Replace("&quoquot;;", "\"").Replace("&ququot;", "\""); + JArray articles = JArray.Parse(json); + + if (articles.Count() > 0) + { + Console.WriteLine("Found " + articles.Count() + " Songs"); + songs = new List(); + } + + + + var i = 1; + foreach (var article in articles) + { + var title = (string)article["title"]; + var artist = (string)article["artist_name"]; + var song = new SongListSong(); + song.Artist = WebUtility.HtmlDecode(artist); + song.Title = WebUtility.HtmlDecode(title); + song.Position = Convert.ToInt32(i); + songs.Add(song); + i++; + } + Console.Write("Parsed " + songs.Count() + " Songs"); + return songs; + } + + + static string DownloadHtml(string url) { string data = null;