Exemplo n.º 1
0
        private async void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            if (!itemsQueue.Any())
            {
                return;
            }

            var item = itemsQueue.Dequeue();

            try
            {
                var newInfo = item.Source switch
                {
                    PullAndBear.SHOP_NAME => await _pullAndBearClient.GetItemInfoAsync(item.Url),
                    Bershka.SHOP_NAME => await _bershkaClient.GetItemInfoAsync(item.Url),
                    _ => throw new Exception(Exceptions.UNKNOWN_SHOP_EXCEPTION),
                };
                if (item.Status != newInfo.Status || item.Price != newInfo.Price)
                {
                    await _botService.SendMessageMarkdownV2Async(
                        item.ChatId,
                        _textConverter.ToString(item, newInfo));
                }
                _updateInfoHelper.GetUpdatedItem(ref item, newInfo);
                await _trackingRepository.UpdateInfoOfItemAsync(item);
            }
            catch (Exception ex)
            {
                // it's ok; lets update item information next time;
            }
            if ((DateTime.Now - item.LastUpdateDate).TotalHours > maxTimeWithoutUpdateIntervalHours)
            {
                await _trackingRepository.RemoveItemAsync(item.Url);

                await _botService.SendMessageAsync(
                    item.ChatId,
                    WANNA_REMOVE_ITEM_EXCEPTION);
            }
            else
            {
                itemsQueue.Enqueue(item);
                if ((DateTime.Now - item.LastUpdateDate).TotalHours > maxUpdateTimeIntervalHours)
                {
                    await _botService.SendMessageAsync(
                        item.ChatId,
                        CANT_UPDATE_EXCEPTION);
                }
            }
        }
Exemplo n.º 2
0
        public async Task <Item> AddNewItemAsync(Message message)
        {
            var url = Regex.Match(message.Text, Common.UrlTemplate).Value;

            if (string.IsNullOrEmpty(url))
            {
                throw new Exception(NEED_INLINE_LINK_EXCEPTION);
            }

            if (await _trackingRepository.IsTrackedAsync(url, message.From.Id))
            {
                throw new Exception(Exceptions.ALREADY_TRACKED_EXCEPTION);
            }

            var shopName = _shopDefiner.GetShopName(url);
            var newInfo  = shopName switch
            {
                PullAndBear.SHOP_NAME => await _pullAndBearClient.GetItemInfoAsync(url),
                Bershka.SHOP_NAME => await _bershkaClient.GetItemInfoAsync(url),
                _ => throw new Exception(Exceptions.UNKNOWN_SHOP_EXCEPTION),
            };

            var newItem = _mapper.Map <ItemOnline, Item>(newInfo);

            newItem.Url = url;
            newItem.StartTrackingDate = DateTime.Now;
            newItem.LastUpdateDate    = newItem.StartTrackingDate;
            newItem.Source            = shopName;
            newItem.ChatId            = message.Chat.Id;
            newItem.UserId            = message.From.Id;

            var itemId = await _trackingRepository.AddNewItemAsync(newItem);

            newItem.ItemId = itemId;
            _timerService.Enqueue(newItem);
            return(newItem);
        }