public override List <IMovie> Mine()
        {
            var lineDelimiters  = new[] { '\n' };
            var tokenDelimiters = new[] { '\t' };
            var result          = new List <IMovie>();
            var data            = HttpRequestUtil.DownloadTextFile(Url);
            var lines           = data?.Split(lineDelimiters);

            if (lines != null)
            {
                var      id          = 1;
                DateTime lastUpdated = DateTime.Now;

                if (DateTime.TryParse(lines[0].Replace("Updated by @akvalley:", string.Empty).Replace("Central (Lock time Fridays 11:00:00)", string.Empty), out lastUpdated))
                {
                    LastUpdated = lastUpdated;
                }

                foreach (var line in lines.Skip(3))
                {
                    var tokens = line?.Split(tokenDelimiters);

                    if (tokens != null && tokens.Length == 3)
                    {
                        var movie = new Movie
                        {
                            Id            = id,
                            WeekendEnding = Convert.ToDateTime(tokens[0]),
                            Earnings      = Convert.ToDecimal(tokens[1]),
                            Name          = MapName(RemovePunctuation(tokens[2]))
                        };
                        result.Add(movie);
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        public override List <IMovie> Mine()
        {
            var result = new List <IMovie>();
            var web    = new HtmlWeb();
            var doc    = web.Load(DEFAULT_URL);

            // TODO: Somehow parse the page title from "Summer Week 13" into a Sunday date for each movie.

            // Lookup XPATH to get the right node that matches.
            // Select all of the <script> nodes that are children of <body> with an attribute of "src"
            // REF: https://www.w3schools.com/xml/xpath_syntax.asp

            //var node = doc.DocumentNode.SelectSingleNode("body/script[@src='*/MonCompare*']");
            var node = doc.DocumentNode.SelectSingleNode("//body/script[contains(@src, 'MonCompare')]");

            if (node != null)
            {
                var src = node.GetAttributeValue("src", null);

                if (src != null)
                {
                    // Now retrieve the JSON (.js) page/file.

                    //doc = web.Load($"{DEFAULT_URL}/{src}");

                    var jsonData = HttpRequestUtil.DownloadString($"{DEFAULT_URL}/{src}");

                    // The string is not really JSON, but CLOSE
                    // Might want to use Regex to change this.

                    jsonData = jsonData.Replace("year =", "\"year\":");
                    jsonData = jsonData.Replace("season =", "\"season\":");
                    jsonData = jsonData.Replace("week =", "\"week\":");
                    jsonData = jsonData.Replace("movies=", "\"movies\":");
                    // Adjust the "JSON" array.
                    jsonData = jsonData.Replace("'[' +", "[").Replace("';", string.Empty).Replace(";", ",");
                    jsonData = jsonData.Replace("'+", string.Empty).Replace("'{", "{");

                    var movieData = JsonConvert.DeserializeObject <MineNerdData>($"{{{jsonData}}}");
                    int id        = 1;

                    foreach (var movie in movieData.Movies)
                    {
                        var name     = RemovePunctuation(HttpUtility.HtmlDecode(movie.Title));
                        var newMovie = new Movie
                        {
                            Id       = id++,
                            Name     = MapName(ParseName(name)),
                            Day      = ParseDayOfWeek(name),
                            Earnings = movie.OriginalEstimatedBoxOffice * 1000,
                            Cost     = movie.Bux,
                            //WeekendEnding = MovieDateUtil.NextSunday().Date
                            WeekendEnding = MovieDateUtil.GameSunday().Date
                        };

                        result.Add(newMovie);
                    }
                }
            }

            return(result);
        }