// Get details about the video from the window title private static YoutubeVideo ParseVideo(string windowTitle) { YoutubeVideo video = new YoutubeVideo(); string[] terms = windowTitle.Split(new string[] { " - " }, StringSplitOptions.None); // If there is no video title, assume user is on the YouTube homepage if (terms.Length <= 2) { video.Idle = true; return(video); } // Get the video title Array.Resize(ref terms, terms.Length - 2); string title = string.Join(" - ", terms); video.Title = title; return(video); }
// Scan the processes for anything YouTube-related private static void ScanProcesses() { Console.WriteLine("Scanning for YouTube..."); Process[] processList = Process.GetProcesses(); Process finalProcess = null; // Check all the window titles for "YouTube" foreach (Process process in processList) { if (process.MainWindowTitle.ToLower().Contains("youtube") && IsBrowser(process)) { Console.WriteLine("Process: {0} ID: {1} Window title: {2}", process.ProcessName, process.Id, process.MainWindowTitle); // Stop at the first match - no more than one YouTube window is scanned finalProcess = process; break; } } // Make sure YouTube is open if (finalProcess != null) { YoutubeVideo video = ParseVideo(finalProcess.MainWindowTitle); // Check if video details have changed if (currentVideo == null || !(currentVideo.Title == video.Title && currentVideo.Idle == video.Idle)) { // Only if the video is different, update the presence currentVideo = video; DiscordClient.SetVideo(video); } } else { // Clear presence if nothing is playing currentVideo = null; DiscordClient.ClearVideo(); } }
// Set a new presence based on playing video (or lack thereof) public static void SetVideo(YoutubeVideo video) { if (video.Idle) { // Update idle timestamp idlePresence.Timestamps = Timestamps.Now; client.SetPresence(idlePresence); } else { client.SetPresence(new RichPresence { Details = video.Title, State = "Watching", Timestamps = Timestamps.Now, Assets = new Assets { LargeImageKey = "squircle_red", LargeImageText = "Watching a Video" } }); } }
public static void Stop() { timer.Stop(); currentVideo = null; }