0

I am trying to make a web browser in CefSharp. I am also trying to make it have a web blocker for YouTube content, mostly for learning purposes on how the YouTube API works. Since there is so little documentation on the API for C#, I am confused on how I am supposed to do this. My code is located at GitHub. My code for the API is in Video.cs and Form1.cs. Here is the code for Video.cs:

using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Tools
{
    public class YouTubeServiceClient
    {
        private static YouTubeServiceClient instance;
        private Video video;

        public static YouTubeServiceClient Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new YouTubeServiceClient();
                }
                return instance;
            }
        }

        public Video Video { get => video; set => video = value; }

        public async Task Run(string videoId)
        {
            UserCredential credential;
            using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
            {
                credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    // This OAuth 2.0 access scope allows for read-only access to the authenticated 
                    // user's account, but not other types of account access.
                    new[] { YouTubeService.Scope.YoutubeReadonly },
                    "user",
                    CancellationToken.None,
                    new FileDataStore(this.GetType().ToString())
                );
            }

            var youtubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = this.GetType().ToString()
            });

            var request = youtubeService.Videos.List("snippet");
            request.Id = videoId;

            // Retrieve the contentDetails part of the channel resource for the authenticated user's channel.
            var response = await request.ExecuteAsync();
            if (response.Items.Count == 1)
            {
                Video = response.Items[0];
            }
        }
    }
}

I used it and got a response sending me to Chrome to enter in my credentials. After this I used my Form1.cs, here is the snippet that deals with it:

private async void OnBrowserAddressChanged(object sender, AddressChangedEventArgs e)
        {
            this.InvokeOnUiThreadIfRequired(() => searchBar.Text = e.Address);
            if (youtubeBlocker && Regex.IsMatch(e.Address, StringTools.WildCardToRegular("https://www.youtube.com/watch*")))
            {
                Uri addressUrl = new Uri(e.Address);
                string videoId = HttpUtility.ParseQueryString(addressUrl.Query).Get("v");

                MessageBox.Show(videoId);
                YouTubeServiceClient videoClient = new YouTubeServiceClient();

                try
                {
                    await videoClient.Run(videoId);
                }
                catch
                {
                    MessageBox.Show("FAILED");
                }

                foreach (string word in blacklistedWords)
                {
                    if (Regex.IsMatch(videoClient.Video.Snippet.Description.ToLower(), StringTools.WildCardToRegular(word)))
                    {
                        MessageBox.Show("BLACKLISTED");
                        break;
                    }
                }

            }
        }

Although I go to the right YouTube pages, my BLACKLISTED message does not pop up. I am really confused to why this might be, and been working on it for 2 days. Is there something wrong with my code? Do I need to use something else? Is there a better way? Thank you.

EDIT: I keep on getting these errors:

[0227/083150.786:INFO:CONSOLE(0)] "The resource https://r8---sn-a8au-hp5l.googlevideo.com/generate_204 was preloaded using link preload but not used within a few seconds from the window's load event. Please make sure it wasn't preloaded for nothing.", source: https://www.youtube.com/watch?v=Ebp8T1HfiLk (0)

[0227/083150.789:INFO:CONSOLE(0)] "The resource https://r8---sn-a8au-hp5l.googlevideo.com/generate_204?conn2 was preloaded using link preload but not used within a few seconds from the window's load event. Please make sure it wasn't preloaded for nothing.", source: https://www.youtube.com/watch?v=Ebp8T1HfiLk (0)
  • You need to call the message boxes on the UI thread, which you are currently only setting searchbar.Text – amaitland Feb 27 '18 at 04:47
  • If you are new to CefSharp then reading https://github.com/cefsharp/CefSharp/wiki/General-Usage#request-handling is recommended. There is now a default IRequestHandler implementation see http://cefsharp.github.io/api/63.0.0/html/T_CefSharp_Handler_DefaultRequestHandler.htm – amaitland Feb 27 '18 at 05:45
  • I was able to view the video id from the message box, but I will try to use the UI thread. Thank you. – J. Henriksson Feb 27 '18 at 12:22

1 Answers1

0

Turns out I was looking in the wrong place. The code for processing the description values was wrong. If you want to see how I instead used YouTube Explode, and how I fixed it, just look at the GitHub page above. Sorry if this was a stupid question, that was me just not understanding how to use the contains method. Sorry, thanks.