using Newtonsoft.Json.Linq;
private static JObject GetJsonData(string url)
using (var wc = new WebClient())
var jsonData = wc.DownloadString(url);
return JObject.Parse(jsonData);
private static JObject RefactorPulses(JObject pulseData)
JObject jrefact = JObject.Parse("{ \"Pulses\":[] }");
var pulses = (JArray)jrefact["Pulses"];
var error = (string)pulseData["error"];
if(string.IsNullOrEmpty(error) == true)
foreach(var r in pulseData)
public static void Main()
var teamName = "keyboard-jockeys";
var epochLastSeven = DateTimeOffset.UtcNow.AddDays(-7).ToUnixTimeSeconds();
var epoch = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
var userPulseUrl = $@"http://api.whatpulse.org/pulses.php?user={{0}}&format=json&start={epochLastSeven}&end={epoch}";
var teamUrl = $@"http://api.whatpulse.org/team.php?team={teamName}&members=yes&format=json";
var pulseUrl = $@"http://api.whatpulse.org/pulses.php?team={teamName}&format=json&start={epochLastSeven}&end={epoch}";
var teamData = GetJsonData(teamUrl);
JObject memberRefact = JObject.Parse("{ \"Members\":[] }");
var members = (JArray)memberRefact["Members"];
foreach(var r in teamData["Members"])
foreach(var p in members)
var getUrl = String.Format(userPulseUrl,p["Username"]);
var jdata = GetJsonData(getUrl);
JObject jrefact = RefactorPulses(jdata);
var days = jrefact["Pulses"].GroupBy(x=>x["Timedate"].Value<DateTime>().ToString("yyyy-MM-dd")).Count();
var clicks = jrefact["Pulses"].Select(x=>x["Clicks"].Value<int>()).Sum();
var keys = jrefact["Pulses"].Select(x=>x["Keys"].Value<int>()).Sum();
var minDate = jrefact["Pulses"].Select(x=>x["Timedate"].Value<DateTime?>()).Min() ?? DateTime.Now;
var maxDate = jrefact["Pulses"].Select(x=>x["Timedate"].Value<DateTime?>()).Max() ?? DateTime.Now;
var keys1hr = jrefact["Pulses"].Where(x=>x["Timedate"].Value<DateTime?>()>=maxDate.AddHours(-1)).Select(x=>x["Keys"].Value<int>()).Sum();
var keys6hr = jrefact["Pulses"].Where(x=>x["Timedate"].Value<DateTime?>()>=maxDate.AddHours(-6)).Select(x=>x["Keys"].Value<int>()).Sum();
var keys12hr = jrefact["Pulses"].Where(x=>x["Timedate"].Value<DateTime?>()>=maxDate.AddHours(-12)).Select(x=>x["Keys"].Value<int>()).Sum();
Console.WriteLine($"player: {p["Username"]} [{days} days active, {p["Keys"]} keys ever - {p["Clicks"]} clicks ever]");
Console.WriteLine($"date range: {minDate.ToString("yyyy-MM-dd HH:mm")} - {maxDate.ToString("yyyy-MM-dd HH:mm")}");
Console.WriteLine($"keys - clicks: {keys} - {clicks}");
Console.WriteLine($"-1hr keys: {keys1hr} - {decimal.Divide(keys1hr,1):F2}");
Console.WriteLine($"-6hr keys: {keys6hr} - {decimal.Divide(keys6hr,6):F2}");
Console.WriteLine($"-12hr keys: {keys12hr} - {decimal.Divide(keys12hr,12):F2}");
Console.WriteLine($"daily key avg: {decimal.Divide(keys,days):F2}");
Console.WriteLine($"click ratio: {decimal.Divide( clicks,keys)*100:F2} %");