I decided to write a program that downloads thumbnails from YouTube. But I ran into the problem that sometimes WebClient throws a 404 error.

I wanted to make it so that if the selected quality gives an error of 404, we take the link to the lower quality of the picture.

But for some reason it does not work.

 public class Downloader { WebClient client = new WebClient(); public void Download(string ide, string quality, string filename, string path) { if (path == @"C:\Downloads") { Directory.CreateDirectory(@"C:\Downloads"); } string uri; if (quality == "maxresdefault")// Π’ ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ΅ Π²Ρ‹Π±Ρ€Π°Π½ΠΎ максимальноС качСство { string[] maxDef = {//ΠΎΡ‚ максимального (ide - ΠΈΠ΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€ Π²ΠΈΠ΄Π΅ΠΎ) "https://img.youtube.com/vi/" + ide + "/maxresdefault.jpg", "https://img.youtube.com/vi/" + ide + "/sddefault.jpg", "https://img.youtube.com/vi/" + ide + "/hqdefault.jpg", "https://img.youtube.com/vi/" + ide + "/mqdefault.jpg", "https://img.youtube.com/vi/" + ide + "/default.jpg" //Π΄ΠΎ минимального }; foreach (var i in maxDef)//Π·Π°ΠΏΠΈΡ…ΠΈΠ²Π°ΡŽ массив Π² Ρ†ΠΈΠΊΠ» { uri = i;//ΠΏΡ€ΠΈΡΠ²Π°ΠΈΠ²Π°ΡŽ максимальноС, ΠΈ ΠΏΠΎ ΠΏΡ€ΠΎΡ…ΠΎΠΆΠ΄Π΅Π½ΠΈΡŽ Π΄ΠΎ минимального //Ссли ΠΏΡ€ΠΈΡΠ²ΠΎΠΈΡ‚ΡŒ ссылку которая Π΄Π΅ΠΉΡΡ‚Π²ΠΈΡ‚Π΅Π»ΡŒΠ½Π°Ρ сразу - ΠΊΠ°Ρ‡Π°Π΅Ρ‚, ΠΈΠ· массива Π½Π΅Ρ‚ filename = path + "\\" + filename + ".jpg";//задаю ΠΏΡƒΡ‚ΡŒ ΠΈ имя Ρ„Π°ΠΉΠ»Π° try { client.DownloadFile(uri, filename);//ΠΏΡ‹Ρ‚Π°ΡŽΡΡŒ ΡΠΊΠ°Ρ‡Π°Ρ‚ΡŒ break;//Ссли скачало Π²Ρ‹Ρ…ΠΎΠΆΡƒ ΠΈΠ· Ρ†ΠΈΠΊΠ»Π° } catch { } } 
  • Maybe Google server you are football for too frequent requests? - Sergey
  • Vryatli, if I upload a file with links, and choose the quality that all pictures have, downloads both 200 and 500. - Soback
  • ... ΠΈΠ½ΠΎΠ³Π΄Π° WebClient Π²Ρ‹ΠΊΠΈΠ΄Ρ‹Π²Π°Π΅Ρ‚ ΠΎΡˆΠΈΠ±ΠΊΡƒ 404. Just in case I ask, is the error reproduced? Those. arises randomly or stably on a specific ide ? - default locale
  • It occurs if the link does not exist, checked in the browser. - Soback
  • Those. There is a video, and the default thumbnails ( http://img.youtube.com/vi/<ide>/default.jpg ) aren't there? - default locale

1 answer 1

Transfer filename = path + "\\" + filename + ".jpg"; before the foreach (var i in maxDef) and it will work. Your path is incremented.

  • Everything works, thank you! - Soback