public async Task <IActionResult> Create([Bind("Id,MessageId,Date,Price,MessageText,Remark")]
                                                 TomorrowMeltedGold tomorrowMeltedGold)
        {
            if (ModelState.IsValid)
            {
                _context.Add(tomorrowMeltedGold);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(tomorrowMeltedGold));
        }
Ejemplo n.º 2
0
 private void CheckGoldChannelMessage(TdApi.Message message)
 {
     try
     {
         //DataReceiveHelper.Information.AppendLine("Gold Message Received");
         var messageText = "";
         if (message.Content is TdApi.MessageContent.MessageText text)
         {
             messageText = text.Text.Text;
         }
         var price = FindGoldPrice(messageText);
         if (price <= 0)
         {
             return;
         }
         var tomorrowMeltedGold = new TomorrowMeltedGold
         {
             MessageId   = message.Id,
             MessageText = messageText,
             Date        = UnixTimeStampToDateTime(message.Date),
             Price       = price
         };
         var goldOptionsBuilder = new DbContextOptionsBuilder <TomorrowMeltedGoldContext>();
         goldOptionsBuilder.UseSqlServer(_connectionString);
         using var goldContext =
                   new TomorrowMeltedGoldContext(goldOptionsBuilder.Options);
         if (goldContext.TomorrowMeltedGold.Any(item =>
                                                item.MessageId == tomorrowMeltedGold.MessageId))
         {
             return;
         }
         goldContext.Add(tomorrowMeltedGold);
         goldContext.SaveChanges();
         //DataReceiveHelper.Information.AppendLine("New Gold Message Added");
     }
     catch (TdException e)
     {
         var error = e.Error;
         DataReceiveHelper.Information.AppendLine("CheckGoldChannelMessage-1 " + error.Message);
     }
     catch (Exception ex)
     {
         DataReceiveHelper.Information.AppendLine("CheckGoldChannelMessage-2 " + ex.Message);
     }
 }
Ejemplo n.º 3
0
        private async void GetOldData()
        {
            try
            {
                var goldOptionsBuilder = new DbContextOptionsBuilder <TomorrowMeltedGoldContext>();
                goldOptionsBuilder.UseSqlServer(_connectionString);
                var dollarOptionsBuilder = new DbContextOptionsBuilder <TomorrowHeratDollarContext>();
                dollarOptionsBuilder.UseSqlServer(_connectionString);
                TdApi.Chat goldChannel   = null;
                TdApi.Chat dollarChannel = null;
                using var client = await DataReceiveHelper.NewClientAsync();

                await foreach (var chat in GetChannels(client))
                {
                    //DataReceiveHelper.Information.AppendLine($"{chat.Title} ID {chat.Id}");
                    if (chat.Title.Equals(GoldChannelName))
                    {
                        goldChannel = chat;
                    }
                    if (chat.Title.Equals(DollarChannelName))
                    {
                        dollarChannel = chat;
                    }
                }
                var goldMessageId   = goldChannel?.LastMessage.Id ?? 0;
                var dollarMessageId = dollarChannel?.LastMessage.Id ?? 0;
                for (var j = 0; j < 10000; j++)
                {
                    try
                    {
                        if (goldChannel != null)
                        {
                            var messages = await client.GetChatHistoryAsync(goldChannel.Id, goldMessageId, 0, 10);

                            foreach (var message in messages.Messages_)
                            {
                                goldMessageId = message.Id;
                                var messageText = "";
                                if (message.Content is TdApi.MessageContent.MessageText text)
                                {
                                    messageText = text.Text.Text;
                                }
                                var price = FindGoldPrice(messageText);
                                if (price <= 0)
                                {
                                    continue;
                                }
                                var tomorrowMeltedGold = new TomorrowMeltedGold
                                {
                                    MessageId   = message.Id,
                                    MessageText = messageText,
                                    Date        = UnixTimeStampToDateTime(message.Date),
                                    Price       = price
                                };
                                await using var goldContext = new TomorrowMeltedGoldContext(goldOptionsBuilder.Options);
                                if (goldContext.TomorrowMeltedGold.All(item =>
                                                                       item.MessageId != tomorrowMeltedGold.MessageId))
                                {
                                    goldContext.Add(tomorrowMeltedGold);
                                    await goldContext.SaveChangesAsync();
                                }
                            }
                        }

                        if (dollarChannel != null)
                        {
                            var messages =
                                await client.GetChatHistoryAsync(dollarChannel.Id, dollarMessageId, 0, 10);

                            foreach (var message in messages.Messages_)
                            {
                                dollarMessageId = message.Id;
                                var messageText = "";
                                if (message.Content is TdApi.MessageContent.MessageText text)
                                {
                                    messageText = text.Text.Text;
                                }
                                var price = FindDollarPrice(messageText);
                                if (price <= 0)
                                {
                                    continue;
                                }
                                var tomorrowHeratDollar = new TomorrowHeratDollar
                                {
                                    MessageId   = message.Id,
                                    MessageText = messageText,
                                    Date        = UnixTimeStampToDateTime(message.Date),
                                    Price       = price
                                };
                                await using var dollarContext =
                                                new TomorrowHeratDollarContext(dollarOptionsBuilder.Options);
                                if (dollarContext.TomorrowHeratDollar.All(item =>
                                                                          item.MessageId != tomorrowHeratDollar.MessageId))
                                {
                                    dollarContext.Add(tomorrowHeratDollar);
                                    await dollarContext.SaveChangesAsync();
                                }
                            }
                        }
                    }
                    catch (TdException e)
                    {
                        var error = e.Error;
                        DataReceiveHelper.Information.AppendLine("GetOldData-1-1 " + error.Message);
                    }
                    catch (Exception ex)
                    {
                        DataReceiveHelper.Information.AppendLine("GetOldData-1-2 " + ex.Message);
                    }

                    _logger.LogInformation("DataReceiver Service running at: {time}", DateTimeOffset.Now);
                    DataReceiveHelper.LoopCount++;
                    await Task.Delay(TimeSpan.FromSeconds(_random.Next(3, 7)));
                }
            }
            catch (TdException e)
            {
                var error = e.Error;
                DataReceiveHelper.Information.AppendLine("GetOldData-2-1 " + error.Message);
            }
            catch (Exception ex)
            {
                DataReceiveHelper.Information.AppendLine("GetOldData-2-2 " + ex.Message);
            }
        }