/// <summary> /// Запускаем ежечасно, желательно в отдельном потоке, поставить приоритет потока на высокий, а потом выполнять параллельно. /// </summary> public void Process(object intChannelDelaySeconds) { this.RequestsDelaySeconds = (int)intChannelDelaySeconds; using (Db db = new Db(HelperDatabase.DB_OPTIONS)) { //1) Взять из базы список всех каналов. List <TelegramChannel> channels = DbMethods.GetAllChannels(db); if (channels == null || channels.Count == 0) { Debug.Error("Нет каналов в базе для сбора статистики!"); return; } //Thread.Sleep(this.RequestsDelaySeconds); Thread.Sleep(new Random(Convert.ToInt32(DateTime.Now.Ticks % 100000)).Next(10, 20) * 1000); //Запускаем цикл по каналам //Получить запросом канал Добавить в базу кол-во подписчиков канала на текущий час. foreach (var channel in channels) { Debug.Log($"Берем статистику канала [{channel.Username}]"); var channelInfo = ChannelMethods.GetAllInfoChannel(GlobalVars.Client, channel.Username).Result; DbMethods.AddStatisticsChannelToDb(db, channelInfo); //Взять все посты у канала, через запрос. var posts = ChannelMethods.GetAllPosts(GlobalVars.Client, channel.Username).Result; foreach (var post in posts) { var p = post as TLMessage; if (Equals(p, null)) { continue; } //Если пост новый и еще не добавлен в базу, то добавить в базу. TelegramPost telePost = DbMethods.AddTelegramPostIfNeed(db, channel, p); DbMethods.AddStatisticsPostToDb(db, channel, p); } //Делаем задержку по времени, чтобы не банили запросы за превыщение лимита //Берем данные по каналу, по его постам и делаем задержку. //Thread.Sleep(this.RequestsDelaySeconds * 1000); Thread.Sleep(new Random(Convert.ToInt32(DateTime.Now.Ticks % 100000)).Next(10, 20) * 1000); } Debug.Log("Сбор статистики завершен!"); } }
internal static Message FormatMessage(Message message, Components components, string token, string imId = null, string channelId = null, bool isPrivateMessage = false, bool isChannelMessage = false) { string userName = UserMethods.GetUserName(components.Users, message.UserId); if (userName == null) { components.Users = UserHelper.GetUsers(token); userName = UserMethods.GetUserName(components.Users, message.UserId); } string botName = BotMethods.GetBotName(components.Bots, message.BotId); if (botName == null) { components.Bots = BotsHelper.GetBots(token); botName = BotMethods.GetBotName(components.Bots, message.BotId); } string imName = ImMethos.GetImName(components.Ims, components.Users, imId); if (imName == null) { components.Ims = ImHelper.GetIms(token); imName = ImMethos.GetImName(components.Ims, components.Users, imId); } string channelName = ChannelMethods.GetChannelName(components.Channels, channelId); if (channelName == null) { components.Channels = ChannelHelper.GetChannels(token); channelName = ChannelMethods.GetChannelName(components.Channels, channelId); } message.UserName = botName ?? userName; message.ChannelName = isPrivateMessage ? null : (imName ?? channelName); message.ChannelId = channelId; message.IsPrivateMessage = isPrivateMessage; message.IsGroupMessage = isChannelMessage; return(message); }
static void InitDbForTest() { var c1 = ChannelMethods.GetAllInfoChannel(GlobalVars.Client, "make_your_style").Result; Thread.Sleep(new Random(Convert.ToInt32(DateTime.Now.Ticks % 100000)).Next(10, 20) * 1000); var c2 = ChannelMethods.GetAllInfoChannel(GlobalVars.Client, "fedorinsights").Result; Thread.Sleep(new Random(Convert.ToInt32(DateTime.Now.Ticks % 100000)).Next(10, 20) * 1000); var c3 = ChannelMethods.GetAllInfoChannel(GlobalVars.Client, "PumpTheMind").Result; Thread.Sleep(new Random(Convert.ToInt32(DateTime.Now.Ticks % 100000)).Next(10, 20) * 1000); var c4 = ChannelMethods.GetAllInfoChannel(GlobalVars.Client, "vot_idea").Result; Thread.Sleep(new Random(Convert.ToInt32(DateTime.Now.Ticks % 100000)).Next(10, 20) * 1000); var c5 = ChannelMethods.GetAllInfoChannel(GlobalVars.Client, "TransformatorTV").Result; Thread.Sleep(new Random(Convert.ToInt32(DateTime.Now.Ticks % 100000)).Next(10, 20) * 1000); var c6 = ChannelMethods.GetAllInfoChannel(GlobalVars.Client, "temablog").Result; Thread.Sleep(new Random(Convert.ToInt32(DateTime.Now.Ticks % 100000)).Next(10, 20) * 1000); var c7 = ChannelMethods.GetAllInfoChannel(GlobalVars.Client, "yurydud").Result; Thread.Sleep(new Random(Convert.ToInt32(DateTime.Now.Ticks % 100000)).Next(10, 20) * 1000); var c8 = ChannelMethods.GetAllInfoChannel(GlobalVars.Client, "meduzalive").Result; Thread.Sleep(new Random(Convert.ToInt32(DateTime.Now.Ticks % 100000)).Next(10, 20) * 1000); var c9 = ChannelMethods.GetAllInfoChannel(GlobalVars.Client, "DavydovIn").Result; Thread.Sleep(new Random(Convert.ToInt32(DateTime.Now.Ticks % 100000)).Next(10, 20) * 1000); var c10 = ChannelMethods.GetAllInfoChannel(GlobalVars.Client, "breakingmash").Result; Thread.Sleep(new Random(Convert.ToInt32(DateTime.Now.Ticks % 100000)).Next(10, 20) * 1000); var c11 = ChannelMethods.GetAllInfoChannel(GlobalVars.Client, "corona").Result; Thread.Sleep(new Random(Convert.ToInt32(DateTime.Now.Ticks % 100000)).Next(10, 20) * 1000); var c12 = ChannelMethods.GetAllInfoChannel(GlobalVars.Client, "oldlentach").Result; Thread.Sleep(new Random(Convert.ToInt32(DateTime.Now.Ticks % 100000)).Next(10, 20) * 1000); var c13 = ChannelMethods.GetAllInfoChannel(GlobalVars.Client, "Lifeyt").Result; Thread.Sleep(new Random(Convert.ToInt32(DateTime.Now.Ticks % 100000)).Next(10, 20) * 1000); var c14 = ChannelMethods.GetAllInfoChannel(GlobalVars.Client, "dvachannel").Result; Thread.Sleep(new Random(Convert.ToInt32(DateTime.Now.Ticks % 100000)).Next(10, 20) * 1000); var c15 = ChannelMethods.GetAllInfoChannel(GlobalVars.Client, "Cbpub").Result; Thread.Sleep(new Random(Convert.ToInt32(DateTime.Now.Ticks % 100000)).Next(10, 20) * 1000); var c16 = ChannelMethods.GetAllInfoChannel(GlobalVars.Client, "exboyfriend").Result; Thread.Sleep(new Random(Convert.ToInt32(DateTime.Now.Ticks % 100000)).Next(10, 20) * 1000); var c17 = ChannelMethods.GetAllInfoChannel(GlobalVars.Client, "Ovsyanka").Result; Thread.Sleep(new Random(Convert.ToInt32(DateTime.Now.Ticks % 100000)).Next(10, 20) * 1000); var c18 = ChannelMethods.GetAllInfoChannel(GlobalVars.Client, "hardchannel").Result; Thread.Sleep(new Random(Convert.ToInt32(DateTime.Now.Ticks % 100000)).Next(10, 20) * 1000); var c19 = ChannelMethods.GetAllInfoChannel(GlobalVars.Client, "paperpublic").Result; Thread.Sleep(new Random(Convert.ToInt32(DateTime.Now.Ticks % 100000)).Next(10, 20) * 1000); var c20 = ChannelMethods.GetAllInfoChannel(GlobalVars.Client, "memes").Result; Thread.Sleep(new Random(Convert.ToInt32(DateTime.Now.Ticks % 100000)).Next(10, 20) * 1000); using (Db db = new Db(HelperDatabase.DB_OPTIONS)) { DbMethods.AddTelegramChannelIfNeed(db, c1); DbMethods.AddTelegramChannelIfNeed(db, c2); DbMethods.AddTelegramChannelIfNeed(db, c3); DbMethods.AddTelegramChannelIfNeed(db, c4); DbMethods.AddTelegramChannelIfNeed(db, c5); DbMethods.AddTelegramChannelIfNeed(db, c6); DbMethods.AddTelegramChannelIfNeed(db, c7); DbMethods.AddTelegramChannelIfNeed(db, c8); DbMethods.AddTelegramChannelIfNeed(db, c9); DbMethods.AddTelegramChannelIfNeed(db, c10); DbMethods.AddTelegramChannelIfNeed(db, c11); DbMethods.AddTelegramChannelIfNeed(db, c12); DbMethods.AddTelegramChannelIfNeed(db, c13); DbMethods.AddTelegramChannelIfNeed(db, c14); DbMethods.AddTelegramChannelIfNeed(db, c15); DbMethods.AddTelegramChannelIfNeed(db, c16); DbMethods.AddTelegramChannelIfNeed(db, c17); DbMethods.AddTelegramChannelIfNeed(db, c18); DbMethods.AddTelegramChannelIfNeed(db, c19); DbMethods.AddTelegramChannelIfNeed(db, c20); var channels = DbMethods.GetAllChannels(db); foreach (var c in channels) { Debug.Log($"Channel {c.Username} | {c.TelegramId.ToString()} | {c.Description}"); } } }