Beispiel #1
0
        public static void FetchViewerPercentageMetrics(YouTubeAnalyticsService analyticsService, string channelId, YTD.Video video, Logger logger)
        {
            using (var dbContext = new DataLakeYouTubeAnalyticsContext()) {
                var now = DateTime.UtcNow;

                var lastFetchedDate = dbContext.ViewerPercentageLastDates.SingleOrDefault(x => x.VideoId == video.VideoId);

                var initialDate = video.PublishedAt;

                if (lastFetchedDate == null)
                {
                    // maybe the video simply doesn't support this report, check for early termination
                    if (!RunViewerPercentageReport(analyticsService, channelId, video.PublishedAt, now, video).Any())
                    {
                        logger.Debug("Report not available for video {VideoId}", video.VideoId);
                        return;
                    }
                    lastFetchedDate = new YTA.ViewerPercentageLastDate()
                    {
                        VideoId = video.VideoId, Date = initialDate
                    };
                    dbContext.Add(lastFetchedDate);
                }
                else
                {
                    initialDate = DateHelper.Max(video.PublishedAt, DateHelper.Min(lastFetchedDate.Date, now - TimeMargin));
                }

                int replicatedDays = 0;
                foreach (var date in DateHelper.DaysInRange(initialDate.Date, now.Date))
                {
                    var viewerPercentages = RunViewerPercentageReport(analyticsService, channelId, video.PublishedAt.Date, date, video);
                    lastFetchedDate.Date = date;
                    if (!viewerPercentages.Any())
                    {
                        continue;
                    }

                    DbWriter.Write(
                        video.VideoId,
                        date,
                        viewerPercentages.Select(x => Api2DbObjectConverter.ConvertViewerPercentageRow(x)),
                        now
                        );

                    dbContext.SaveChanges();
                    replicatedDays++;
                    if (replicatedDays % MaxDaysToReplicateInIteration == 0)
                    {
                        break;
                    }
                }
                logger.Debug("Replicated {Days} days for video {VideoId}", replicatedDays, video.VideoId);
            }
        }
Beispiel #2
0
 public static IEnumerable <YTA.VideoDailyMetric> FetchDailyMetrics(YouTubeAnalyticsService analyticsService, string channelId, YTD.Video video, Logger logger, bool reprocess = false)
 {
     using (var dbContext = new DataLakeYouTubeAnalyticsContext()) {
         var now = DateTime.UtcNow;
         var mostRecentRecord = dbContext.VideoDailyMetrics
                                .Where(x => x.VideoId == video.VideoId && x.ValidityStart <= now && now < x.ValidityEnd)
                                .OrderByDescending(x => x.Date)
                                .FirstOrDefault();
         return(FetchVideoDailyMetrics(mostRecentRecord, channelId, video, now, analyticsService, logger, reprocess)
                .Select(x => Api2DbObjectConverter.ConvertDailyMetricRow(video.VideoId, x)));
     }
 }
Beispiel #3
0
        private static IList <IList <object> > RunViewerPercentageReport(YouTubeAnalyticsService analyticsService, string channelId, DateTime fromDate, DateTime toDate, YTD.Video video)
        {
            var reportRequest = analyticsService.Reports.Query();

            reportRequest.Ids        = $"channel=={channelId}";
            reportRequest.StartDate  = fromDate.ToString("yyyy-MM-dd");
            reportRequest.EndDate    = toDate.ToString("yyyy-MM-dd");
            reportRequest.Metrics    = "viewerPercentage";
            reportRequest.Filters    = String.Format("video=={0}", video.VideoId);
            reportRequest.Dimensions = "gender,ageGroup";
            reportRequest.Sort       = "gender,ageGroup";

            return(reportRequest.ExecuteAsync().Result.Rows);
        }
Beispiel #4
0
        private static IEnumerable <IList <object> > FetchVideoDailyMetrics(YTA.VideoDailyMetric mostRecentRecord, string channelId, YTD.Video video, DateTime now, YouTubeAnalyticsService analyticsService, Logger logger, bool reprocessMetrics = false)
        {
            DateTime mostRecentMetricDate;

            if (mostRecentRecord == null || reprocessMetrics)
            {
                TimeSpan PublishedAtOffset;
                if (reprocessMetrics)
                {
                    PublishedAtOffset = new TimeSpan(30, 0, 0, 0);
                }
                else
                {
                    PublishedAtOffset = TimeMargin;
                }
                mostRecentMetricDate = (video.PublishedAt != null) ? video.PublishedAt - PublishedAtOffset : now;
            }
            else
            {
                mostRecentMetricDate = mostRecentRecord.Date;
            }

            var fromDate = DateHelper.Min(now - TimeMargin, mostRecentMetricDate);
            var toDate   = now;

            var reportRequest = analyticsService.Reports.Query();

            reportRequest.Ids        = $"channel=={channelId}";
            reportRequest.StartDate  = fromDate.ToString("yyyy-MM-dd");
            reportRequest.EndDate    = toDate.ToString("yyyy-MM-dd");
            reportRequest.Metrics    = "views,likes,shares,comments,averageViewDuration,dislikes,subscribersGained,subscribersLost,videosAddedToPlaylists,videosRemovedFromPlaylists";
            reportRequest.Filters    = $"video=={video.VideoId}";
            reportRequest.Dimensions = "day";
            reportRequest.Sort       = "day";

            var report = reportRequest.ExecuteAsync().Result;

            if (report.Rows != null)
            {
                logger.Debug("Found {Rows} rows", report.Rows.Count);
                foreach (var row in report.Rows)
                {
                    yield return(row);
                }
            }
        }
Beispiel #5
0
        private void btnSubmitQuery_Click(object sender, EventArgs e)
        {
            if (dtpStartDate.Value > dtpEndDate.Value)
            {
                MessageBox.Show("Start Date should be less than End Date !!");
            }
            else if (txtClientId.Text.Length > 0 && txtClientSecret.Text.Length > 0 && txtRefreshToken.Text.Length > 0 && txtResourceId.Text.Length > 0)
            {
                this.pictureBox.Image = Properties.Resources.Animation;
                this.backgroundWorker.RunWorkerAsync();

                // Here passing ClientId, Client Secret
                var client = new WebServerClient(GoogleAuthenticationServer.Description, txtClientId.Text, txtClientSecret.Text);

                var auth = new OAuth2Authenticator <WebServerClient>(client, Authenticatestate);

                var _analyticService = new YouTubeAnalyticsService(new BaseClientService.Initializer
                {
                    Authenticator = auth
                });

                // create comma seperated list of Metrics
                string requestedMetrics = GetSelectedMterics();

                // Here requesting YouTube data by passing ChannelId, StartDate, EndDate and Metrics
                Google.Apis.YouTubeAnalytics.v1.Data.ResultTable result = _analyticService.Reports.Query(cmbResourceType.Text + "==" + txtResourceId.Text, dtpStartDate.Text, dtpEndDate.Text, requestedMetrics).Execute();

                DataTable youTubeResponseData = new DataTable();

                // Process the YouTube API response
                if (result != null && result.Rows != null && result.Rows.Count > 0)
                {
                    foreach (Google.Apis.YouTubeAnalytics.v1.Data.ResultTable.ColumnHeadersData header in result.ColumnHeaders)
                    {
                        youTubeResponseData.Columns.Add(header.Name, typeof(string));
                    }

                    int     rowNumber  = 0;
                    DataRow youTubeRow = youTubeResponseData.NewRow();
                    foreach (var metricValue in result.Rows[0])
                    {
                        youTubeRow[rowNumber] = metricValue;
                        rowNumber++;
                    }
                    youTubeResponseData.Rows.Add(youTubeRow);
                }
                else
                {
                    MessageBox.Show("No Data Available !!");
                }

                if (youTubeResponseData.Rows.Count > 0)
                {
                    dgYouTubeResponse.DataSource = youTubeResponseData;
                }

                this.pictureBox.Image = null;
            }
            else
            {
                MessageBox.Show("All fields are compulsory. Please provide it !!");
            }

            this.backgroundWorker.CancelAsync();
        }
        public async void FetchYoutubeAnalyticsAPI()
        {
            UserCredential credential;

            using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
            {
                credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    new[] { YouTubeService.Scope.YoutubeReadonly },
                    "user",
                    CancellationToken.None
                    );
            }

            var youTubeAnalyticsService = new YouTubeAnalyticsService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = Assembly.GetExecutingAssembly().GetName().Name
            });

            var request = youTubeAnalyticsService.Reports.Query();

            request.StartDate = ("2019-01-01");
            request.EndDate   = ("2020-09-30");
            request.Ids       = ("channel==UCmLQ3sdAd6CypJIne5ZANaA");
            request.Metrics   = ("views,comments,likes,dislikes,estimatedMinutesWatched,averageViewDuration");

            QueryResponse requestquery = request.Execute();

            List <int> myChannelDataList = new List <int>();

            int listsize = 0;

            foreach (object obj in requestquery.Rows[0])
            {
                int value = Convert.ToInt32(obj);
                myChannelDataList.Add(value);

                /*      Debug.WriteLine("Value : " + value);
                 *    Debug.WriteLine("LISTE / " + myChannelDataList[0]);
                 *    Debug.WriteLine("Count : " + myChannelDataList.Count);*/
                listsize = myChannelDataList.Count;
            }
            List <string> metrics = new List <string> {
                "Vues", "Commentaires", "Likes", "Dislike", "Minutes", "MoyenneTemps"
            };

            for (int i = 0; i < listsize; i++)
            {
                Label labels = new Label();
                labels.Top       = (i + 4) * 20;
                labels.Left      = 100;
                labels.AutoSize  = true;
                labels.TextAlign = ContentAlignment.MiddleLeft;
                labels.Text      = metrics[i] + " :" + myChannelDataList[i].ToString();
                this.Controls.Add(labels);
            }


            // https://developers.google.com/youtube/v3/docs/channels/list
            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = Assembly.GetExecutingAssembly().GetName().Name
            });
            var requestSub = youtubeService.Channels.List("statistics");

            requestSub.Mine = (true);

            ChannelListResponse responsesub = requestSub.Execute();

            foreach (var sresults in responsesub.Items)
            {
                var substats      = sresults.Statistics;
                var subcount      = substats.SubscriberCount;
                var viewCount     = substats.ViewCount;
                var videoCount    = substats.VideoCount;
                var commentsCount = substats.CommentCount;

                Debug.WriteLine("SubCount : " + subcount);

                Label labels = new Label();
                labels.Top       = 16;
                labels.Left      = 280;
                labels.AutoSize  = true;
                labels.TextAlign = ContentAlignment.MiddleLeft;
                labels.Text      = "Subs :" + subcount;
                this.Controls.Add(labels);
            }
        }
Beispiel #7
0
 public ReprocessDailyVideoMetricsQuery(YouTubeService dataService, YouTubeAnalyticsService analyticsService)
 {
     DataService      = dataService;
     AnalyticsService = analyticsService;
 }
Beispiel #8
0
 public ViewerPercentageMetricsQuery(YouTubeService dataService, YouTubeAnalyticsService analyticsService)
 {
     DataService      = dataService;
     AnalyticsService = analyticsService;
 }