Exemplo n.º 1
0
        private async Task <bool> RefreshChannelHelper(int channelId, long accessHash)
        {
            // get channel info
            TLInterfacePackage tl      = GetAvailableTLInterface();
            Channel            channel = await tl.Interface.GetChannelAsync(channelId, accessHash);

            tl.LastCallTime = DateTime.Now;
            tl.IsAvailable  = true;
            if (channel != null)
            {
                tl = GetAvailableTLInterface();
                ChannelFull channelFull = await tl.Interface.GetFullChannelAsync(channel.ChannelId, channel.AccessHash.Value);

                tl.LastCallTime = DateTime.Now;
                tl.IsAvailable  = true;
                // update channel
                db.UpdateChannel(channelId, new MongoChannel
                {
                    Title              = channel.Title,
                    Username           = channel.Username,
                    About              = channelFull.About,
                    ParticipantsCounts = new List <CountTime>()
                    {
                        new CountTime(channelFull.ParticipantsCount, DateTime.Now)
                    }
                });
                Log(string.Format("Channel '{0}' (@{1}) with {2} members has been refreshed.", channel.Title, channel.Username, channelFull.ParticipantsCount));
                return(true);
            }
            return(false);
        }
Exemplo n.º 2
0
        private async Task <bool> AddChannelHelper(string username, bool active)
        {
            // get channel info
            TLInterfacePackage tl      = GetAvailableTLInterface();
            Channel            channel = await tl.Interface.GetChannelAsync(username);

            tl.LastCallTime = DateTime.Now;
            tl.IsAvailable  = true;
            if (channel != null)
            {
                tl = GetAvailableTLInterface();
                ChannelFull channelFull = await tl.Interface.GetFullChannelAsync(channel.ChannelId, channel.AccessHash.Value);

                tl.LastCallTime = DateTime.Now;
                tl.IsAvailable  = true;
                if (!db.IsChannelAdded(channel.ChannelId))
                {
                    // refresh channel on duplicate username
                    if (db.IsChannelAdded(username))
                    {
                        MongoChannel channelToRefresh = db.GetChannel(username);
                        await RefreshChannelHelper(channelToRefresh.ChannelId, channelToRefresh.AccessHash.Value);
                    }
                    // add channel
                    db.AddChannel(new MongoChannel
                    {
                        ChannelId          = channel.ChannelId,
                        AccessHash         = channel.AccessHash,
                        Title              = channel.Title,
                        Username           = channel.Username,
                        About              = channelFull.About,
                        ParticipantsCounts = new List <CountTime>()
                        {
                            new CountTime(channelFull.ParticipantsCount, DateTime.Now)
                        },
                        IsActive = active
                    });
                    Log(string.Format("Channel '{0}' (@{1}) with {2} members has been added.", channel.Title, channel.Username, channelFull.ParticipantsCount));
                    return(true);
                }
                else
                {
                    // refresh channel if it already exists
                    await RefreshChannelHelper(channel.ChannelId, channel.AccessHash.Value);
                }
            }
            return(false);
        }
Exemplo n.º 3
0
        private TLInterfacePackage GetAvailableTLInterface()
        {
            TLInterfacePackage tLInterfacePackage = null;

            lock (_myLock)
            {
                while (tLInterfacePackage == null)
                {
                    try
                    {
                        tLInterfacePackage = _tlInterfacePackages.First(i => i.IsAvailable == true && i.LastCallTime.Add(TimeSpan.FromMilliseconds(_apiCallDelay)) < DateTime.Now);
                    }
                    catch (InvalidOperationException)
                    {
                        tLInterfacePackage = null;
                    }
                }
                tLInterfacePackage.IsAvailable = false;
            }
            return(tLInterfacePackage);
        }
Exemplo n.º 4
0
        private async Task <JobResult> ProcessGetFeedCommand(string[] args)
        {
            int  totalMessagesCount = 0;
            int  from    = DateTime.Now.ToIntSeconds();
            int  to      = DateTime.Now.ToIntSeconds();
            bool stopped = false;

            for (int i = 0; i < args.Length; i++)
            {
                if (args[i].ToLower() == "-from")
                {
                    try
                    {
                        from = DateTime.Parse(args[i + 1]).ToIntSeconds();
                    }
                    catch (Exception)
                    {
                        return(new JobResult("Invalid date after '-from'", JobStatus.Finished));
                    }
                }
                if (args[i].ToLower() == "-to")
                {
                    try
                    {
                        to = DateTime.Parse(args[i + 1]).ToIntSeconds();
                    }
                    catch (Exception)
                    {
                        return(new JobResult("Invalid date after '-to'", JobStatus.Finished));
                    }
                }
            }
            List <int> channelsIDs = db.GetActiveChannelsIDs();

            foreach (int channelId in channelsIDs)
            {
                if (_stop)
                {
                    stopped = true;
                    break;
                }
                MongoChannel channel = db.GetChannel(channelId);
                if (channel.CoveredTimePeriods == null)
                {
                    channel.CoveredTimePeriods = new TimePeriods();
                }
                int  currentChannelMessagesCount = 0;
                bool skipChannel     = false;
                int  time            = to;
                int  lastMessageTime = 0;
                if (channel.CoveredTimePeriods.ContainsLarger(time))
                {
                    lastMessageTime = time;
                }
                while (true)
                {
                    if (_stop)
                    {
                        stopped = true;
                        break;
                    }
                    if (time < from)
                    {
                        break;
                    }
                    time = channel.CoveredTimePeriods.LargestNonIncludedTimeFromPeriod(from, time);
                    if (time < from || skipChannel)
                    {
                        break;
                    }
                    int                addedMessagesCount = 0;
                    List <Message>     messages           = new List <Message>();
                    TLInterfacePackage tl = new TLInterfacePackage();
                    tl = GetAvailableTLInterface();
                    try
                    {
                        messages = await tl.Interface.GetChannelMessagesAsync(channel.ChannelId, channel.AccessHash.Value, null, time, null);

                        tl.LastCallTime = DateTime.Now;
                        tl.IsAvailable  = true;
                    }
                    catch (InvalidOperationException e)
                    {
                        Log("InvalidOperationException: " + e.Message);
                        return(new JobResult(JobStatus.Stopped));
                    }
                    if (messages.Count == 0)
                    {
                        break;
                    }
                    foreach (Message item in messages)
                    {
                        if (!db.IsMessageAdded(item.SourceChannel.ChannelId, item.MessageId))
                        {
                            // extract usernames from message text
                            foreach (string username in ExtractUsernamesHelper(item.Text))
                            {
                                if (!db.IsChannelAdded(username))
                                {
                                    if (!db.IsUnresolvedUsernameAdded(new MongoUsername {
                                        Username = username
                                    }))
                                    {
                                        db.AddUnresolvedUsername(new MongoUsername {
                                            Username = username
                                        });
                                    }
                                }
                            }
                            // add message source channel if it's not added yet.
                            if (!db.IsChannelAdded(item.SourceChannel.ChannelId))
                            {
                                if (item.SourceChannel.AccessHash.HasValue)
                                {
                                    await AddChannelHelper(item.SourceChannel.ChannelId, item.SourceChannel.AccessHash.Value, false);
                                }
                            }
                            // add message to database
                            MongoMessage messageToAdd = new MongoMessage
                            {
                                ChannelId = item.SourceChannel.ChannelId,
                                MessageId = item.MessageId,
                                Text      = item.Text,
                                Date      = item.OriginalDate ?? item.Date,
                                Views     = new List <CountTime>()
                                {
                                    item.Views
                                }
                            };
                            if (item.MediaContent != null)
                            {
                                if (string.IsNullOrEmpty(messageToAdd.Text))
                                {
                                    messageToAdd.Text = item.MediaContent.Caption;
                                }
                                messageToAdd.MediaType = item.MediaContent.Type;
                                messageToAdd.MediaSize = item.MediaContent.Size;
                                messageToAdd.FileName  = item.MediaContent.FileName;
                            }
                            db.AddMessage(messageToAdd);
                            addedMessagesCount++;
                        }
                        else if (messages.Count == 1)
                        {
                            skipChannel = true;
                        }
                    }
                    if (lastMessageTime == 0)
                    {
                        lastMessageTime = messages.Max(m => m.Date);
                    }
                    time = messages.Min(m => m.Date);
                    channel.CoveredTimePeriods.AddPeriod(new TimePeriod(time, lastMessageTime));
                    db.UpdateChannelCoveredTimePeriods(channel.ChannelId, channel.CoveredTimePeriods);
                    currentChannelMessagesCount += addedMessagesCount;
                    Log(string.Format("{0} new messages has been added from '{1}' (@{2}).", addedMessagesCount, channel.Title, channel.Username));
                    lastMessageTime = time;
                }
                totalMessagesCount += currentChannelMessagesCount;
                Log(string.Format("{0} total new messages has been added from '{1}' (@{2}).", currentChannelMessagesCount, channel.Title, channel.Username));
            }
            if (stopped)
            {
                return(new JobResult(JobStatus.Stopped));
            }
            string message = "";

            if (totalMessagesCount == 0)
            {
                message = "No new message has been added.";
            }
            else if (totalMessagesCount == 1)
            {
                message = "1 new message has been added.";
            }
            else
            {
                message = string.Format("{0} new messages has been added.", totalMessagesCount);
            }
            return(new JobResult(message, JobStatus.Finished));
        }