Esempio n. 1
0
 public YoutubePageControlleur(YoutubePage a_Vue)
 {
     m_MainWindowControlleur = MainWindowControlleur.Instance;
     m_Vue = a_Vue;
     CreateAlltabs(m_MainWindowControlleur.YoutubeChannels);
 }
        private void addYoutubeSubscriber(SocialPageFormViewModel model)
        {
            var    url          = Regex.Match(model.Url, @"http(s):\/\/(www\.)youtube\.com\/(user|channel)\/[^\/\s]+").ToString();
            string usernameOrId = Regex.Match(url, @"(?<=channel\/|user\/)[^\/\s]+").ToString();

            if (String.IsNullOrWhiteSpace(url) || String.IsNullOrWhiteSpace(usernameOrId))
            {
                throw new BadLinkException("This youtube link isn't good. Try to add something like this https://youtube.com/channel/youtube");
            }

            YoutubePage newYoutubePage = new YoutubePage()
            {
                Cpc  = model.Cpc,
                Name = model.Name,
                Url  = url
            };


            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                ApiKey = "AIzaSyDp5rhMI0dW7uHIYhq0_iLS9vW5pgy1oy0",
            });

            var request = youtubeService.Channels.List("statistics");

            if (model.Url.Contains("channel"))
            {
                request.Id = usernameOrId;
            }
            else
            {
                request.ForUsername = usernameOrId;
            }

            var response = request.Execute();

            var userRetrieveFromYt = response.Items.SingleOrDefault();

            if (userRetrieveFromYt == null)
            {
                throw new BadLinkException("This user doesn't exist");
            }
            else
            {
                if (userRetrieveFromYt.Statistics.HiddenSubscriberCount == true)
                {
                    throw new BadLinkException("You must not hide the subscribers counter");
                }

                var currentUserId = User.Identity.GetUserId();
                newYoutubePage.UserId          = currentUserId;
                newYoutubePage.YoutubeUsername = userRetrieveFromYt.Id;
                var ytAlreadyAdded = _context.Youtubes.SingleOrDefault(y => y.YoutubeUsername == newYoutubePage.YoutubeUsername);


                if (ytAlreadyAdded == null)
                {
                    _context.Youtubes.Add(newYoutubePage);
                }
                else
                {
                    if (ytAlreadyAdded.UserId.Equals(currentUserId))
                    {
                        ytAlreadyAdded.Deleted = false;
                    }
                    else
                    {
                        throw new LinkAlreadyExistException("This channel is already added on this site");
                    }
                }
            }
        }