Esempio n. 1
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);
        }