namespace VideoBrowser.Helpers
{
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using VideoBrowser.Core;
///
/// Defines the .
///
public static class YoutubeHelper
{
#region Methods
///
/// Returns a fixed URL, stripped of unnecessary invalid information.
///
/// The URL to fix.
/// The .
public static string FixUrl(string url)
{
// Remove "Watch Later" information, causes error
url = url.Replace("&index=6&list=WL", "");
return url;
}
///
/// Returns a formatted string of the given title, stripping illegal characters and replacing HTML entities with their actual character. (e.g. " -> ').
///
/// The title to format.
/// The .
public static string FormatTitle(string title)
{
var illegalCharacters = new string[] { "/", @"\", "*", "?", "\"", "<", ">" };
var replace = new Dictionary()
{
{"|", "-"},
{"'", "'"},
{""", "'"},
{"<", "("},
{">", ")"},
{"+", " "},
{":", "-"},
{"amp;", "&"}
};
var sb = new System.Text.StringBuilder(title);
foreach (string s in illegalCharacters)
{
sb.Replace(s, string.Empty);
}
foreach (KeyValuePair s in replace)
{
sb.Replace(s.Key, s.Value);
}
return sb.ToString().Trim();
}
///
/// Returns the highest quality audio format from the given VideoFormat.
///
/// The format to get audio format from.
/// The .
public static VideoFormat GetAudioFormat(VideoFormat format)
{
var audio = new List();
// Add all audio only formats
audio.AddRange(format.VideoInfo.Formats.FindAll(f => f.AudioOnly == true && f.Extension != "webm"));
// Return null if no audio is found
if (audio.Count == 0)
{
return null;
}
// Return either the one with the highest audio bit rate, or the last found one
return audio.OrderBy(a => a.AudioBitRate).Last();
}
///
/// Returns the playlist id from given url.
///
/// The url to get playlist id from.
/// The .
public static string GetPlaylistId(string url)
{
var regex = new Regex(@"^(?:https?://)?(?:www.)?youtube.com/.*list=([0-9a-zA-Z\-_]*).*$");
return regex.Match(url).Groups[1].Value;
}
///
/// Returns true if the given url is a playlist YouTube url.
///
/// The url to check.
/// The .
public static bool IsPlaylist(string url)
{
var regex = new Regex(@"^(?:https?://)?(?:www.)?youtube.com/.*list=([0-9a-zA-Z\-_]*).*$");
return regex.IsMatch(url);
}
///
/// Returns true if the given url is a valid YouTube url.
///
/// The url to check.
/// The .
public static bool IsValidYouTubeUrl(string url)
{
if (!url.ToLower().Contains("www.youtube.com/watch?"))
{
return false;
}
var pattern = @"^(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?$";
var regex = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
return regex.IsMatch(url);
}
///
/// The CheckFormats.
///
/// The list.
/// The .
internal static VideoFormat[] CheckFormats(IList list)
{
var formats = new List(list.Distinct());
formats.RemoveAll(f => f.Extension.Contains("webm") ||
f.HasAudioAndVideo ||
f.FormatID == "meta");
return formats.ToArray();
}
#endregion Methods
}
}