Example #1
0
        public async Task <IEnumerable <Package> > LoginAndFetchPackagesAsync(string username, string password)
        {
            try
            {
                var path = await _flypack.LoginAsync(username, password);

                var packages = await _flypack.GetPackagesAsync(path, username);

                if (_paths != null)
                {
                    _paths[username] = path;
                }
                // TODO: if there are differences, might be a good idea to update the cache and notify the other users about the changes.
                //if (_currentPackages != null) _currentPackages[username] = packages;

                return(packages);
            }
            catch (Exception ex)
            {
                LogFailedLogin(ex, new LoggedUser {
                    Username = username
                });
            }

            return(Array.Empty <Package>());
        }
Example #2
0
        public async Task StartAsync(TelegramBotClient client, string channelIdentifier, CancellationToken cancellationToken)
        {
            _logger.LogInformation("Login into Flypack with account: {account}", _settings.Username);
            path = await _flypack.LoginAsync(_settings.Username, _settings.Password);

            if (string.IsNullOrEmpty(path))
            {
                LogFailedLogin(client, channelIdentifier); return;
            }

            while (!cancellationToken.IsCancellationRequested)
            {
                _logger.LogDebug("Fetch running at: {time}", DateTime.Now);
                var packages = await _flypack.GetPackagesAsync(path);

                if (packages == null && _retriesCount < MAX_RETRIES)
                {
                    LogFailedListPackages(client, channelIdentifier, path);
                    path = await _flypack.LoginAsync(_settings.Username, _settings.Password);

                    _retriesCount++; continue;
                }
                else if (_retriesCount >= MAX_RETRIES)
                {
                    LogMaxLoginAttemptsReached(client, channelIdentifier, path);
                    break;
                }
                else
                {
                    _retriesCount = 0;
                }

                packages = FilterPackages(packages);

                if (packages.Any())
                {
                    var message = ParseMessageFor(packages);
                    await client.SendTextMessageAsync(
                        chatId : channelIdentifier,
                        text : message,
                        parseMode : ParseMode.Markdown
                        );
                }
                await Task.Delay(TimeSpan.FromMinutes(_settings.FetchInterval), cancellationToken);
            }

            _logger.LogInformation("Cancellation requested");
        }