Skip to content

Commit

Permalink
[Application] Yt parser fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Dec 10, 2020
1 parent 2b71eec commit e2a0e3c
Showing 1 changed file with 44 additions and 8 deletions.
52 changes: 44 additions & 8 deletions CastIt.Application/Youtube/YoutubeUrlDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public async Task<List<string>> ParseYouTubePlayList(string url, CancellationTok
{
_logger.LogInformation($"{nameof(ParseYouTubePlayList)}: Body contains pure javascript, parsing it...");
string pattern = @"(\/watch).*?(?="")";
body = body.Replace("\\u0026", "&").Replace("\\/", "/");
body = ReplaceWithAmpersand(body).Replace("\\/", "/");
links = Regex.Matches(body, pattern)
.Select(match => RemoveNotNeededParams(YoutubeUrl + match.Value))
.Where(link => link.StartsWith(YoutubeUrl, StringComparison.OrdinalIgnoreCase))
Expand Down Expand Up @@ -198,8 +198,7 @@ private string GetKeyContentValue(string str)

private string GetURLEncodedStream(string stream)
{
// replace all the \u0026 with &
string str = DecodeUrlString(stream).Replace("\\u0026", "&");
string str = ReplaceWithAmpersand(DecodeUrlString(stream));
string urlMap = str.Substring(str.IndexOf("url=http", StringComparison.OrdinalIgnoreCase) + 4);
// search urlMap until we see either a & or ,
var sb = new StringBuilder();
Expand All @@ -214,8 +213,17 @@ private string GetURLEncodedStream(string stream)

private async Task<string> GetUrlFromCipher(string cipher, string jsUrl)
{
jsUrl = ReplaceWithAmpersand(jsUrl);
cipher = ReplaceWithAmpersand(cipher);

string urlPattern = @"(?<=url[^&]+).*?(?=\\"")";
var urlMatch = Regex.Match(cipher, urlPattern);
if (!urlMatch.Success)
{
_logger.LogInformation($"{nameof(GetUrlFromCipher)}: No match was found, trying without the backslash in the pattern...");
urlMatch = Regex.Match(cipher, @"(?<=url[^&]+).*?(?=\"")");
}

if (string.IsNullOrEmpty(urlMatch.Value))
{
var msg = $"Couldn't retrieve url from cipher = {cipher}";
Expand All @@ -230,13 +238,19 @@ private async Task<string> GetUrlFromCipher(string cipher, string jsUrl)
string s = DecodeUrlString(Regex.Match(cipher, sPattern).Value);
if (string.IsNullOrEmpty(s))
{
_logger.LogWarning($"Couldn't find the sPattern in cipher = {cipher}");
_logger.LogInformation($"{nameof(GetUrlFromCipher)}: Couldn't find the sPattern in cipher, trying without the backslash in the pattern...");
s = DecodeUrlString(Regex.Match(cipher, @"(?<=\""s=)([^&]+)").Value);
}

if (string.IsNullOrEmpty(s))
{
_logger.LogWarning($"{nameof(GetUrlFromCipher)}: Couldn't find the sPattern in cipher = {cipher}");
return url;
}

s = await JsDescramble(s, jsUrl);

string sp = Regex.Match(cipher, "sp=([^&]+)").Value?.Split("=".ToCharArray())?.Last();
string sp = Regex.Match(cipher, "sp=([^&]+)").Value.Split("=".ToCharArray()).Last();
if (string.IsNullOrEmpty(sp))
{
sp = "signature";
Expand Down Expand Up @@ -472,13 +486,25 @@ private Dictionary<int, string> GetVideoQualities(string body)
throw new Exception("Couldn't retrieve video qualities");
}

string streamMap = DecodeUrlString(formatMatch.Value).Replace(@"\\u0026", "&");
string streamMap = ReplaceWithAmpersand(DecodeUrlString(formatMatch.Value));

string heightPattern = @"(?<=\\""height\\"":).+?(?=,)";
string heightPatternA = @"(?<=\\""height\\"":).+?(?=,)";
string heightPatternB = @"(?<=\""height\"":).+?(?=,)";
var streams = Regex.Matches(streamMap, "{(.*?)}").AsQueryable().OfType<Match>().ToList();
var qualities = streams.ToDictionary(k =>
{
var val = Regex.Match(k.ToString(), heightPattern).Value;
var val = Regex.Match(k.ToString(), heightPatternA).Value;
if (string.IsNullOrEmpty(val))
{
_logger.LogInformation($"Couldn't retrieve height, trying now without backslashes in the pattern");
val = Regex.Match(k.ToString(), heightPatternB).Value;
}

if (string.IsNullOrEmpty(val))
{
_logger.LogWarning($"Couldn't retrieve height from = {k}");
}

return int.Parse(string.IsNullOrEmpty(val) ? "-1" : val);
}, v => v.ToString());

Expand Down Expand Up @@ -516,6 +542,12 @@ private async Task<string> GetFinalUrl(string body, string pickedQualityUrl)
jsMatch = Regex.Match(playerConfig, "(\"jsUrl\":.*?.js)");
}

if (!jsMatch.Success)
{
_logger.LogInformation($"{nameof(GetFinalUrl)}: Js url was not found in player config, checking if we have a jsUrl key in the body...");
jsMatch = Regex.Match(body, "(\"jsUrl\":.*?.js)");
}

string jsUrl = jsMatch.Value;
if (string.IsNullOrWhiteSpace(jsUrl))
{
Expand Down Expand Up @@ -588,5 +620,9 @@ private bool SpecialCharsExists(string input)
Array.Resize(ref two, c);
return two.Length > 0;
}

// replace all the \u0026 with &
private string ReplaceWithAmpersand(string val)
=> val.Replace("\\u0026", "&").Replace("\u0026", "&");
}
}

0 comments on commit e2a0e3c

Please sign in to comment.