Esempio n. 1
0
        public async Task <IEnumerable <Track> > ForYear(int year, BillboardGenre billboardGenre)
        {
            Logger.Magenta($"Gathering tracks from Billboard for Year:{year}....");
            var uri      = string.Format(BillboardEndpoint, year, genres[billboardGenre]);
            var response = await _httpClient.GetAsync(uri);

            var html = await response.Content.ReadAsStringAsync();

            var document = new HtmlAgilityPack.HtmlDocument();

            document.LoadHtml(html);

            var tracks = new List <Track>();
            var table  = document.DocumentNode.Descendants("table").Where(x => x.Attributes.Any(a => a.Value?.Equals("archive-table") ?? false)).FirstOrDefault();

            foreach (var tr in table.Descendants("tr"))
            {
                var tds = tr.Elements("td").ToList();
                if (tds != null && tds.Count == 3)
                {
                    var ahref     = tds[0].Element("a");
                    var chartPath = ahref.Attributes["href"].Value;
                    if (chartPath != null)
                    {
                        var childLink = $"{BillboardUrl}{chartPath}";
                        tracks.AddRange((await new ChartReader(childLink, _httpClient).GetTracks()));
                    }
                }
            }
            tracks = tracks.Distinct(new TrackComprer()).ToList();
            Logger.Magenta($"For Year:{year} and Genre:{billboardGenre.ToString()}. Discovered {tracks.Count} tracks");

            return(tracks);
        }
Esempio n. 2
0
        private async Task <IEnumerable <Track> > Intercept(Task <IEnumerable <Track> > tracks, string output, int year, BillboardGenre genre)
        {
            var result = await tracks;
            var data   = new
            {
                year   = year,
                tracks = result.ToList()
            };

            if (!Directory.Exists(output))
            {
                Directory.CreateDirectory(output);
            }
            if (!Directory.Exists(Path.Combine(output, genre.ToString())))
            {
                Directory.CreateDirectory(Path.Combine(output, genre.ToString()));
            }
            File.WriteAllText($"{Path.Combine(output, genre.ToString())}\\{year}.json", JsonConvert.SerializeObject(data, Formatting.Indented));
            return(result);
        }