Exemplo n.º 1
0
        /* Constructors */

        /// <summary>
        /// Instantiate class with a specific platform game entry based on gdbId
        /// </summary>
        /// <param name="gdbId"></param>
        public ScraperHandler(int gdbId, int gameId)
        {
            SSearch         = new ScraperSearch();
            MasterRecord    = ScraperMaster.GetMasterList().Where(a => a.gid == gdbId).FirstOrDefault();
            _GlobalSettings = SSearch._GlobalSettings;
            mw     = Application.Current.Windows.OfType <MainWindow>().FirstOrDefault();
            GameId = gameId;
        }
Exemplo n.º 2
0
        /* Methods */

        public static void DoScrape(ProgressDialogController controller, Game game, string countString, ScraperSearch gs, bool rescrape)
        {
            // ignore if manual editing is set
            if (game.ManualEditSet == true)
            {
                return;
            }

            string gameName = game.gameName;
            int    systemId = game.systemId;
            int    gameId   = game.gameId;

            // attempt local match for game
            controller.SetMessage("Attempting local search match for:\n" + gameName + " (" + GSystem.GetSystemCode(systemId) + ")" + "\n(" + countString);
            List <ScraperMaster> results = gs.SearchGameLocal(gameName, systemId, gameId).ToList();

            if (results.Count == 0)
            {
                // no results returned
                return;
            }
            if (results.Count == 1)
            {
                // one result returned - add GdbId to the Game table
                Game.SetGdbId(gameId, results.Single().gid);
                // also add it to our locagames object (so it is not skipped in the next part)
                //game.gdbId = results.Single().gid;
            }
            if (results.Count > 1)
            {
                // more than one result returned - do nothing (because a definite match was not found)
                return;
            }

            /* Begin scraping */
            bool scrapingNeeded = false;

            // check game directory
            if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + @"Data\Games\" + results.Single().gid.ToString()))
            {
                // directory does not exist - scraping needed
                scrapingNeeded = true;
            }
            else
            {
                // directory does exist - check whether json file is present
                if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"Data\Games\" + results.Single().gid.ToString() + @"\" + results.Single().gid.ToString() + ".json"))
                {
                    // json file is not present - scraping needed
                    scrapingNeeded = true;
                }
            }

            // if rescrape bool has been set - always rescrape
            if (rescrape == true)
            {
                scrapingNeeded = true;
            }

            if (scrapingNeeded == false)
            {
                return;
            }

            // do scrape
            string message = "Scraping Started....\nGetting data for: " + gameName + " (" + GSystem.GetSystemCode(systemId) + ")" + "\n(" + countString;

            controller.SetMessage(message);

            ScraperHandler sh = new ScraperHandler(results.Single().gid, gameId, false);

            sh.ScrapeGame(controller);
            //GameListBuilder.UpdateFlag();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Scrape multiple games that have been selected in the games library (either scrape unscraped, or re-scrape all)
        /// </summary>
        /// <param name="list"></param>
        /// <param name="scrapeType"></param>
        public async static void ScrapeMultiple(List <GamesLibraryModel> list, ScrapeType scrapeType, int systemId)
        {
            // instantiate instance of this class
            ScraperSearch gs = new ScraperSearch();

            // get mainwindow
            MainWindow MWindow = Application.Current.Windows.OfType <MainWindow>().FirstOrDefault();

            // popup progress dialog
            var mySettings = new MetroDialogSettings()
            {
                NegativeButtonText = "Cancel Scraping",
                AnimateShow        = false,
                AnimateHide        = false
            };
            var controller = await MWindow.ShowProgressAsync("Scraping Data", "Initialising...", true, settings : mySettings);

            controller.SetCancelable(true);
            await Task.Delay(100);

            await Task.Run(() =>
            {
                // check whether list is null and generate it if it is (so scraping/rescraping whole systems or favorites)
                if (list == null)
                {
                    list = new List <GamesLibraryModel>();
                    List <Game> games = new List <Game>();
                    if (scrapeType == ScrapeType.Favorites || scrapeType == ScrapeType.RescrapeFavorites)
                    {
                        games = Game.GetGames().Where(a => a.isFavorite == true && a.hidden != true).ToList();
                    }
                    else
                    {
                        // get all games that have matching systemId and are not marked as hidden
                        games = Game.GetGames(systemId).Where(a => a.hidden != true).ToList();
                    }

                    // populate list
                    foreach (var g in games)
                    {
                        GamesLibraryModel glm = new GamesLibraryModel();
                        glm.ID = g.gameId;
                        list.Add(glm);
                    }
                }


                // iterate through each game in list - match local then scrape
                int iter           = 0;
                int maxCount       = list.Count();
                int skip           = 0;
                controller.Minimum = iter;
                controller.Maximum = maxCount;
                foreach (var game in list)
                {
                    if (controller.IsCanceled)
                    {
                        controller.CloseAsync();
                        return;
                    }

                    iter++;
                    controller.SetProgress(iter);
                    // game game object from the database
                    Game g = Game.GetGame(game.ID);

                    string countString = iter + " of " + maxCount + " (" + skip + " skipped)";

                    switch (scrapeType)
                    {
                    // scrape selected games (that have no been scraped yet)
                    case ScrapeType.Selected:
                    case ScrapeType.Favorites:
                    case ScrapeType.ScrapeSystem:
                        if (g.gdbId == null || g.gdbId == 0 || Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + @"Data\Games\" + g.gdbId.ToString()) == false)
                        {
                            // scraping can happen
                            DoScrape(controller, g, countString, gs, false);
                        }
                        else
                        {
                            // the game already has a valid gdbid set AND has a game directory on disc.
                            skip++;
                        }
                        break;

                    // rescrape all selected games
                    case ScrapeType.SelectedRescrape:
                    case ScrapeType.RescrapeFavorites:
                    case ScrapeType.RescrapeSystem:
                        // scraping must always happen
                        DoScrape(controller, g, countString, gs, true);
                        break;
                    }
                }
            });

            await controller.CloseAsync();

            if (controller.IsCanceled)
            {
                await MWindow.ShowMessageAsync("MedLaunch Scraper", "Scraping Cancelled");

                GamesLibraryVisualHandler.RefreshGamesLibrary();
            }
            else
            {
                await MWindow.ShowMessageAsync("MedLaunch Scraper", "Scraping Completed");

                GamesLibraryVisualHandler.RefreshGamesLibrary();
            }
        }