Example #1
0
        public async Task AddChannel(string ytNameOrId, string vkToken, string groupId, string userId, bool isYtName = true)
        {
            if (isYtName && _channels.FirstOrDefault(x => x.YtName == ytNameOrId) != null)
            {
                throw  new Exception("Error. Channel already exists");
            }
            // else if (!isYtName && !IsYtChannelIdExist(ytNameOrId))
            //     throw  new Exception("Error. Channel with the id doesn't exist");

            var channel = new Channel()
            {
                YtName    = isYtName ? ytNameOrId : await GetYtNameByChannelId(ytNameOrId),
                VkToken   = vkToken,
                VkGroupId = groupId,
                VkUserId  = userId,
                YtId      = isYtName ? GetYtChannelId(ytNameOrId) : ytNameOrId
            };

            while (channel.YtId == null)
            {
                Logger.Error("Channel not found\nEnter user name: ");

                string name;
                do
                {
                    name = Console.ReadLine();
                } while (string.IsNullOrEmpty(name));
                channel.YtId   = GetYtChannelId(name);
                channel.YtName = name;
            }
            while (channel.YtName == null)
            {
                Logger.Error("Channel not found\nEnter channel id: ");

                string name;
                do
                {
                    name = Console.ReadLine();
                } while (string.IsNullOrEmpty(name));
                channel.YtId   = name;
                channel.YtName = await GetYtNameByChannelId(name);
            }
            while (true)
            {
                try
                {
                    VkApi test = new VkApi();
                    test.Authorize(new ApiAuthParams()
                    {
                        AccessToken   = channel.VkToken,
                        ApplicationId = _settings.ApplicationId,
                        UserId        = long.Parse(channel.VkUserId)
                    });
                    if (!test.IsAuthorized)
                    {
                        throw new Exception("Error Vk authorization data not valid");
                    }
                    break;
                }
                catch (Exception ex)
                {
                    if (ex.InnerException != null)
                    {
                        Logger.Error(ex.Message + Environment.NewLine + ex.InnerException.StackTrace);
                    }
                    else
                    {
                        Logger.Error(ex.Message);
                    }

                    string vk;
                    do
                    {
                        Logger.Error("Enter vk group token: ");
                        vk = Console.ReadLine();
                    } while (string.IsNullOrEmpty(vk));
                    channel.VkToken = vk;
                }
            }

            if (_channels.FirstOrDefault(x => x.YtName == channel.YtName) != null)
            {
                return;
            }

            // using (var ytApi = new YouTubeService(new BaseClientService.Initializer() {ApiKey = _ytKey}))
            // {
            //     var searchListRequest = ytApi.Videos.List("snippet");
            //     searchListRequest.ChannelId = channel.YtId;
            //     searchListRequest.Order = SearchResource.ListRequest.OrderEnum.Date;
            //     searchListRequest.Type = "video";
            //     //searchListRequest.MaxResults = 20;
            //     searchListRequest.PublishedAfter = DateTime.UtcNow.Subtract(TimeSpan.FromHours(24));
            //
            //     channel.VideoStack.PushRange(searchListRequest.Execute().Items.Select(x => x.Id.VideoId));
            // }
            var videos = _youtubeService.GetVideoList(channel.PlaylistId);

            _channels.Add(channel);

            Logger.Info($"Channel {channel.YtName} has been added");

            UpdateChannelsInFile();
        }