public ItemCrawlerTests() { _shops = new List<WebStore>() { new WebStore() { FriendlyName = "Mpx", Domain = "www.mpx.no", ProductTitleQueryExpr = "h1", }, new WebStore() { FriendlyName = "Komplett", Domain = "www.komplett.no", ProductTitleQueryExpr = "h1", }, new WebStore() { FriendlyName = "Elkjop", Domain = "www.elkjop.no", ProductTitleQueryExpr = "h1", }, }; crawler = new ItemCrawler(Url); }
public ActionResult AddItem(AddItemViewModel model) { var uri = model.Url.ConvertToUri(); var domainName = uri.Host; var webStore = (from shop in RavenSession.Query<WebStore>() where shop.Domain == domainName select shop).FirstOrDefault(); var categoryList = ExtractCategoriesFromString(model.Categories); if (webStore != null) { var itemCrawler = new ItemCrawler(uri); var item = new Item() { OriginalUrl = uri.OriginalString, Title = itemCrawler.QueryInnerText(webStore.ProductTitleQueryExpr), Description = itemCrawler.QueryInnerText(webStore.ProductDescriptionQueryExpr), Image = itemCrawler.QueryImage(webStore.ProductImageQueryExpr, domainName), Price = itemCrawler.QueryInnerText(webStore.ProductPriceQueryExpr), Owner = UserId, StoreId = "webstores/" + webStore.Id, Store = webStore.FriendlyName, Categories = categoryList, Created = DateTime.Now, }; RavenSession.Store(item); } else { var itemCrawler = new ItemCrawler(uri); var item = new Item() { OriginalUrl = uri.OriginalString, Title = itemCrawler.QueryInnerText("title"), Image = "http://placehold.it/100x100", Owner = UserId, Categories = categoryList, Created = DateTime.Now, }; RavenSession.Store(item); var loadedItem = RavenSession.Load<Item>(item.Id); var metadata = RavenSession.Advanced.GetMetadataFor(loadedItem); metadata.Add("Item-From-Domain", new RavenJValue(domainName)); metadata.Add("Is-Updated", new RavenJValue(false)); RavenSession.Store(item); var proposeNewStore = new ProposedWebStore() { Created = DateTime.Now, Domain = domainName, State = ProposedWebStoreState.Proposed, OriginalUrl = uri.OriginalString, ProposedByUserId = UserId, }; RavenSession.Store(proposeNewStore); } return RedirectToAction("Index"); }