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