5
\$\begingroup\$

I made some C# code to grab youtube thumbnails from urls, I originally made this in python took some time converting it to C#. I am VERY new to C# and did this with minimal help.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    internal class Program
    {
        static void Main(string[] args)
        {

            var HD = "maxresdefault.jpg";
            var SD = "hqdefualt.jpg";
            Console.WriteLine("Welcome to the C# version of YT thumbnail grabber!\nTo get started, go to any video and click 'copy url', then paste it into the program and we'll do the rest!\nTwo websites will open, one with a HD version of the thumbnail, and one with a SD version\nWARNING: A THUMBNAIL MAY ONLY LOAD IN SD OR HD. WHICHEVER IT WAS MADE IN\nInput URL now - ");
            var link = Console.ReadLine();
            var image = link.Split('/')[3];
            var website = "https://img.youtube.com/vi/" + image + "/";

            void openBrowser(string a, string b)
            {
                System.Diagnostics.Process.Start(website + a);
                System.Diagnostics.Process.Start(website + b);
            }

            openBrowser(HD, SD);
            

            Console.Read();
        }
    }
}
\$\endgroup\$
0

1 Answer 1

5
\$\begingroup\$

Prefer constants

  • Rather than defining HD and SD as mutable variable, please prefer constants
    • I think there is a typo in your version "hqdefualt.jpg"
const string HD = "maxresdefault.jpg", SD = "hqdefault.jpg";
  • You should do the same with url prefix
const string HD = "...", SD = "...", UrlPrefix = "https://img.youtube.com/vi";

Prefer verbatim identifier (@)

  • Rather than using \n inside a long string please prefer @ and multi-line string
Console.WriteLine(@"Welcome to the C# version of YT thumbnail grabber!
To get started, go to any video and click 'copy url', then paste it into the program and we'll do the rest!
Two websites will open, one with a HD version of the thumbnail, and one with a SD version
WARNING: A THUMBNAIL MAY ONLY LOAD IN SD OR HD. WHICHEVER IT WAS MADE IN
Input URL now - ");

Do not trust user input

  • Rather than blindly trusting the user that (s)he provided a valid url please perform some preliminary check(s)
var link = Console.ReadLine();
if (!Uri.TryCreate(link, UriKind.Absolute, out Uri ytLink))
{
    Console.WriteLine("Please provide a valid url!");
    Environment.Exit(-1);
}

Prefer parsed data

  • Rather than using magic like this Split("/")[3] please prefer structured data access
  • Since we have parsed the user input as an Uri we can access the same data via the PathAndQuery property
var image = ytLink.PathAndQuery;

Prefer string interpolation

  • Rather than concatenating the final url in several steps use a single string interpolation command
$"{UrlPrefix}{image}/{SD}"
$"{UrlPrefix}{image}/{HD}"

Prefer inlining

  • Rather than creating a local method openBrowser which is called only once please inline the functionality
Process.Start($"{UrlPrefix}{image}/{SD}");
Process.Start($"{UrlPrefix}{image}/{HD}");
  • Please also prefer namespace usings: using System.Diagnostics;

The full source code

const string HD = "maxresdefault.jpg", SD = "hqdefault.jpg", UrlPrefix = "https://img.youtube.com/vi";
static void Main()
{
    Console.WriteLine(@"Welcome to the C# version of YT thumbnail grabber!
To get started, go to any video and click 'copy url', then paste it into the program and we'll do the rest!
Two websites will open, one with a HD version of the thumbnail, and one with a SD version
WARNING: A THUMBNAIL MAY ONLY LOAD IN SD OR HD. WHICHEVER IT WAS MADE IN
Input URL now - ");

    var link = Console.ReadLine();
    if (!Uri.TryCreate(link, UriKind.Absolute, out Uri ytLink))
    {
        Console.WriteLine("Please provide a valid url!");
        Environment.Exit(-1);
    }

    Process.Start($"{UrlPrefix}{ytLink.PathAndQuery}/{SD}");
    Process.Start($"{UrlPrefix}{ytLink.PathAndQuery}/{HD}");

    Console.Readline();
}

Other observations

Launching browser

  • Process.Start with an URL will not work on non-Windows machines
    • I have a Macbook where that command failed with an exception
  • It might make sense to use HttpClient to download the images

Url generation

  • I've tried your generated urls and did not provide any valid thumbnail
  • I have found this SO topic where the described url generation worked like a charm
\$\endgroup\$
2
  • 1
    \$\begingroup\$ I think there is a type should be typo \$\endgroup\$ Commented Sep 29, 2022 at 19:42
  • 1
    \$\begingroup\$ Thank you! As I said I'm very new to c# and your post was very helpful! I'll keep it in mind for my future code. \$\endgroup\$ Commented Sep 30, 2022 at 7:46

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.