Signed-off-by: Matt Bruce <mbrucedogs@gmail.com>

This commit is contained in:
Matt Bruce 2019-09-26 21:08:51 -05:00
parent d079e5c67d
commit 0500e734cc

View File

@ -4,6 +4,7 @@ using FireSharp.Config;
using FireSharp.Interfaces; using FireSharp.Interfaces;
using Herse.Models; using Herse.Models;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Configuration; using System.Configuration;
@ -23,7 +24,7 @@ namespace BillboardPlaylistUpdater
static void Main(string[] args) static void Main(string[] args)
{ {
//args = new string[] { "bsully5150" }; //args = new string[] { "mbrucedogs" };
if (args.Length != 1) if (args.Length != 1)
{ {
Console.WriteLine("usage: songcrawler partyid songspath"); Console.WriteLine("usage: songcrawler partyid songspath");
@ -74,7 +75,7 @@ namespace BillboardPlaylistUpdater
static void UpdateCurrentCharts() 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 pop = Download("Pop-Songs", "https://www.billboard.com/charts/pop-songs");
SongList rock = Download("Rock-Songs", "https://www.billboard.com/charts/rock-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"); SongList country = Download("Country-Songs", "https://www.billboard.com/charts/country-songs");
@ -226,6 +227,66 @@ namespace BillboardPlaylistUpdater
return songs; 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<SongListSong> songs = ParseHot100(title, html);
if (songs != null)
{
list = new SongList();
list.Title = title;
list.Songs = songs;
}
return list;
}
static List<SongListSong> ParseHot100(string name, string html)
{
List<SongListSong> 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("&quot;", "\"").Replace("&quotquot;", "\"").Replace("&quoquot;;", "\"").Replace("&ququot;", "\"");
JArray articles = JArray.Parse(json);
if (articles.Count() > 0)
{
Console.WriteLine("Found " + articles.Count() + " Songs");
songs = new List<SongListSong>();
}
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) static string DownloadHtml(string url)
{ {
string data = null; string data = null;