Created
November 18, 2018 16:45
-
-
Save Citillara/e8c82d26424226420cea40076f534217 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Runtime.Serialization.Json; | |
using System.Net; | |
using System.Net.Cache; | |
using System.Runtime.Serialization; | |
using System.IO; | |
using System.Threading; | |
using System.Diagnostics; | |
using System.Globalization; | |
namespace TwitchGameListFetcher | |
{ | |
class Program | |
{ | |
const string CLIENT_ID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; | |
const string OAUTH = "OAuth xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; | |
static void Main(string[] args) | |
{ | |
TwitchResponse response = null; | |
List<TwitchGameChannel> games = new List<TwitchGameChannel>(); | |
int limit = 100; | |
int offset = 0; | |
do | |
{ | |
//Thread.Sleep(500); | |
response = GetData<TwitchResponse>(GetUrl(limit, offset)); | |
if (response == null) | |
break; | |
games.AddRange(response.GameChannels); | |
offset = offset + response.GameChannels.Count(); | |
if (offset > response.Total) | |
break; | |
} while (true); | |
var nfi = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone(); | |
nfi.NumberGroupSeparator = " "; | |
Console.WriteLine("DONE"); | |
var r = from g in games | |
orderby g.Viewers descending | |
select new { Name = g.Game.Name, Viewers = g.Viewers }; | |
var longest = games.Aggregate("", (max, cur) => max.Length > cur.Game.Name.Length ? max : cur.Game.Name); | |
var number_of_viewers = games.Sum(g => g.Viewers); | |
var number_of_channels = games.Sum(g => g.Channels); | |
var number_of_games = games.Count(); | |
StringBuilder sb = new StringBuilder(); | |
sb.AppendFormat("Total viewers : {0} - Total channels : {1} - Total games : {2}", | |
number_of_viewers.ToString("#,0", nfi), number_of_channels.ToString("#,0", nfi), number_of_games.ToString("#,0", nfi)); | |
sb.AppendLine(); | |
foreach (var rr in r) | |
{ | |
sb.Append(rr.Name); | |
for(int i = rr.Name.Length; i < longest.Length + 6; i++) | |
sb.Append(" "); | |
sb.AppendFormat("{0} - {1:0.00}%", rr.Viewers.ToString("#,0", nfi), (double)rr.Viewers * 100.0d / (double)number_of_viewers); | |
sb.AppendLine(); | |
} | |
File.WriteAllText("data.txt", sb.ToString()); | |
Process.Start("data.txt"); | |
} | |
static string GetUrl(int limit, int offset) | |
{ | |
return string.Format("https://api.twitch.tv/kraken/games/top?limit={0}&offset={1}", limit, offset); | |
} | |
static T GetData<T>(string url) | |
{ | |
Console.WriteLine(url); | |
HttpWebRequest request = WebRequest.CreateHttp(url); | |
T result = default(T); | |
request.UserAgent = "Retriving program"; | |
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; | |
request.Headers.Add("Accept-Language", "fr,en-US;q=0.8,en;q=0.6"); | |
request.Headers.Add("Client-Id", CLIENT_ID); | |
request.Headers.Add("Authorization", OAUTH); | |
request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache); | |
request.KeepAlive = true; | |
try | |
{ | |
using (Stream jsonSource = request.GetResponse().GetResponseStream()) | |
{ | |
var s = new DataContractJsonSerializer(typeof(T)); | |
result = (T)s.ReadObject(jsonSource); | |
} | |
} | |
catch | |
{ | |
return default(T); | |
} | |
return result; | |
} | |
} | |
[DataContract] | |
class TwitchResponse | |
{ | |
[DataMember(Name = "_total")] | |
public long Total; | |
[DataMember(Name = "_links")] | |
public TwitchLinks Links; | |
[DataMember(Name = "top")] | |
public TwitchGameChannel[] GameChannels; | |
} | |
[DataContract] | |
class TwitchLinks | |
{ | |
[DataMember(Name = "self")] | |
public string Self; | |
[DataMember(Name = "next", IsRequired=false)] | |
public string Next; | |
[DataMember(Name = "prev", IsRequired=false)] | |
public string Prev; | |
} | |
[DataContract] | |
class TwitchGameChannel | |
{ | |
[DataMember(Name = "viewers")] | |
public long Viewers; | |
[DataMember(Name = "channels")] | |
public long Channels; | |
[DataMember(Name = "game")] | |
public TwitchGame Game; | |
} | |
[DataContract] | |
class TwitchGame | |
{ | |
[DataMember(Name = "name")] | |
public string Name; | |
[DataMember(Name = "_id")] | |
public long Id; | |
[DataMember(Name = "giantbomb_id")] | |
public string GiantbombId; | |
[DataMember(Name = "box")] | |
public TwitchGameBox GameBox; | |
[DataMember(Name = "logo")] | |
public TwitchGameLogo Logo; | |
[DataMember(Name = "_links")] | |
public string[] Links; | |
} | |
[DataContract] | |
class TwitchGameBox | |
{ | |
[DataMember(Name = "large")] | |
public string Large; | |
[DataMember(Name = "medium")] | |
public string Medium; | |
[DataMember(Name = "small")] | |
public string Small; | |
[DataMember(Name = "template")] | |
public string Template; | |
} | |
[DataContract] | |
class TwitchGameLogo | |
{ | |
[DataMember(Name="large")] | |
public string Large; | |
[DataMember(Name = "medium")] | |
public string Medium; | |
[DataMember(Name = "small")] | |
public string Small; | |
[DataMember(Name = "template")] | |
public string Template; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment