public void SendItemUpdates(ShopifyApi api) { _log.Info("Begin SendItemUpdates"); var runner = new PublicationRunner(_log, _time, _dbFactory, PublicationRunner.PublishingMode.PerItem); var sleeper = new StepSleeper(TimeSpan.FromSeconds(2), 1); runner.SendItemUpdates((img) => { }, (parentItem, enableColorVariation, variations, styles, styleImages, styleFeatures, dropShippers) => { var description = BuildDescription(styles, styleFeatures); sleeper.NextStep(); return(api.AddOrUpdateProduct(parentItem, variations, styles, styleImages, styleFeatures, description)); }, api.Market, api.MarketplaceId, null); _log.Info("End SendItemUpdates"); }
public void SendInventoryUpdates(ShopifyApi api) { _log.Info("Begin SendInventoryUpdates"); var sleeper = new StepSleeper(TimeSpan.FromSeconds(2), 1); using (var db = _dbFactory.GetRWDb()) { var listings = db.Listings.GetQuantityUpdateRequiredList(api.Market, api.MarketplaceId); var itemIdList = listings.Select(l => l.ItemId).ToList(); var items = db.Items.GetAllViewAsDto().Where(i => itemIdList.Contains(i.Id)).ToList(); var listingWithErrorList = new List <Listing>(); foreach (var listing in listings) { var item = items.FirstOrDefault(i => i.Id == listing.ItemId); if (item != null && !String.IsNullOrEmpty(item.SourceMarketId)) { sleeper.NextStep(); var result = api.UpdateQuantityByLocation(long.Parse(item.SourceMarketId), listing.RealQuantity, true); if (result.Status == CallStatus.Success) { listing.QuantityUpdateRequested = false; listing.AmazonRealQuantity = listing.RealQuantity; db.Commit(); _log.Info("Qty updated, listingId=" + listing.ListingId + ", sendQty=" + listing.RealQuantity); } else { listingWithErrorList.Add(listing); _log.Info("Can't update qty, result=" + result.ToString()); } } else { if (item == null) { _log.Warn("Can't find item, for listing=" + listing.ListingId); } else { if (!item.DropShipperId.HasValue) { _log.Warn("Item hasn't dropShipperId, itemId=" + item.Id); } if (String.IsNullOrEmpty(item?.SourceMarketId)) { _log.Warn("Item hasn't sourceMarketId, itemId=" + item.Id); } } } } } _log.Info("End SendInventoryUpdates"); }
private IList <ItemBuyBoxResponse> RequestItemsBuyBoxByProductApi(ILogService logger, IList <ItemDTO> items, AmazonApi api) { var results = new List <ItemBuyBoxResponse>(); var index = 0; var step = 20; var sleep = new StepSleeper(TimeSpan.FromSeconds(1), 10); /* * Maximum request quota Restore rate Hourly request quota * 20 requests 10 items every second 36000 requests per hour */ while (index < items.Count) { var checkedItems = items.Skip(index).Take(step).ToList(); var resp = RetryHelper.ActionWithRetriesWithCallResult(() => api.GetCompetitivePricingForSKU(checkedItems.Select(i => i.SKU).ToList()), logger); if (resp.IsFail) { var message = resp.Message ?? ""; //Request is throttled ---> Amazon.OrdersApi.Runtime.MwsException: Request is throttled //... if (message.IndexOf("ServiceUnavailable", StringComparison.InvariantCultureIgnoreCase) >= 0 || message.IndexOf("throttled", StringComparison.InvariantCultureIgnoreCase) >= 0) { //do sleep } //NOTE: temporary do always _log.Info("Sleep 5 min"); Thread.Sleep(5 * 60 * 1000); } else { foreach (var item in resp.Data) { results.Add(new ItemBuyBoxResponse() { ASIN = item.ASIN, CheckedDate = DateTime.UtcNow, Status = item.BuyBoxStatus ?? BuyBoxStatusCode.NoWinner, WinnerPrice = item.LowestPrice, }); } index += step; sleep.NextStep(); } } return(results); }
public void UpdateMyPrice(AmazonApi api, IUnitOfWork db) { var lastUpdate = _time.GetUtcTime().AddHours(-12); var allItems = db.Listings .GetAll() .Where(p => p.Market == (int)api.Market && p.MarketplaceId == api.MarketplaceId && !p.IsRemoved && (!p.PriceFromMarketUpdatedDate.HasValue || p.PriceFromMarketUpdatedDate < lastUpdate)) .ToList(); var index = 0; var stepSize = 20; var stepSleeper = new StepSleeper(TimeSpan.FromSeconds(1), 5); while (index < allItems.Count) { try { var itemsToUpdate = allItems.Skip(index).Take(stepSize).ToList(); var skuList = itemsToUpdate.Select(i => i.SKU).ToList(); var items = api.GetMyPriceBySKU(skuList); foreach (var item in items) { var toUpdate = itemsToUpdate.FirstOrDefault(pi => pi.SKU == item.SKU); if (toUpdate != null) { toUpdate.ListingPriceFromMarket = item.ListingPriceFromMarket; toUpdate.ShippingPriceFromMarket = item.ShippingPriceFromMarket; toUpdate.ReqularPriceFromMarket = item.ReqularPriceFromMarket; toUpdate.PriceFromMarketUpdatedDate = _time.GetUtcTime(); } } } catch (Exception ex) { _log.Error("Error when updating rank for index: " + index, ex); } index += stepSize; db.Commit(); stepSleeper.NextStep(); } }
public void ProcessUnpublishedRequests(ShopifyApi api) { _log.Info("Begin ProcessUnpublishedRequests"); var sleeper = new StepSleeper(TimeSpan.FromSeconds(2), 1); using (var db = _dbFactory.GetRWDb()) { var items = db.Items.GetAllViewAsDto().Where(i => i.Market == (int)api.Market && (i.MarketplaceId == api.MarketplaceId || String.IsNullOrEmpty(api.MarketplaceId)) && i.PublishedStatus == (int)PublishedStatuses.HasUnpublishRequest) .ToList(); var parentItemAsins = items.Select(i => i.ParentASIN).Distinct().ToList(); var parentItemsToUnpublish = db.ParentItems.GetAllAsDto().Where(pi => pi.Market == (int)api.Market && (pi.MarketplaceId == api.MarketplaceId || String.IsNullOrEmpty(api.MarketplaceId)) && parentItemAsins.Contains(pi.ASIN)) .ToList(); foreach (var parentItem in parentItemsToUnpublish) { var itemId = StringHelper.TryGetLong(parentItem.SourceMarketId); if (!itemId.HasValue) { _log.Error("Item has empty SourceMarketId"); continue; } sleeper.NextStep(); var result = api.UnpublishItem(itemId.Value); if (result.IsSuccess) { _log.Info("ASIN was unpublished=" + parentItem.ASIN + ", sourceMarketId=" + parentItem.SourceMarketId); var dbItemList = db.Items.GetAll().Where(i => i.ParentASIN == parentItem.ASIN && i.Market == parentItem.Market && (i.MarketplaceId == parentItem.MarketplaceId || String.IsNullOrEmpty(parentItem.MarketplaceId))) .ToList(); foreach (var dbItem in dbItemList) { _log.Info("Unpublished variation, id=" + dbItem.SourceMarketId); dbItem.ItemPublishedStatus = (int)PublishedStatuses.Unpublished; db.Commit(); } } } } _log.Info("End ProcessUnpublishedRequests"); }
public void UpdateLowestPrice(AmazonApi api, IUnitOfWork db) { var allItems = db.Listings.GetAll().Where(p => p.Market == (int)api.Market && p.MarketplaceId == api.MarketplaceId && !p.IsRemoved).ToList(); var index = 0; var stepSize = 20; var stepSleeper = new StepSleeper(TimeSpan.FromSeconds(1), 10); while (index < allItems.Count) { try { var itemsToUpdate = allItems.Skip(index).Take(stepSize).ToList(); var skuList = itemsToUpdate.Select(i => i.SKU).ToList(); var items = api.GetCompetitivePricingForSKU(skuList); foreach (var item in items) { var toUpdate = itemsToUpdate.FirstOrDefault(pi => pi.SKU == item.SKU); if (toUpdate != null && item.LowestPrice.HasValue) { toUpdate.LowestPrice = item.LowestPrice; toUpdate.LowestPriceUpdateDate = _time.GetAppNowTime(); } } } catch (Exception ex) { _log.Error("Error when updating rank for index: " + index, ex); } index += stepSize; stepSleeper.NextStep(); } db.Commit(); }
public void UpdateRatingByProductApi(AmazonApi api, IUnitOfWork db) { var allParentItems = db.ParentItems.GetAll().Where(p => p.Market == (int)api.Market && p.MarketplaceId == api.MarketplaceId).ToList(); var index = 0; var stepSize = 20; var stepSleeper = new StepSleeper(TimeSpan.FromSeconds(1), 10); while (index < allParentItems.Count) { try { var parentItems = allParentItems.Skip(index).Take(stepSize).ToList(); var asinList = parentItems.Select(i => i.ASIN).ToList(); var items = api.GetCompetitivePricingForSKU(asinList); foreach (var item in items) { var toUpdate = parentItems.FirstOrDefault(pi => pi.ASIN == item.ASIN); if (toUpdate != null) { toUpdate.Rank = (int?)item.Rank; } } } catch (Exception ex) { _log.Error("Error when updating rank for index: " + index, ex); } index += stepSize; stepSleeper.NextStep(); } db.Commit(); }
public void SendPriceUpdates(ShopifyApi api) { _log.Info("Begin SendPriceUpdates"); var today = _time.GetAppNowTime().Date; var sleeper = new StepSleeper(TimeSpan.FromSeconds(2), 1); using (var db = _dbFactory.GetRWDb()) { var itemQuery = from l in db.Listings.GetAll() join i in db.Items.GetAll() on l.ItemId equals i.Id join s in db.Styles.GetAll() on i.StyleId equals s.Id join sale in db.StyleItemSaleToListings.GetAllListingSaleAsDTO() on l.Id equals sale.ListingId into withSale from sale in withSale.DefaultIfEmpty() where l.PriceUpdateRequested && (i.ItemPublishedStatus == (int)PublishedStatuses.Published || i.ItemPublishedStatus == (int)PublishedStatuses.Unpublished) && i.Market == (int)api.Market && (i.MarketplaceId == api.MarketplaceId || String.IsNullOrEmpty(api.MarketplaceId)) && !l.IsRemoved select new ItemDTO() { ListingEntityId = l.Id, SourceMarketId = i.SourceMarketId, SKU = l.SKU, StyleId = i.StyleId, CurrentPrice = l.CurrentPrice, ListPrice = s.MSRP, SalePrice = sale != null ? sale.SalePrice : null, SaleStartDate = sale != null ? sale.SaleStartDate : null, SaleEndDate = sale != null ? sale.SaleEndDate : null, }; var listingWithErrorList = new List <ItemDTO>(); var items = itemQuery.ToList(); foreach (var item in items) { //var item = items.FirstOrDefault(i => i.Id == listing.ItemId); if (!String.IsNullOrEmpty(item.SourceMarketId)) { sleeper.NextStep(); var result = api.UpdatePrice(StringHelper.TryGetLong(item.SourceMarketId).Value, item.CurrentPrice, item.ListPrice //item.SalePrice, //item.SaleStartDate, //item.SaleEndDate ); if (result.Status == CallStatus.Success) { var dbListing = db.Listings.GetAll().FirstOrDefault(i => i.Id == item.ListingEntityId); dbListing.PriceUpdateRequested = false; dbListing.AmazonCurrentPrice = item.CurrentPrice; db.Commit(); _log.Info("Price updated, listingId=" + dbListing.ListingId + ", newPrice=" + dbListing.CurrentPrice); } else { listingWithErrorList.Add(item); _log.Info("Can't update qty, result=" + result.ToString()); } } else { if (String.IsNullOrEmpty(item.SourceMarketId)) { _log.Warn("Item hasn't sourceMarketId, itemId=" + item.Id); } } } } _log.Info("End SendPriceUpdates"); }