public async Task <JObject> GetPageConfigAsync(string videoId, TimeSpan timeout) { string result; using (var httpClient = new HttpClient()) { var builder = new UriBuilder("https://youtube.com/watch?v=" + videoId); builder.Port = -1; var query = HttpUtility.ParseQueryString(builder.Query); query["hl"] = "en"; query["bpctr"] = (DateTime.Now.Ticks / 1000).ToString(); builder.Query = query.ToString(); httpClient.Timeout = timeout; httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/4.0"); // senza su xamarin forms con android non mi ritornava ytplayer.config result = await httpClient.GetStringAsync(builder.ToString()); } string configJson = DataFormatTools.ExtractBetween(result, "ytplayer.config = ", ";ytplayer.load"); if (configJson == null) { return(null); } return(JObject.Parse(configJson)); }
internal string[] ExtractActions(string content) { Match objResult = SignatureRegexes.actionsObjRegexp.Match(content); Match funcResult = SignatureRegexes.actionsFuncRegexp.Match(content); if (!objResult.Success || !funcResult.Success) { return(null); } string obj = objResult.Groups[1].Value.Replace("\\$", "\\\\$"); string objBody = objResult.Groups[2].Value.Replace("\\$", "\\\\$"); string funcBody = funcResult.Groups[1].Value.Replace("\\$", "\\\\$"); Match reverseResult = SignatureRegexes.reverseRegexp.Match(objBody); Match sliceResult = SignatureRegexes.sliceRegexp.Match(objBody); Match spliceResult = SignatureRegexes.spliceRegexp.Match(objBody); Match swapResult = SignatureRegexes.swapRegexp.Match(objBody); string reverseKey = DataFormatTools.CleanQuotations(reverseResult.Groups[1].Value.Replace("\\$", "\\\\$")); string sliceKey = DataFormatTools.CleanQuotations(sliceResult.Groups[1].Value.Replace("\\$", "\\\\$")); string spliceKey = DataFormatTools.CleanQuotations(spliceResult.Groups[1].Value.Replace("\\$", "\\\\$")); string swapKey = DataFormatTools.CleanQuotations(swapResult.Groups[1].Value.Replace("\\$", "\\\\$")); string keys = "(" + string.Join("|", reverseKey, sliceKey, spliceKey, swapKey) + ")"; string keyStr = "(?:a=)?" + obj + "(?:\\." + keys + "|\\['" + keys + "'\\]|\\[\"" + keys + "\"\\])\\(a,(\\d+)\\)"; Regex keyRegex = new Regex(keyStr); List <string> tokens = new List <string>(); MatchCollection matches = keyRegex.Matches(funcBody); foreach (Match m in matches) { string key = m.Groups[1].Value ?? m.Groups[2].Value ?? m.Groups[3].Value; if (key == swapKey) { tokens.Add("w" + m.Groups[4].Value); } else if (key == reverseKey) { tokens.Add("r"); } else if (key == sliceKey) { tokens.Add("s" + m.Groups[4].Value); } else if (key == spliceKey) { tokens.Add("p" + m.Groups[4].Value); } } return(tokens.ToArray()); }