public ScraperGame DownloadInfo(ScraperResult result) { Scraper defaultScraper = result.DataProvider; ScraperGame scraperGame = defaultScraper.GetDetails(result); if (scraperGame == null || !doWork()) { return(null); } Scraper coversScraper, screensScraper, fanartScraper; lock (scrapers) { coversScraper = this.coversScraper; screensScraper = this.screensScraper; fanartScraper = this.fanartScraper; } ScraperResultsCache resultsCache = new ScraperResultsCache(result.Title, result.SearchParams); resultsCache.Add(defaultScraper, result); string image1, image2; List <Scraper> searchScrapers = getSearchScrapers(coversScraper, defaultScraper); if (!searchForImages(searchScrapers, resultsCache, ThumbSearchType.Covers, out image1, out image2)) { return(null); //doWork is false } scraperGame.BoxFrontUrl = image1; scraperGame.BoxBackUrl = image2; searchScrapers = getSearchScrapers(screensScraper, defaultScraper); if (!searchForImages(searchScrapers, resultsCache, ThumbSearchType.Screens, out image1, out image2)) { return(null); } scraperGame.TitleScreenUrl = image1; scraperGame.InGameUrl = image2; searchScrapers = getSearchScrapers(fanartScraper, defaultScraper); searchForImages(searchScrapers, resultsCache, ThumbSearchType.Fanart, out image1, out image2); scraperGame.FanartUrl = image1; return(scraperGame); }
//Asyncronously download metadata for all or new roms in DB public void StartImport(bool onlyNew) { Logger.LogInfo("Background import starting..."); Stop(); Game[] games = DB.Instance.GetGames(); if (stopWorker) { return; } List <Game> gamesToUpdate = new List <Game>(); if (onlyNew) { foreach (Game item in games) { if (item.Yearmade == 0) //assume new games are any where year is 0 { gamesToUpdate.Add(item); } } } else { gamesToUpdate.AddRange(games); } Logger.LogInfo("Found {0} games to update", gamesToUpdate.Count); if (gamesToUpdate.Count == 0) //no items to update { Logger.LogInfo("Background import finished"); return; } //try and load a valid scraper Assembly asm = Assembly.GetExecutingAssembly(); string script = ""; using (StreamReader sr = new StreamReader(asm.GetManifestResourceStream("myEmulators.Scripts.Mobygames.xml"))) { script = sr.ReadToEnd(); } ScriptableScraper scraper = new ScriptableScraper(script, false); if (!scraper.LoadSuccessful) { Logger.LogError("Error loading scraper"); scraper = null; return; //problem with scraper, stop import } System.Data.DataTable dt = Dropdowns.GetSystems(); //get a list of systems to match against parent emulator if (workingAnimation != null) { workingAnimation.Visible = true; } workerThread = new Thread(new ThreadStart(delegate() { EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 85L); ImageCodecInfo jpegCodec = ImageCodecInfo.GetImageEncoders()[1]; EncoderParameters encoderParams = new EncoderParameters(1); encoderParams.Param[0] = qualityParam; Bitmap gameArt = null; bool aborted = false; //split into groups of 3 to allow partial update of gui int groups = (int)Math.Ceiling(gamesToUpdate.Count / 3.0); List <Game>[] gameGroups = new List <Game> [groups]; for (int x = 0; x < groups; x++) //add items to a group { List <Game> gameGroup = new List <Game>(); if (x == groups - 1) //last group may contain less than 3 items { for (int y = 3 * x; y < gamesToUpdate.Count; y++) { gameGroup.Add(gamesToUpdate[y]); } } else { int y = 3 * x; gameGroup.AddRange(new Game[] { gamesToUpdate[y], gamesToUpdate[y + 1], gamesToUpdate[y + 2] }); } gameGroups[x] = gameGroup; } if (stopWorker) { return; } try { foreach (List <Game> gameGroup in gameGroups) { //partial results List <Game> gamesToReturn = new List <Game>(); foreach (Game item in gameGroup) { if (stopWorker) { return; } string SavePath = ThumbsHandler.Instance.thumb_games + @"\" + item.ParentEmulator.Title + @"\" + item.GameID.ToString(); if (!Directory.Exists(SavePath)) { Directory.CreateDirectory(SavePath); } string searchString = StripRomCodes(item.Title); string searchSystem = ""; Dictionary <string, string> paramList = new Dictionary <string, string>(); paramList["search.title"] = searchString; //try and match emulator title against predefined systems to narrow search foreach (System.Data.DataRow row in dt.Rows) { if ((row[1] as string).ToLower() == item.ParentEmulator.Title.ToLower() || (item.ParentEmulator.isPc() && row[1] as string == "Windows")) { //Console match paramList["search.system"] = row[0] as string; searchSystem = row[1] as string; break; } } if (stopWorker) { return; } //get matches and choose best result based on search settings List <ScraperResult> results = Conf_Scraper.getSearchResults(scraper, paramList); int possibleTitleMatches = 0; int possibleSystemMatches = 0; string titleSiteId = ""; string systemSiteId = ""; bool isExact = false; foreach (ScraperResult result in results) { if (stopWorker) { return; } if (result.Title == searchString && result.System == searchSystem) { //exact match break and get details isExact = true; possibleTitleMatches = 1; titleSiteId = result.SiteId; break; } else if (result.Title == searchString) { if (possibleTitleMatches == 0) { titleSiteId = result.SiteId; } possibleTitleMatches++; } else if (result.System == searchSystem) { if (possibleSystemMatches == 0) { systemSiteId = result.SiteId; } possibleSystemMatches++; } } bool importTop = Options.Instance.GetBoolOption("importtop"); bool importExact = Options.Instance.GetBoolOption("importexact"); string matchSiteId = ""; if (possibleTitleMatches == 1 && (isExact || !importExact)) { matchSiteId = titleSiteId; } else if (possibleSystemMatches == 1 && !importExact) { matchSiteId = systemSiteId; } else if (results.Count > 0 && importTop) { matchSiteId = results[0].SiteId; } if (matchSiteId == "") //no match, skip { continue; } //get game details paramList = new Dictionary <string, string>(); paramList["game.site_id"] = matchSiteId; if (stopWorker) { return; } ScraperGame scraperGame = Conf_Scraper.getGame(scraper, paramList); item.Title = scraperGame.Title; item.Description = StripTags(scraperGame.Description); item.Company = scraperGame.Company; try { item.Yearmade = Convert.ToInt32(scraperGame.Year); } catch { item.Yearmade = 0; } try { item.Grade = Convert.ToInt32(Math.Round((Convert.ToDouble(scraperGame.Grade, System.Globalization.CultureInfo.InvariantCulture) * 2), 0)); } catch { item.Grade = 0; } try { item.Genre = scraperGame.Genre.Split('|')[1]; } catch { item.Genre = ""; } item.Save(); if (stopWorker) { return; } double thumbDimensions = 0; if (Options.Instance.GetBoolOption("resizethumbs")) { thumbDimensions = ImageHandler.Instance.GetCaseAspect(item.ParentEmulator.Title); //the aspect ratio used to resize thumbs } //get covers List <string> thumbs = Conf_Scraper.getCoverUrls(scraper, paramList); //getCoverUrls handles image match logic and will return max of 2 results if (thumbs[0] != "") //front cover { try { gameArt = ImageHandler.Instance.BitmapFromWeb(thumbs[0]); if (Options.Instance.GetBoolOption("resizethumbs")) //resize image if necessary { using (Image resizedThumb = ImageHandler.Instance.resizeImage(gameArt, thumbDimensions)) { resizedThumb.Save(SavePath + @"\BoxFront.jpg", jpegCodec, encoderParams); } } else { gameArt.Save(SavePath + @"\BoxFront.jpg", jpegCodec, encoderParams); } } catch { } finally { if (gameArt != null) { gameArt.Dispose(); gameArt = null; } } } if (thumbs[1] != "") //back cover { try { gameArt = ImageHandler.Instance.BitmapFromWeb(thumbs[1]); if (Options.Instance.GetBoolOption("resizethumbs")) { using (Image resizedThumb = ImageHandler.Instance.resizeImage(gameArt, thumbDimensions)) { resizedThumb.Save(SavePath + @"\BoxBack.jpg", jpegCodec, encoderParams); } } else { gameArt.Save(SavePath + @"\BoxBack.jpg", jpegCodec, encoderParams); } } catch { } finally { if (gameArt != null) { gameArt.Dispose(); gameArt = null; } } } if (stopWorker) { return; } //get screens thumbs = Conf_Scraper.getScreenUrls(scraper, paramList); if (thumbs[0] != "") //title screenshot { try { gameArt = ImageHandler.Instance.BitmapFromWeb(thumbs[0]); gameArt.Save(SavePath + @"\TitleScreenshot.jpg", jpegCodec, encoderParams); } catch { } finally { if (gameArt != null) { gameArt.Dispose(); gameArt = null; } } } if (thumbs[1] != "") //ingame screenshot { try { gameArt = ImageHandler.Instance.BitmapFromWeb(thumbs[1]); gameArt.Save(SavePath + @"\IngameScreenshot.jpg", jpegCodec, encoderParams); } catch { } finally { if (gameArt != null) { gameArt.Dispose(); gameArt = null; } } } gamesToReturn.Add(item); } //Update DB and GUI MediaPortal.GUI.Library.GUIWindowManager.SendThreadMessage(new MediaPortal.GUI.Library.GUIMessage() { TargetWindowId = 2497, SendToTargetWindow = true, Object = gamesToReturn }); } } catch (ThreadAbortException) { Thread.ResetAbort(); aborted = true; } finally { object msgObject = aborted ? ScraperState.Aborted : ScraperState.Finished; //send finished message to GUI MediaPortal.GUI.Library.GUIWindowManager.SendThreadMessage(new MediaPortal.GUI.Library.GUIMessage() { TargetWindowId = 2497, SendToTargetWindow = true, Object = msgObject }); try { gameArt.Dispose(); } catch { } } } )); workerThread.Start(); }