// Calculate most viewed channel public async Task <List <TimePerChannelViewModel> > GetMostTimeChannel() { var channelIds = new List <string>(); var timeWatched = new List <TimeSpan>(); foreach (var video in HistoryVideos) { channelIds.Add(VideoViewModelsDict[video.GetVideoID()].ChannelId); } channelIds = channelIds.Distinct().ToList(); foreach (var channelId in channelIds) { TimeSpan time = new TimeSpan(0, 0, 0); foreach (HistoryVideo historyVideo in HistoryVideos) { if (VideoViewModelsDict[historyVideo.GetVideoID()].ChannelId == channelId) { time = time.Add(VideoViewModelsDict[historyVideo.GetVideoID()].GetDuration()); } } timeWatched.Add(time); time = new TimeSpan(0, 0, 0); } var channelIdsArr = channelIds.ToArray(); var timeWatchedArr = timeWatched.ToArray(); Array.Sort(timeWatchedArr, channelIdsArr); Array.Reverse(timeWatchedArr); Array.Reverse(channelIdsArr); channelIds = channelIdsArr.Take(10).ToList(); timeWatched = timeWatchedArr.Take(10).ToList(); List <TimePerChannelViewModel> timePerChannelViewModels = new List <TimePerChannelViewModel>(); for (int i = 0; i < channelIds.Count; i++) { timePerChannelViewModels.Add(new TimePerChannelViewModel() { Channel = await YouTubeApiHelper.GetInstance().GetChannel(channelIds[i]), TimeWatched = timeWatched[i] }); } timePerChannelViewModels = timePerChannelViewModels.OrderByDescending(vm => vm.TimeWatched).ToList(); //using (StreamWriter sw = new StreamWriter(UriHelper.CALCULATIONS_OUTPUT_DIR + "/Time Watched Per Channel - " + DateTime.Now.ToFileTime() + ".csv")) //{ // foreach (var viewModel in timePerChannelViewModels) // { // sw.WriteLine(viewModel.Channel + ";" + Math.Round(viewModel.TimeWatched.TotalHours) + " Hours (" + Math.Round(viewModel.TimeWatched.TotalHours * 60) + " minutes)"); // } //} return(timePerChannelViewModels); }
public async Task Initialise(string jobId, string takeoutDataJson, string emailAddress) { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); // Update DB Status YouTubeProcessingJobStatus jobStatus = new YouTubeProcessingJobStatus() { JobId = jobId, JobStatus = JobStatus.INITIATED }; FileDataHelper fileDataHelper = new FileDataHelper(); fileDataHelper.SaveJobStatus(jobStatus); System.Diagnostics.Debug.WriteLine("Starting"); List <HistoryVideo> historyVideos = GetHistoryFromJson(takeoutDataJson).ToList(); System.Diagnostics.Debug.WriteLine("Fetching video data"); Dictionary <string, VideoViewModel> videoViewModelsDict = await YouTubeApiHelper.GetInstance().GetVideos(historyVideos.Take(8000).ToList()); System.Diagnostics.Debug.WriteLine("Doing calculations"); jobStatus.JobStatus = JobStatus.PROCESSING; fileDataHelper.SaveJobStatus(jobStatus); YouTubeProcessingJobData processingJobData = await PerformCalculations(jobId, historyVideos, videoViewModelsDict); fileDataHelper.SaveProcessingJob(processingJobData); jobStatus.JobStatus = JobStatus.COMPLETED; fileDataHelper.SaveJobStatus(jobStatus); System.Diagnostics.Debug.WriteLine("Sending email"); //await MailJetHelper.SendEmail(emailAddress, "https://localhost:44369/"); // Update DB Status // Save Job to DB stopWatch.Stop(); // Get the elapsed time as a TimeSpan value. TimeSpan ts = stopWatch.Elapsed; // Format and display the TimeSpan value. string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); System.Diagnostics.Debug.WriteLine("Completed in " + elapsedTime); }
// Calculate most viewed channel public async Task <List <ViewsPerChannelViewModel> > GetMostViewedChannel() { var channelIds = new List <string>(); var numVideosWatched = new List <int>(); foreach (var video in HistoryVideos) { channelIds.Add(VideoViewModelsDict[video.GetVideoID()].ChannelId); } channelIds = channelIds.Distinct().ToList(); foreach (var channelId in channelIds) { int numVids = 0; foreach (HistoryVideo historyVideo in HistoryVideos) { if (VideoViewModelsDict[historyVideo.GetVideoID()].ChannelId == channelId) { numVids++; } } numVideosWatched.Add(numVids); numVids = 0; } var channelIdsArr = channelIds.ToArray(); var numVideosWatchedArr = numVideosWatched.ToArray(); Array.Sort(numVideosWatchedArr, channelIdsArr); Array.Reverse(numVideosWatchedArr); Array.Reverse(channelIdsArr); channelIds = channelIdsArr.Take(10).ToList(); numVideosWatched = numVideosWatchedArr.Take(10).ToList(); List <ViewsPerChannelViewModel> viewsPerChannelViewModels = new List <ViewsPerChannelViewModel>(); for (int i = 0; i < channelIds.Count; i++) { viewsPerChannelViewModels.Add(new ViewsPerChannelViewModel() { Channel = await YouTubeApiHelper.GetInstance().GetChannel(channelIds[i]), NumVideos = numVideosWatched[i] }); } viewsPerChannelViewModels = viewsPerChannelViewModels.OrderByDescending(vm => vm.NumVideos).ToList(); //using (StreamWriter sw = new StreamWriter(UriHelper.CALCULATIONS_OUTPUT_DIR + "/Views Per Channel - " + DateTime.Now.ToFileTime() + ".csv")) //{ // foreach (var viewModel in viewsPerChannelViewModels) // { // sw.WriteLine(viewModel.Channel + ";" + viewModel.NumVideos); // } //} return(viewsPerChannelViewModels); }
public static YouTubeApiHelper GetInstance() { if (_instance == null) { _instance = new YouTubeApiHelper(); } return(_instance); }