Exemplo n.º 1
0
        public async Task <ChannelViewModel> GetChannel(string channelId)
        {
            PopulatePastChannelSearchesDict();


            if (PastChannelSearchesDict.ContainsKey(channelId))
            {
                return(PastChannelSearchesDict[channelId]);
            }
            else
            {
                ApiChannel apiChannel = await GetChannelDataFromApi(channelId);

                if (apiChannel.Items.Length > 0)
                {
                    ChannelViewModel viewModel = ChannelViewModel.FromApiChannel(apiChannel);
                    PastChannelSearchesDict.Add(channelId, viewModel);
                    await WriteChannelViewModelToFile(viewModel);

                    return(viewModel);
                }

                return(null);
            }
        }
Exemplo n.º 2
0
        public async Task <Dictionary <string, ChannelViewModel> > GetChannels(List <string> channelIds)
        {
            PopulatePastChannelSearchesDict();
            Dictionary <string, ChannelViewModel> apiChannelsDict = new Dictionary <string, ChannelViewModel>();

            foreach (var id in channelIds)
            {
                if (!apiChannelsDict.ContainsKey(id))
                {
                    if (PastVideoSearchesDict.ContainsKey(id))
                    {
                        apiChannelsDict.Add(id, PastChannelSearchesDict[id]);
                    }
                    else
                    {
                        ApiChannel apiChannel = await GetChannelDataFromApi(id);

                        if (apiChannel.Items.Length > 0)
                        {
                            ChannelViewModel viewModel = ChannelViewModel.FromApiChannel(apiChannel);
                            await WriteChannelViewModelToFile(viewModel);

                            apiChannelsDict.Add(id, viewModel);
                            PastChannelSearchesDict.Add(id, viewModel);
                        }
                    }
                }
            }

            return(apiChannelsDict);
        }
        public static ChannelViewModel FromApiChannel(ApiChannel apiChannel)
        {
            ChannelViewModel viewModel = new ChannelViewModel()
            {
                Id               = apiChannel.Items[0].Id,
                ChannelTitle     = apiChannel.Items[0].Snippet.Title,
                ChannelUrl       = "https://www.youtube.com/channel/" + apiChannel.Items[0].Id,
                ChannelThumbnail = apiChannel.Items[0].Snippet.Thumbnails.Default.Url == null ? "" : apiChannel.Items[0].Snippet.Thumbnails.Default.Url,
                ChannelBanner    = apiChannel.Items[0].BrandingSettings.Image == null ? "" : apiChannel.Items[0].BrandingSettings.Image.BannerExternalUrl
            };

            return(viewModel);
        }
Exemplo n.º 4
0
        public async Task <ApiChannel> GetChannelDataFromApi(string channelId)
        {
            string googleKey = Environment.GetEnvironmentVariable("$GOOGLE_APIKEY_YOUTUBE_WRAPPED", EnvironmentVariableTarget.Machine);
            Uri    uri       = new Uri(@"https://youtube.googleapis.com/youtube/v3/channels?part=snippet,brandingSettings&id=" + channelId + "&key=" + googleKey);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

            request.AutomaticDecompression = DecompressionMethods.GZip;
            //request.UserAgent = "12345";

            string responseString = string.Empty;

            using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
                using (Stream stream = response.GetResponseStream())
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        responseString = reader.ReadToEnd();
                        ApiChannel apiChannel = JsonConvert.DeserializeObject <ApiChannel>(responseString);
                        return(apiChannel);
                    }
        }