public static string YTUrlParser(string ytLink) { // https://youtu.be/Gi-H7C2oHdA if (ytLink.Contains("youtu.be")) { var id = ytLink.Split('/').Last().Trim(); ytLink = "https://www.youtube.com/watch?v=" + id; } //https://www.youtube.com/channel/UCgxTPTFbIbCWfTR9I2-5SeQ //https://www.youtube.com/user/NavalnyRu //https://www.youtube.com/watch?v=cHqtLrpRQ3Y& var yc = new YoutubeClient(); string link = ytLink.Trim().Split("&")[0].TrimEnd('/'); string cid = null; if (link.Contains("watch?v")) { cid = yc.GetVideoAuthorChannelAsync(link.Split('=')[1]).Result.Id; } if (link.Contains(".com/user")) { var text = new WebClient().DownloadString(link); var match = new Regex("(?:data-channel-external-id=\")(.*?)(?:\")").Match(text); cid = match.Groups.Last().Value; } if (link.Contains(".com/channel")) { link = link.Replace("/videos", ""); cid = link.Split("/").Last(); } return(cid); }
public async Task YoutubeClient_GetVideoAuthorChannelAsync_Test(string videoId) { var client = new YoutubeClient(); var channel = await client.GetVideoAuthorChannelAsync(videoId); Assert.That(channel, Is.Not.Null); }
private async Task DownloadMusicFile(HttpClient httpclient, DataRepository _repository, YoutubeClient _ytclient, YouTubeSourceMedia media) { _logger.LogInformation($"Getting metadata for {media.YouTubeId}"); try { var mediainfo = await _ytclient.GetVideoAsync(media.YouTubeId); var channel = await _ytclient.GetVideoAuthorChannelAsync(media.YouTubeId); MediaStreamInfoSet info = null; info = await _ytclient.GetVideoMediaStreamInfosAsync(media.YouTubeId); var audioinfo = info.Audio .OrderBy(x => x.Size) .Where(x => x.AudioEncoding == AudioEncoding.Aac) .FirstOrDefault(); media.FilePath = Path.Combine(media.GetType().Name, channel.Id, $"{media.YouTubeId}.m4a") .GetSafePath(); media.ArtworkPath = Path.Combine(media.GetType().Name, channel.Id, $"{media.YouTubeId}.jpg") .GetSafePath(); var downloadfilepath = Path.Combine(_config.SourcesPath, media.FilePath).GetSafePath(); var downloadartworkpath = Path.Combine(_config.SourcesPath, media.ArtworkPath).GetSafePath(); _logger.LogInformation($"Downloading file {downloadfilepath}"); Directory.CreateDirectory(Path.GetDirectoryName(downloadfilepath)); await _ytclient.DownloadMediaStreamAsync(audioinfo, downloadfilepath); await File.WriteAllBytesAsync(downloadartworkpath, await GetThumbnail(httpclient, mediainfo)); media.IsDownloaded = true; await _repository.UpdateMedia(media); } catch (YoutubeExplode.Exceptions.VideoUnplayableException ex) { _logger.LogError(ex, $"Unable to download {media.YouTubeId}"); media.IsActive = false; await _repository.UpdateMedia(media); return; } _logger.LogInformation($"Download for {media.YouTubeId} completed"); }
public void YoutubeClient_GetVideoAuthorChannelAsync_Unavailable_Test(string videoId) { var client = new YoutubeClient(); Assert.ThrowsAsync <VideoUnavailableException>(() => client.GetVideoAuthorChannelAsync(videoId)); }