/// <summary> /// Gets YouTube/Vimeo video model for given <paramref name="videoUrl"/>. /// </summary> /// <remarks> /// Returns empty <see cref="VideoModel"/> if <paramref name="videoUrl"/> is null. /// </remarks> /// <param name="videoUrl">YouTube or Vimeo video URL.</param> /// <exception cref="NotSupportedException"><paramref name="video"/> doesn't refer to a Youtube or Vimeo video.</exception> /// <exception cref="UriFormatException"><paramref name="videoUrl"/> is malformed URL.</exception> public static VideoModel GetVideoModel(string videoUrl) { if (videoUrl == null) { return(new VideoModel()); } // Throws exception if videoUrl is invalid URL. var videoUri = new UriBuilder(videoUrl).Uri; var match = VideoRegex.Match(videoUrl); if (match.Success) { string videoId = String.Empty; VideoKindEnum videoKind = VideoKindEnum.Unknown; if (!String.IsNullOrEmpty(match.Groups["youtube"].Value)) { // Search for ?v=XXX in URL. var queryDictionary = HttpUtility.ParseQueryString(videoUri.Query); videoId = queryDictionary[YOUTUBE_VIDEO_IDENTIFIER] ?? videoUri.AbsolutePath.Split('/').LastOrDefault(); videoKind = VideoKindEnum.Youtube; } else if (!String.IsNullOrEmpty(match.Groups["vimeo"].Value)) { // Return last segment in absolute path as a video identifier. videoId = videoUri.AbsolutePath.Split('/').LastOrDefault(); videoKind = VideoKindEnum.Vimeo; } return(new VideoModel(videoUrl, videoId, videoKind)); } throw new NotSupportedException($"{videoUrl} doesn't refer to supported video type."); }
public void GetVideoModel_ProvidedYoutubeOrVimeoUrl_GetCorrectVideoModel(string videoUrl, string expectedVideoId, VideoKindEnum expectedVideoKind) { var actualModel = VideoHelper.GetVideoModel(videoUrl); Assert.That(actualModel, Is.Not.Null); Assert.Multiple(() => { Assert.That(actualModel.VideoUrl, Is.EqualTo(videoUrl)); Assert.That(actualModel.VideoId, Is.EqualTo(expectedVideoId)); Assert.That(actualModel.VideoKind, Is.EqualTo(expectedVideoKind)); }); }
public void GetVideoEmbedUrl_ProvidedVariousVideoKinds_GetCorrectVideoFormat(VideoKindEnum kind, string embedUrlPrefix) { var model = new VideoModel("foo", "123456", kind); string actualEmbedUrl = VideoHelper.GetVideoEmbedUrl(model); Assert.That(actualEmbedUrl, Does.StartWith(embedUrlPrefix)); }
/// <summary> /// Create a new instance of <see cref="VideoWidgetViewModel"/> /// </summary> /// <param name="videoUrl">Video URL.</param> /// <param name="videoId">Video identifier.</param> /// <param name="videoKind">Video kind.</param> public VideoWidgetViewModel(string videoUrl, string videoId, VideoKindEnum videoKind) { VideoUrl = videoUrl; VideoId = videoId; VideoKind = videoKind; }