Esempio n. 1
0
        public static async Task <double> Average(this List <ViewStatistic> viewStatistic)
        {
            if (Token == null || Credentials == null)
            {
                throw new Exception("Token or credentials not found!");
            }

            double average = 0;

            if (viewStatistic != null && viewStatistic.Count > 0)
            {
                var googleService = new GoogleService(Credentials);
                googleService.SetToken(Token);

                var channel = await googleService.GetChannels();

                if (channel != null && channel.Items.Count > 0)
                {
                    var startDate = DateTime.ParseExact(viewStatistic.FirstOrDefault().Date, "yyyy-MM-dd", CultureInfo.InvariantCulture);
                    var endDate   = DateTime.ParseExact(viewStatistic.LastOrDefault().Date, "yyyy-MM-dd", CultureInfo.InvariantCulture);

                    var channelVideos = await googleService.GetChannelVideos(channel.Items.FirstOrDefault().Id, startDate, endDate);

                    if (channelVideos != null && channelVideos.PageInfo != null)
                    {
                        var totalVideos = channelVideos.PageInfo.TotalResults;
                        var totalViews  = viewStatistic.Sum(t => t.Value);

                        average = totalViews / totalVideos;
                    }
                }
            }

            return(average);
        }