SaveFromWeb() public static method

public static SaveFromWeb ( string url, string path ) : void
url string
path string
return void
示例#1
0
        public static async Task ReFetchInfo(Game g, string link)
        {
            try {
                var loadPage = "http://www.gamefaqs.com" + link;
                await Task.Run(() => _doc = Web.Load(loadPage));

                var htmlNode    = _doc.DocumentNode.SelectSingleNode("//h1[@class='page-title']");
                var htmlNode2   = _doc.DocumentNode.SelectSingleNode("//div[@class='details']");
                var htmlNode3   = _doc.DocumentNode.SelectSingleNode("//li[contains(., 'MetaCritic MetaScore')]//span");
                var nextSibling = _doc.DocumentNode.SelectSingleNode("//li[@class='core-platform']").NextSibling;
                var htmlNode4   = _doc.DocumentNode.SelectSingleNode("//li[contains(., 'Release:')]//a");
                var htmlNode5   = _doc.DocumentNode.SelectSingleNode("//img[@class='boxshot']");
                var imgLocation = htmlNode5.Attributes["src"].Value.Replace("_thumb", "_front");
                var path        = Path.Combine(UserSettings.ImageDir, g.FileSafeTitle + Path.GetExtension(imgLocation));
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                g.Title           = ((htmlNode != null) ? htmlNode.InnerText.Trim().FromHtml() : "n/a");
                g.MetacriticScore = ((htmlNode3 != null) ? htmlNode3.InnerText.Trim().FromHtml() : "n/a");
                g.Publisher       = ((nextSibling != null) ? nextSibling.InnerText.Trim().FromHtml() : "n/a");
                g.ReleaseDate     = ((htmlNode4 != null) ? htmlNode4.InnerText.Trim().FromHtml().Replace("»", "") : "n/a");
                g.Description     = ((htmlNode2 != null) ? htmlNode2.InnerText.Trim().FromHtml() : "n/a");
                var xElement = UserSettings.xGames.Descendants("Game").FirstOrDefault((XElement _g) => _g.Element("Serial").Value == g.Serial);
                if (xElement != null)
                {
                    xElement.Element("Name").Value        = g.Title;
                    xElement.Element("Score").Value       = g.MetacriticScore;
                    xElement.Element("Publisher").Value   = g.Publisher;
                    xElement.Element("Release").Value     = g.ReleaseDate;
                    xElement.Element("Description").Value = g.Description;
                    try {
                        if (!imgLocation.Contains("noboxshot") && !imgLocation.IsEmpty())
                        {
                            var saveTo = Path.Combine(UserSettings.ImageDir, g.FileSafeTitle + Path.GetExtension(imgLocation));
                            await Task.Run(() => Tools.SaveFromWeb(imgLocation, saveTo));

                            g.ImagePath = saveTo;
                        }
                        else if (imgLocation.Contains("noboxshot"))
                        {
                            Console.WriteLine("no image found for " + g.Title);
                        }
                    }
                    catch {
                        Console.WriteLine("failed fetching image for: " + g.Title);
                    }
                    Console.WriteLine("fetched info for: " + g.Title);
                }
            }
            catch (Exception ex) {
                Console.WriteLine("failed fetching info for: " + g.Title);
                Console.WriteLine("Reason: " + ex.Message);
                var xElement2 = UserSettings.xGames.Descendants("Game").FirstOrDefault(_g => _g.Element("Serial").Value == g.Serial);
                if (xElement2 != null)
                {
                    g.Description = "n/a**";
                    xElement2.Element("Description").Value = "n/a**";
                }
            }
        }
示例#2
0
        public static async Task FetchInfo(Game g)
        {
            try {
                var list = await FetchSearchResults(g);

                if (list.Count != 0)
                {
                    list = (from r in list
                            orderby r.Rating
                            select r).ToList <GameSearchResult>();
                    var loadPage = "http://www.gamefaqs.com" + list[0].Link;
                    await Task.Run(() => _doc = Web.Load(loadPage));

                    var htmlNode    = _doc.DocumentNode.SelectSingleNode("//div[@class='details']");
                    var htmlNode2   = _doc.DocumentNode.SelectSingleNode("//li[contains(., 'MetaCritic MetaScore')]//span");
                    var nextSibling = _doc.DocumentNode.SelectSingleNode("//li[@class='core-platform']").NextSibling;
                    var htmlNode3   = _doc.DocumentNode.SelectSingleNode("//li[contains(., 'Release:')]//a");
                    var htmlNode4   = _doc.DocumentNode.SelectSingleNode("//img[@class='boxshot']");
                    var imgLocation = htmlNode4.Attributes["src"].Value.Replace("_thumb", "_front");
                    g.MetacriticScore = ((htmlNode2 != null) ? htmlNode2.InnerText.Trim().FromHtml() : "n/a");
                    g.Publisher       = ((nextSibling != null) ? nextSibling.InnerText.Trim().FromHtml() : "n/a");
                    g.ReleaseDate     = ((htmlNode3 != null) ? htmlNode3.InnerText.Trim().FromHtml().Replace("»", "") : "n/a");
                    g.Description     = ((htmlNode != null) ? htmlNode.InnerText.Trim().FromHtml() : "n/a");
                    var xElement = UserSettings.xGames.Descendants("Game").FirstOrDefault(_g => _g.Element("Serial").Value == g.Serial);
                    if (xElement != null)
                    {
                        xElement.Element("Score").Value       = g.MetacriticScore;
                        xElement.Element("Publisher").Value   = g.Publisher;
                        xElement.Element("Release").Value     = g.ReleaseDate;
                        xElement.Element("Description").Value = g.Description;
                        try {
                            if (g.ImagePath.IsEmpty() && !imgLocation.Contains("noboxshot"))
                            {
                                if (File.Exists(Path.Combine(UserSettings.ImageDir, g.FileSafeTitle) + ".jpg"))
                                {
                                    g.ImagePath = Path.Combine(UserSettings.ImageDir, g.FileSafeTitle) + ".jpg";
                                    return;
                                }
                                var saveTo = Path.Combine(UserSettings.ImageDir, g.FileSafeTitle + Path.GetExtension(imgLocation));
                                await Task.Run(() => Tools.SaveFromWeb(imgLocation, saveTo));

                                g.ImagePath = saveTo;
                            }
                            else if (g.ImagePath.IsEmpty() && imgLocation.Contains("noboxshot"))
                            {
                                xElement.Element("ImagePath").Value = "none";
                                g.ImagePath = "none";
                            }
                        }
                        catch {
                            Console.WriteLine("failed fetching image for: " + g.Title);
                        }
                        Console.WriteLine("fetched info for: " + g.Title);
                    }
                }
            }
            catch (Exception) {
                Console.WriteLine("failed fetching info for: " + g.Title);
                var xElement2 = UserSettings.xGames.Descendants("Game").FirstOrDefault(_g => _g.Element("Serial").Value == g.Serial);
                if (xElement2 != null)
                {
                    g.Description = "n/a**";
                    xElement2.Element("Description").Value = "n/a**";
                }
            }
        }