Beispiel #1
0
        getchannelshit(string channelid, string path)
        {
            var client = new YoutubeClient();

            IReadOnlyList <YoutubeExplode.Models.Video> video = null;
            int  retry2 = 10;
            bool Bretry = false;

            YoutubeExplode.Models.Channel channel = null;
            Console.WriteLine("Downloading channel started " + channelid);
            while (video == null)
            {
                if (retry2 == 0)
                {
                    Bretry = true;
                    break;
                }
                try
                {
                    video = await client.GetChannelUploadsAsync(channelid);

                    channel = await client.GetChannelAsync(channelid);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Retrying channel download " + (11 - retry2) + " attempt");
                }
                retry2--;
                if (video == null)
                {
                    Thread.Sleep(1000);
                }
                else
                {
                    break;
                }
            }

            //  var video = await client.GetChannelUploadsAsync(channelid);
            if (video != null)
            {
                try
                {
                    foreach (var x in video)
                    {
                        try
                        {
                            await getshit(x.Id, path, false, channel.Title);
                        }
                        catch (Exception e)
                        { Console.WriteLine("getshiterror\n" + e); }
                    }
                }
                catch (Exception x)
                {
                    Console.WriteLine(x);
                }
            }
            // Console.WriteLine("Downloading channel finished " + channelid);
        }
        public static async Task Execute(Update update)
        {
            try
            {
                var          userId = update.CallbackQuery.From.Id.ToString();
                ExtendedUser user   = null;
                user = Bot.GetUserById(userId);
                YoutubeExplode.Models.Channel author = null;
                string channelId = "";
                if (user != null)
                {
                    var CallbackQuery = update.CallbackQuery;
                    var CallbackData  = CallbackQuery.Data.Split('~');
                    //0 - btnName; 1 - videoId; 2 - chatId; 3 - rate; 4 - subscribed;
                    var    btnName    = CallbackData[0];
                    string videoId    = "";
                    long   chatId     = -1;
                    string rate       = null;
                    bool   subscribed = false;
                    if (CallbackData.Length > 1)
                    {
                        videoId = CallbackData[1];
                        if (CallbackData.Length > 2)
                        {
                            chatId = long.Parse(CallbackData[2]);
                            if (CallbackData.Length > 3)
                            {
                                rate       = CallbackData[3];
                                subscribed = CallbackData[4] == "t";
                            }
                        }
                        try
                        {
                            author    = await new YoutubeClient().GetVideoAuthorChannelAsync(videoId);
                            channelId = author.Id;
                        } catch
                        {
                            //videoid can be "-" if user wants to logout, so just ignore exception
                        }
                    }

                    switch (btnName)
                    {
                    case "l":     //like
                        if (user?.Credential != null)
                        {
                            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
                            {
                                HttpClientInitializer = user.Credential,
                                ApplicationName       = "TelegramBot",
                            });
                            await youtubeService.Videos.Rate(videoId, VideosResource.RateRequest.RatingEnum.Like).ExecuteAsync();

                            if (chatId != -1)
                            {
                                await Bot.EditMessageReplyMarkupAsync(chatId, CallbackQuery.Message.MessageId, await GetMarkupAsync(chatId, videoId, subscribed, "l"));
                            }
                        }
                        else
                        {
                            if (chatId != -1)
                            {
                                await Bot.SendTextMessageAsync(chatId, "Невозможно поставить оценку видео, вы должны войти в ваш аккаунт YouTube. Выполните команду /login");
                            }
                        }
                        break;

                    case "d":     //dislike
                        if (user?.Credential != null)
                        {
                            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
                            {
                                HttpClientInitializer = user.Credential,
                                ApplicationName       = "TelegramBot",
                            });
                            await youtubeService.Videos.Rate(videoId, VideosResource.RateRequest.RatingEnum.Dislike).ExecuteAsync();

                            if (chatId != -1)
                            {
                                await Bot.EditMessageReplyMarkupAsync(chatId, CallbackQuery.Message.MessageId, await GetMarkupAsync(chatId, videoId, subscribed, "d"));
                            }
                        }
                        else
                        {
                            if (chatId != -1)
                            {
                                await Bot.SendTextMessageAsync(chatId, "Невозможно поставить оценку видео, вы должны войти в ваш аккаунт YouTube. Выполните команду /login");
                            }
                        }
                        break;

                    case "d-A":     //download audio
                        if (chatId == -1)
                        {
                            var chats = user.ChatsAndLastNotifications.Keys;
                            if (chats.Count > 0)
                            {
                                chatId = long.Parse(chats.ElementAt(0));
                                if (chats.Contains(user.Id))
                                {
                                    chatId = long.Parse(user.Id);
                                }
                            }
                        }
                        if (chatId != -1)
                        {
                            await Bot.SendTextMessageAsync(chatId, "Началась загрузка аудиофайла. Пожалуйста, подождите - это может занять некоторое время");

                            Thread thread = new Thread(new ParameterizedThreadStart(Audio));
                            thread.Start(new Data(chatId, user, videoId));
                        }
                        break;

                    case "s-N":     //switch Notifications
                        if (user?.Credential != null)
                        {
                            if (user.ChatsAndSubscribedChannels.GetValueOrDefault(chatId.ToString()).Contains(channelId))
                            {
                                await PubSubHubbub.PuSH.RemoveChannelAsync(chatId, user, channelId);

                                await Bot.SendTextMessageAsync(chatId,
                                                               $"Теперь с канала [{author.Title}](https://www.youtube.com/channel/{channelId}) не будут приходить уведомления о новых видео",
                                                               ParseMode.Markdown,
                                                               null);

                                await Bot.EditMessageReplyMarkupAsync(chatId, CallbackQuery.Message.MessageId, await GetMarkupAsync(chatId, videoId, false, rate));
                            }
                            else
                            {
                                await PubSubHubbub.PuSH.AddChannelAsync(chatId, user, channelId);

                                await Bot.SendTextMessageAsync(chatId,
                                                               $"Теперь с канала [{author.Title}](https://www.youtube.com/channel/{channelId}) будут приходить уведомления о новых видео",
                                                               ParseMode.Markdown,
                                                               null);

                                await Bot.EditMessageReplyMarkupAsync(chatId, CallbackQuery.Message.MessageId, await GetMarkupAsync(chatId, videoId, true, rate));
                            }
                        }
                        break;

                    case "l-o":     //logout
                        if (user?.Credential != null)
                        {
                            user.Credential       = null;
                            user.YoutubeChannelId = null;
                            user.YoutubeUsername  = null;

                            await Bot.SendTextMessageAsync(chatId, $"*Вы вышли из аккаунта YouTube*" + "\n\r" +
                                                           $"Больше вы не будете получать уведомления о новых видео. " + "\n\r" +
                                                           $"Чтобы включить уведомления отправьте команду /start и следуйте инструкциям", ParseMode.Markdown, null);

                            await PubSubHubbub.PuSH.RemoveAllChannelsAsync(chatId, user);
                        }
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                await Bot.SendDebugMessageAsync(e.Message);

                await Bot.SendDebugMessageAsync(e.StackTrace);
            }
        }