public void GetItemIDs() { string page; using (var myWebClient = new WebClient()) { myWebClient.Encoding = Encoding.UTF8; page = myWebClient.DownloadString("http://backpack.tf/classifieds/?tradable=1&quality=11,6,1,3&intent=1&sort=bump"); } HtmlDocument htmlDocument = new HtmlDocument(); htmlDocument.LoadHtml(page); IEnumerable <HtmlNode> links = htmlDocument.DocumentNode.Descendants("li") //this is a tag that I observed preceded all the html nodes that contain price listings .Where(x => x.Attributes.Contains("data-listing-price")) //this is an attribute that an html node containing a price listing should have .Where(x => x.Attributes["data-listing-intent"].Value == "1") //sell orders! .Where(x => !x.Attributes.Contains("data-gifted-id")); //no gifted items foreach (HtmlNode element in links) { ClassifiedInfo info = new ClassifiedInfo(); info.itemid = element.Attributes["data-id"].Value; info.completename = element.Attributes["title"].Value; bool flag = false; foreach (ClassifiedInfo thingy in this.ClassifiedItems) { if (thingy.itemid == info.itemid) { Console.WriteLine("duplicate"); flag = true; break; } else { flag = false; } } if (!flag) { this.ClassifiedItems.Add(info); } } }
public Classifieds GetClassifieds() { try //try clause. the code will break from the try clause and move to the catch clause if there's a problem executing the code { string page; using (var myWebClient = new WebClient()) { myWebClient.Encoding = Encoding.UTF8; page = myWebClient.DownloadString("http://backpack.tf/classifieds/?tradable=1&quality=11,6,1,3&intent=1&sort=bump"); } //HtmlWeb Htmlweb = new HtmlWeb(); HtmlDocument htmlDocument = new HtmlDocument(); htmlDocument.LoadHtml(page); IEnumerable <HtmlNode> links = htmlDocument.DocumentNode.Descendants("li") //this is a tag that I observed preceded all the html nodes that contain price listings .Where(x => x.Attributes.Contains("data-listing-price")) //this is an attribute that an html node containing a price listing should have .Where(x => x.Attributes["data-listing-intent"].Value == "1") //sell orders! .Where(x => !x.Attributes.Contains("data-gifted-id")); //no gifted items foreach (HtmlNode element in links) { ClassifiedInfo info = new ClassifiedInfo(); string name = element.Attributes["data-name"].Value; //item name if (element.Attributes["data-slot"].Value.Contains("misc")) { info.cosmetic = true; } else { info.cosmetic = false; } if (name.Contains("#")) //not crate I believe { string[] words = name.Split('#'); info.crate = int.Parse(words[1].Trim()); info.numeric = "crate"; name = words[0].Trim(); } else { info.crate = 0; info.numeric = "0"; } if (name.Contains("Australium")) { info.australium = 1; name = name.Replace("Australium", ""); name = name.Trim(); } else { info.australium = 0; } info.name = name; info.quality = int.Parse(element.Attributes["data-quality"].Value); info.craftable = int.Parse(element.Attributes["data-craftable"].Value); info.listprice = StringParsing.StringToDouble(element.Attributes["data-listing-price"].Value, true); //item price, in string format, "x ref, y keys, z, buds"; it's just how the website formats it info.steamid = element.Attributes["data-listing-steamid"].Value; //the lister's steamid number string tradelink = ""; //the lister's trade offer link (not always provided) info.itemid = element.Attributes["data-id"].Value; info.GetNames(); if (MemoryCache.Default.Contains(info.completename)) { info.price = double.Parse(MemoryCache.Default.Get(info.completename).ToString()); } else { continue; } info.BPprice = double.Parse(MemoryCache.Default.Get(info.completename + " BP").ToString()); if (element.Attributes.Contains("data-listing-offers-url")) { tradelink = element.Attributes["data-listing-offers-url"].Value; tradelink = tradelink.Replace("token=", "^"); string[] words = tradelink.Split('^'); info.token = words.Last(); } bool flag = false; foreach (ClassifiedInfo thingy in this.ClassifiedItems) { if (thingy.itemid == info.itemid) { flag = true; break; } else { flag = false; } } if (!flag) { this.ClassifiedItems.Add(info); } } return(this); } catch (Exception ex) { Console.WriteLine("Classifieds Error" + ex); Console.Read(); return(null); } }