public async Task <dynamic> GetSongObj(string songName, int sCount) { client.RequestUri = $"https://theinfoapps.com/myfm/search/v2/?appid=com.tenormusicfm.ios.fm&page_no={sCount}&query={songName}"; getJsonUri = await client.Get(clientCookie, clientHeader); obj = DynamicJson.Parse(getJsonUri); if (!this.ExistsSongObject(obj)) { return(null); } return(obj); }
private void thread() { while (true) { try { var req = new HClient(); var result = req.Get("https://www.cl.cam.ac.uk/research/dtg/weather/current-obs.txt").Expect(HttpStatusCode.OK).DataString; var datetime = Regex.Match(result, @"at (?<time>\d+:\d\d (AM|PM)) on (?<date>\d+ \w\w\w \d\d):"); var dt = DateTime.ParseExact(datetime.Groups["date"].Value + "@" + datetime.Groups["time"].Value, "dd MMM yy'@'h:mm tt", null); var curTemp = decimal.Parse(Regex.Match(result, @"Temperature:\s+(?<temp>-?\d+(\.\d)?) C").Groups["temp"].Value); _temperatures.RemoveAllByKey(date => date < DateTime.UtcNow - TimeSpan.FromDays(8)); _temperatures[DateTime.UtcNow] = curTemp; using (var db = Db.Open()) db.Insert(new TbWeatherTemperature { Timestamp = DateTime.UtcNow.ToDbDateTime(), Temperature = (double)curTemp }); var dto = new WeatherDto(); dto.ValidUntilUtc = DateTime.UtcNow + TimeSpan.FromMinutes(30); dto.CurTemperature = _temperatures.Where(kvp => kvp.Key >= DateTime.UtcNow.AddMinutes(-15)).Average(kvp => kvp.Value); var temps = _temperatures.OrderBy(kvp => kvp.Key).ToList(); var avg = temps.Select(kvp => (time: kvp.Key, temp: temps.Where(x => x.Key >= kvp.Key.AddMinutes(-7.5) && x.Key <= kvp.Key.AddMinutes(7.5)).Average(x => x.Value))).ToList(); var min = findExtreme(avg, 5, seq => seq.MinElement(x => x.temp)); dto.MinTemperature = min.temp; dto.MinTemperatureAtTime = $"{min.time.ToLocalTime():HH:mm}"; dto.MinTemperatureAtDay = min.time.ToLocalTime().Date == DateTime.Today ? "today" : "yesterday"; var max = findExtreme(avg, 12, seq => seq.MaxElement(x => x.temp)); dto.MaxTemperature = max.temp; dto.MaxTemperatureAtTime = $"{max.time.ToLocalTime():HH:mm}"; dto.MaxTemperatureAtDay = max.time.ToLocalTime().Date == DateTime.Today ? "today" : "yesterday"; dto.CurTemperatureColor = getTemperatureDeviationColor(dto.CurTemperature, DateTime.Now, avg); dto.MinTemperatureColor = getTemperatureDeviationColor(min.temp, min.time.ToLocalTime(), avg); dto.MaxTemperatureColor = getTemperatureDeviationColor(max.temp, max.time.ToLocalTime(), avg); PopulateSunriseSunset(dto, DateTime.Today); SendUpdate(dto); } catch { } Thread.Sleep(TimeSpan.FromSeconds(60)); } }
private static void RetrievePonyCoatColorsFromMlpWikia() { var ponyColors = ClassifyJson.DeserializeFile <Dictionary <string, string> >(_poniesJson); ponyColors.Where(kvp => kvp.Value == null).ParallelForEach(kvp => { var pony = kvp.Key; try { var client = new HClient(); var response = client.Get(@"http://mlp.wikia.com/wiki/" + pony.Replace(' ', '_')); var str = response.DataString; var doc = CQ.CreateDocument(str); var infoboxes = doc["table.infobox"]; string color = null; for (int i = 0; i < infoboxes.Length - 1; i++) { if (infoboxes[i].Cq()["th[colspan='2']"].FirstOrDefault()?.InnerText.Contains(pony) != true) { continue; } var colorTr = infoboxes[i + 1].Cq().Find("tr").Where(tr => tr.Cq().Find("td>b").Any(td => td.InnerText == "Coat")).ToArray(); if (colorTr.Length == 0 || colorTr[0].Cq().Find("td").Length != 2) { continue; } var colorSpan = colorTr[0].Cq().Find("td>span"); var styleAttr = colorSpan[0]["style"]; var m = Regex.Match(styleAttr, @"background-color\s*:\s*#([A-F0-9]{3,6})"); if (m.Success) { color = m.Groups[1].Value; } } lock (ponyColors) { ConsoleUtil.WriteLine($"{pony.Color(ConsoleColor.Cyan)} = {(color == null ? "<nope>".Color(ConsoleColor.Red) : color.Color(ConsoleColor.Green))}", null); //if (color != null) ponyColors[pony] = color ?? "?"; } } catch (Exception e) { lock (ponyColors) ConsoleUtil.WriteLine($"{pony.Color(ConsoleColor.Cyan)} = {e.Message.Color(ConsoleColor.Red)} ({e.GetType().FullName.Color(ConsoleColor.DarkRed)})", null); } }); ClassifyJson.SerializeToFile(ponyColors, _poniesJson); }
private HResponse retryOnAuthHeaderFail(string url, HClient hClient, Func <SummonerInfo, string> getAuthHeader) { while (true) { var resp = hClient.Get(url); if (resp.StatusCode != HttpStatusCode.Unauthorized) { return(resp); } var newHeader = getAuthHeader(this); if (newHeader == null) { return(null); } AuthorizationHeader = newHeader; hClient.ReqHeaders["Authorization"] = newHeader; save(); } }
public static void Load(string path) { var hc = new HClient(); // Load version info var versionsStr = hc.Get("https://ddragon.leagueoflegends.com/api/versions.js").Expect(HttpStatusCode.OK).DataString; versionsStr = versionsStr.Replace("Riot.DDragon.versions = ", "").Replace(";", ""); var versions = JsonList.Parse(versionsStr); GameVersion = versions.First().GetString(); // Load champion data var championDataUrl = $"https://ddragon.leagueoflegends.com/cdn/{GameVersion}/data/en_US/champion.json"; var championDataPath = Path.Combine(path, championDataUrl.FilenameCharactersEscape()); string championDataStr; Directory.CreateDirectory(path); if (File.Exists(championDataPath)) { championDataStr = File.ReadAllText(championDataPath); } else { championDataStr = hc.Get(championDataUrl).Expect(HttpStatusCode.OK).DataString; File.WriteAllText(championDataPath, championDataStr); } var championData = JsonDict.Parse(championDataStr); Champions = new ReadOnlyDictionary <int, ChampionInfo>( championData["data"].GetDict().Select(kvp => new ChampionInfo(kvp.Key, kvp.Value.GetDict())).ToDictionary(ch => ch.Id, ch => ch) ); // Load item data var itemDataUrl = $"https://ddragon.leagueoflegends.com/cdn/{GameVersion}/data/en_US/item.json"; var itemDataPath = Path.Combine(path, itemDataUrl.FilenameCharactersEscape()); string itemDataStr; if (File.Exists(itemDataPath)) { itemDataStr = File.ReadAllText(itemDataPath); } else { itemDataStr = hc.Get(itemDataUrl).Expect(HttpStatusCode.OK).DataString; File.WriteAllText(itemDataPath, itemDataStr); } var itemData = JsonDict.Parse(itemDataStr); Items = new ReadOnlyDictionary <int, ItemInfo>( itemData["data"].GetDict().Select(kvp => new ItemInfo(kvp.Key, kvp.Value.GetDict(), GameVersion)).ToDictionary(ch => ch.Id, ch => ch) ); foreach (var item in Items.Values) { item.NoUnconditionalChildren = item.BuildsInto.All(ch => Items[ch].RequiredAlly != null || Items[ch].RequiredChampion != null); var allFrom = (item.SpecialRecipeFrom == null ? item.BuildsFrom : item.BuildsFrom.Concat(item.SpecialRecipeFrom.Value)).Concat(item.AllFrom).Where(id => Items.ContainsKey(id)).Select(id => Items[id]).ToList(); foreach (var fr in allFrom) { item.AllFrom.Add(fr.Id); fr.AllInto.Add(item.Id); } foreach (var into in item.BuildsInto.Concat(item.AllInto).Where(id => Items.ContainsKey(id)).Select(id => Items[id]).ToList()) { item.AllInto.Add(into.Id); into.AllFrom.Add(item.Id); } } IEnumerable <int> recursiveItems(int item, bool children) { foreach (var child in children ? Items[item].AllInto : Items[item].AllFrom) { yield return(child); foreach (var sub in recursiveItems(child, children)) { yield return(sub); } } } foreach (var item in Items.Values) { item.AllIntoTransitive = recursiveItems(item.Id, children: true).Distinct().Select(id => Items[id]).ToList().AsReadOnly(); item.AllFromTransitive = recursiveItems(item.Id, children: false).Distinct().Select(id => Items[id]).ToList().AsReadOnly(); item.NoPurchasableChildren = item.AllIntoTransitive.All(ch => !ch.Purchasable); } }
public static void GetPlayers() { var h = new HClient(); var allData = new Dictionary <string, Dictionary <string, Dictionary <string, Dictionary <string, int> > > >(); foreach (var tournament in new[] { "Wimbledon Championships", "US Open", "French Open" }) { foreach (var isMale in new[] { true, false }) { foreach (var year in Enumerable.Range(1968, 2018 - 1968 + 1)) { var path = $@"D:\c\KTANE\KtaneStuff\DataFiles\Tennis\{tournament} {year}{(isMale ? "" : " (W)")}.txt"; var write = false; string data; if (File.Exists(path)) { data = File.ReadAllText(path); } else { try { Console.WriteLine($"Downloading: {tournament} {year} ({(isMale ? "M" : "W")})"); var raw = h.Get($@"https://en.wikipedia.org/w/index.php?title={year}_{tournament.Replace(' ', '_')}_%E2%80%93_{(isMale ? "Men" : "Women")}%27s_Singles&action=raw"); data = raw.DataString; write = true; } catch (Exception e) { ConsoleUtil.WriteLine($"{year.ToString().Color(ConsoleColor.Green)} {tournament.Color(ConsoleColor.Green)}: {e.Message.Color(ConsoleColor.Magenta)} {e.GetType().FullName.Color(ConsoleColor.Red)}", ConsoleColor.DarkRed); continue; } } var m = Regex.Match(data, @"^===.*(Finals|Final Eight).*===", RegexOptions.Multiline | RegexOptions.IgnoreCase); if (!m.Success) { ConsoleUtil.WriteLine($"{year.ToString().Color(ConsoleColor.Cyan)} {tournament.Color(ConsoleColor.Cyan)}: {"Finals section not found".Color(ConsoleColor.Magenta)}", ConsoleColor.Cyan); continue; } var subdata = data.Substring(m.Index); m = Regex.Match(subdata, @"^\}\}", RegexOptions.Multiline); if (!m.Success) { ConsoleUtil.WriteLine($"{year.ToString().Color(ConsoleColor.Cyan)} {tournament.Color(ConsoleColor.Cyan)}: {"Could not find end of table.".Color(ConsoleColor.Magenta)}", ConsoleColor.Cyan); continue; } subdata = subdata.Substring(0, m.Index); foreach (var encounter in subdata.Replace("\r", "").Split('\n') .Select(line => new { Line = line, Match = Regex.Match(line, @"^\|\s*RD(\d+)-team(\d+)\s*=\s*(?:\{\{flagicon\|[ \w]+(?:\|\d+)?\}\}\s*|'''|\{\{nowrap\|)*(?:\[\[)?(.*?)((?:\]\]\s*|'''\s*|\}\}\s*)*)( \(''\[\[|$)", RegexOptions.IgnoreCase) }) .Where(line => line.Match.Success) .Select(line => new { line.Line, Round = int.Parse(line.Match.Groups[1].Value), Place = int.Parse(line.Match.Groups[2].Value) - 1, Name = line.Match.Groups[3].Value, Suffix = line.Match.Groups[4].Value, IsWinner = line.Match.Groups[4].Value.Contains("'''") }) .Select(line => new { line.Line, line.Round, line.Place, Name = (line.Name.Contains('|') ? line.Name.Remove(line.Name.IndexOf('|')) : line.Name).Replace(" (tennis)", "").Replace(" (tennis player)", ""), line.Suffix, line.IsWinner }) .GroupBy(line => line.Round * 100 + (line.Place >> 1))) { Clipboard.SetText($@"https://en.wikipedia.org/w/index.php?title={year}_{tournament.Replace(' ', '_')}_%E2%80%93_{(isMale ? "Men" : "Women")}%27s_Singles&action=edit§ion=4"); var winner = modify(encounter.First(g => g.IsWinner).Name); var loser = modify(encounter.First(g => !g.IsWinner).Name); if (winner == null || loser == null) { continue; } allData.IncSafe(tournament, isMale ? "Men" : "Women", winner, loser); } if (write) { File.WriteAllText(path, data); } } } } Utils.ReplaceInFile(@"D:\c\KTANE\Tennis\Assets\Data.cs", "// Start auto-generated", "// End auto-generated", new[] { ("Wimbledon Championships", "wimbledon"), ("US Open", "usOpen"), ("French Open", "frenchOpen") }.SelectMany(tournament =>