public NameValueCollection OutputDataValues() { GlobalFunctions.GetYouTubeVideoID(Link, out string ID); return(new NameValueCollection { { nameof(Link), Link }, { nameof(ShortLink), ShortLink }, { nameof(ID), ID }, { nameof(OriginalRequester), OriginalRequester }, { nameof(Title), Title }, { nameof(LengthSec), LengthSec.ToString() }, { nameof(LengthInTime), LengthInTime.ToString() }, { nameof(DirLocation), DirLocation }, { nameof(LastValidPing), LastValidPing.ToString() }, { nameof(LastPingFailed), LastPingFailed.ToString() }, { nameof(PingValid), PingValid.ToString() }, { nameof(LocalFile), LocalFile.ToString() }, { nameof(AudioCached), AudioCached().ToString() }, { nameof(UniqueSystemID), UniqueSystemID } }); }
public async Task GetYouTubeAudioData(YoutubeDL YoutubeDLWorker, YoutubeDLSharp.Options.AudioConversionFormat type = YoutubeDLSharp.Options.AudioConversionFormat.Mp3, bool Force = false) { if (YoutubeDLWorker == null) { throw new NullReferenceException("GetYouTubeAudioData: YoutubeDLWorker was provided null"); } if (VideoAquireTask != null) { await VideoAquireTask; return; } if (LocalFile) { MainForm.StaticPostToDebug($"GetYouTubeVideoInformation: Skipped {(string.IsNullOrEmpty(Title) ? Link : Title)} download, Song is Local."); return; } if (string.IsNullOrEmpty(Title)) { if (InformationAquireTask != null) { await InformationAquireTask; } else { await GetYouTubeVideoInformation(YoutubeDLWorker); } } if (!Force && AudioCached()) { MainForm.StaticPostToDebug($"GetYouTubeAudioData: {(string.IsNullOrEmpty(Title) ? Link : Title)} Audio found, download canceled."); return; } else if (Force) { MainForm.StaticPostToDebug($"GetYouTubeVideoInformation: Forced download of {(string.IsNullOrEmpty(Title) ? Link : Title)} Audio"); } DownloadWorking = true; if (GlobalFunctions.GetYouTubeVideoID(Link, out string ID)) { if (AudioCached()) { File.Delete(FullDirLocation); } RunResult <string> Youtubedata = null; Exception exception = null; try { VideoAquireTask = YoutubeDLWorker.RunAudioDownload("https://www.youtube.com/watch?v=" + ID, type); Youtubedata = await VideoAquireTask; } catch (Exception e) { exception = e; } if (exception != null) { MainForm.StaticPostToDebug($"{Title} : Error attempting to download song data. : {ErrorMessage = exception.Message}"); LastPingFailed = true; } else if (Youtubedata != null && Youtubedata.Success) { string extension = $".{type.ToString().ToLower()}"; string filenameTitle = Title; char[] invalidchars = Path.GetInvalidFileNameChars(); foreach (char invalid in invalidchars) { filenameTitle = filenameTitle.Replace(invalid, 'X'); } string newFilename = $"{Path.GetDirectoryName(Youtubedata.Data)}\\{ID} - {filenameTitle}{extension}"; try { bool check; do // Youtube-dl might still be processing the file so wait until it is created then rename { check = true; IEnumerable <string> directories = Directory.GetFiles($"{Path.GetDirectoryName(Youtubedata.Data)}\\"); foreach (string dir in directories) { if (dir.Contains(ID) && dir.Contains(extension)) { File.Move(dir, newFilename); check = false; break; } } await Task.Delay(1000); } while (check); } catch (Exception e) { MainForm.StaticPostToDebug($"Rename function failed on {Title} : {e.Message}"); } string successMessage = $"Audio Download of {Link} Successfull: {newFilename}"; MainForm.StaticPostToDebug(successMessage); //Find filename by ID then rename that entry to new one, need to change special characters before that too DirLocation = newFilename.Replace(Directory.GetCurrentDirectory(), ""); LastValidPing = DateTime.Now; LastPingFailed = false; } else { if (Youtubedata == null) { MainForm.StaticPostToDebug($"GetYouTubeAudioData using link: {Link} Failed, Downloader encountered errors."); } else { string errors = ""; foreach (string error in Youtubedata.ErrorOutput) { errors += error + " :: "; } MainForm.StaticPostToDebug(errors); } LastPingFailed = true; } } else { ErrorMessage = $"{Link} Failed, link was not recognised by Regex."; MainForm.StaticPostToDebug($"GetYouTubeAudioData using link: {Link} Failed, link was not recognised by Regex."); LastPingFailed = true; } VideoAquireTask = null; DownloadWorking = false; }
public async Task GetYouTubeVideoInformation(YoutubeDL YoutubeDLWorker, bool Force = false) { if (YoutubeDLWorker == null) { throw new NullReferenceException("GetYouTubeVideoInformation: YoutubeDLWorker was provided null"); } if (InformationAquireTask != null) { await InformationAquireTask; return; } else if (LocalFile) { MainForm.StaticPostToDebug($"GetYouTubeVideoInformation: Skipped {(string.IsNullOrEmpty(Title) ? Link : Title)} data, Song is Local."); return; } else if (!Force && PingValid) { MainForm.StaticPostToDebug($"GetYouTubeVideoInformation: {(string.IsNullOrEmpty(Title) ? Link : Title)} is still within valid period, download canceled."); return; } else if (Force && PingValid) { MainForm.StaticPostToDebug($"GetYouTubeVideoInformation: Forced download of {(string.IsNullOrEmpty(Title) ? Link : Title)} data."); } DownloadWorking = true; if (GlobalFunctions.GetYouTubeVideoID(Link, out string youtubeMatch)) { RunResult <VideoData> Youtubedata = null; Exception exception = null; try { MainForm.StaticPostToDebug($"GetYouTubeVideoInformation: Download of {(string.IsNullOrEmpty(Title) ? Link : Title)} started."); InformationAquireTask = YoutubeDLWorker.RunVideoDataFetch("https://www.youtube.com/watch?v=" + youtubeMatch , overrideOptions: new YoutubeDLSharp.Options.OptionSet() { DumpJson = true, DumpSingleJson = true, HlsPreferNative = true, IgnoreConfig = true, NoPlaylist = true, SkipDownload = true, GetThumbnail = false, ListThumbnails = false, WriteAllThumbnails = false, WriteThumbnail = false }); Youtubedata = await InformationAquireTask; } catch (Exception e) { exception = e; } if (exception != null) { MainForm.StaticPostToDebug($"{Link} : Error attempting to download song information. : {ErrorMessage = exception.Message}"); LastPingFailed = true; } else if (Youtubedata != null && Youtubedata.Success) { Title = Youtubedata.Data.Title; LengthSec = (int)Youtubedata.Data.Duration; if (LengthSec > 900) { LastPingFailed = true; ErrorMessage = "Video Length exceeds set limit of 15 Mins."; MainForm.StaticPostToDebug($"Video Length exceeds set limit of 15 Mins.... {Title}"); } else { LastValidPing = DateTime.Now; LastPingFailed = false; MainForm.StaticPostToDebug($"Secondary Song Info Downloaded... {Title}"); } } else { if (Youtubedata == null) { MainForm.StaticPostToDebug($"https://www.youtube.com/watch?v={youtubeMatch} : YoutubeDLWorker Crashed Out"); } else { string errors = ""; foreach (string error in Youtubedata.ErrorOutput) { errors += error + " :: "; } //This video is not available if (errors.Contains("This video is not available")) { ErrorMessage = "Video not available, it may not be available at streamers location."; } MainForm.StaticPostToDebug(errors); } LastPingFailed = true; } } else { ErrorMessage = $"{Link} Failed, link was not recognised by Regex."; MainForm.StaticPostToDebug($"GetYouTubeVideoInformation using link: {Link} Failed, link was not recognised by Regex."); LastPingFailed = true; } DownloadWorking = false; InformationAquireTask = null; return; }