Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Net;
- using System.Net.Security;
- using System.Security.Cryptography.X509Certificates;
- using System.Text;
- /*
- {
- "appid":730,
- "name":"Counter-Strike: Global Offensive",
- "developer":"Valve",
- "publisher":"Valve",
- "score_rank":79,
- "owners":28974829,
- "owners_variance":149540,
- "players_forever":28328069,
- "players_forever_variance":147989,
- "players_2weeks":9354209,
- "players_2weeks_variance":87157,
- "average_forever":17644,
- "average_2weeks":831,
- "median_forever":5058,
- "median_2weeks":390,
- "ccu":650895,
- "price":"1499",
- "tags":{
- "FPS":16943,
- "Multiplayer":13624,
- "Shooter":12713,
- "Action":10799,
- "Team-Based":10285,
- "Competitive":9584,
- "Tactical":8465,
- "First-Person":7256,
- "e-sports":6662,
- "PvP":6330,
- "Online Co-Op":5654,
- "Military":4564,
- "Co-op":4393,
- "Strategy":4376,
- "War":4312,
- "Trading":3164,
- "Realistic":3160,
- "Difficult":3127,
- "Fast-Paced":3061,
- "Moddable":2471
- }
- }
- * */
- public class Game
- {
- public int appid;
- public string name;
- public string developer;
- public string publisher;
- public int score_rank;
- public int owners;
- public int owners_variance;
- public int players_forever;
- public int players_forever_variance;
- public int players_2weeks;
- public int players_2weeks_variance;
- public int average_forever;
- public int average_2weeks;
- public int median_forever;
- public int median_2weeks;
- public int ccu;
- public float price;
- public string[] tags;
- public int earnings;
- public bool isIndie;
- public Game(JSONObject json)
- {
- appid = ParseInt(json.Fields[0]);
- name = (string)json.Fields[1].GetValue();
- developer = (string)json.Fields[2].GetValue();
- publisher = (string)json.Fields[3].GetValue();
- score_rank = ParseInt(json.Fields[4]);
- owners = ParseInt(json.Fields[5]);
- owners_variance = ParseInt(json.Fields[6]);
- players_forever = ParseInt(json.Fields[7]);
- players_forever_variance = ParseInt(json.Fields[8]);
- players_2weeks = ParseInt(json.Fields[9]);
- players_2weeks_variance = ParseInt(json.Fields[10]);
- average_forever = ParseInt(json.Fields[11]);
- average_2weeks = ParseInt(json.Fields[12]);
- median_forever = ParseInt(json.Fields[13]);
- median_2weeks = ParseInt(json.Fields[14]);
- ccu = ParseInt(json.Fields[15]);
- string priceString = (string)json.Fields[16].GetValue();
- if (priceString == null)
- {
- price = 0;
- }
- else
- {
- price = float.Parse(priceString) / 100f;
- }
- earnings = (int)(price * owners);
- isIndie = false;
- JSONArrayField tagArrayField = json.Fields[17] as JSONArrayField;
- if (tagArrayField != null)
- {
- tags = new string[0];
- }
- else
- {
- JSONObject tagObject = (JSONObject)json.Fields[17].GetValue();
- tags = new string[tagObject.FieldCount];
- for (int i = 0; i < tags.Length; i++)
- {
- tags[i] = tagObject.Fields[i].Name;
- if (tags[i] == "Indie")
- {
- isIndie = true;
- }
- }
- }
- }
- int ParseInt(JSONField field)
- {
- JSONIntField intField = field as JSONIntField;
- if (intField != null)
- {
- return intField.Value;
- }
- return -1;
- }
- }
- public class TagData
- {
- public string name;
- public int numGames;
- public float averagePrice;
- public float medianPrice;
- public float averageScore;
- public float medianScore;
- public float averageOwners;
- public float medianOwners;
- public float averagePlayers;
- public float medianPlayers;
- public float averageEarnings;
- public float medianEarnings;
- public float medianOwnersPerScore;
- public float medianEarningsPerScore;
- public float percentAbove5k;
- public float percentAbove10k;
- public float percentAbove20k;
- public float percentAbove30k;
- public float percentAbove50k;
- public float percentAbove100k;
- public float percentAbove250k;
- public float percentAbove500k;
- public float percentAbove1m;
- public Game[] games;
- const char SEPERATOR = ',';
- public TagData(string tagName, List<Game> gameList)
- {
- for (int i = 0; i < gameList.Count; i++)
- {
- if (!AllowGame(gameList[i]))
- {
- gameList.RemoveAt(i);
- i--;
- }
- }
- gameList.Sort((a, b) => b.owners.CompareTo(a.owners));
- games = gameList.ToArray();
- name = tagName;
- numGames = gameList.Count;
- if (numGames > 0)
- {
- float[] allPrices = new float[numGames];
- float[] allScores = new float[numGames];
- float[] allOwners = new float[numGames];
- float[] allPlayers = new float[numGames];
- float[] allEarnings = new float[numGames];
- for (int i = 0; i < numGames; i++)
- {
- allPrices[i] = gameList[i].price;
- allScores[i] = gameList[i].score_rank;
- allOwners[i] = gameList[i].owners;
- allPlayers[i] = gameList[i].players_forever;
- allEarnings[i] = gameList[i].earnings;
- if (gameList[i].owners > 5000) percentAbove5k++;
- if (gameList[i].owners > 10000) percentAbove10k++;
- if (gameList[i].owners > 20000) percentAbove20k++;
- if (gameList[i].owners > 30000) percentAbove30k++;
- if (gameList[i].owners > 50000) percentAbove50k++;
- if (gameList[i].owners > 100000) percentAbove100k++;
- if (gameList[i].owners > 250000) percentAbove250k++;
- if (gameList[i].owners > 500000) percentAbove500k++;
- if (gameList[i].owners > 1000000) percentAbove1m++;
- }
- percentAbove5k = (percentAbove5k / numGames) * 100f;
- percentAbove10k = (percentAbove10k / numGames) * 100f;
- percentAbove20k = (percentAbove20k / numGames) * 100f;
- percentAbove30k = (percentAbove30k / numGames) * 100f;
- percentAbove50k = (percentAbove50k / numGames) * 100f;
- percentAbove100k = (percentAbove100k / numGames) * 100f;
- percentAbove250k = (percentAbove250k / numGames) * 100f;
- percentAbove500k = (percentAbove500k / numGames) * 100f;
- percentAbove1m = (percentAbove1m / numGames) * 100f;
- averagePrice = GetAverage(allPrices);
- medianPrice = GetMedian(allPrices);
- averageScore = GetAverage(allScores);
- medianScore = GetMedian(allScores);
- averageOwners = GetAverage(allOwners);
- medianOwners = GetMedian(allOwners);
- averagePlayers = GetAverage(allPlayers);
- medianPlayers = GetMedian(allPlayers);
- averageEarnings = GetAverage(allEarnings);
- medianEarnings = GetMedian(allEarnings);
- medianOwnersPerScore = medianOwners / medianScore;
- medianEarningsPerScore = medianEarnings / medianScore;
- }
- }
- bool AllowGame(Game game)
- {
- if (game.price < 9) return false;
- if (game.price > 30) return false;
- if (!game.isIndie) return false;
- if (game.score_rank < 50) return false;
- // if (game.owners > 1000000) return false;
- return true;
- }
- public void Log()
- {
- Console.WriteLine("tag: " + name);
- Console.WriteLine("numGames: " + numGames);
- Console.WriteLine("averagePrice: " + averagePrice);
- Console.WriteLine("medianPrice: " + medianPrice);
- Console.WriteLine("averageScore: " + averageScore);
- Console.WriteLine("medianScore: " + medianScore);
- Console.WriteLine("averageOwners: " + averageOwners);
- Console.WriteLine("medianOwners: " + medianOwners);
- Console.WriteLine("averagePlayers: " + averagePlayers);
- Console.WriteLine("medianPlayers: " + medianPlayers);
- Console.WriteLine("averageEarnings: " + averageEarnings);
- Console.WriteLine("medianEarnings: " + medianEarnings);
- Console.WriteLine("medianOwnersPerScore: " + medianOwnersPerScore);
- Console.WriteLine("medianEarningsPerScore: " + medianEarningsPerScore);
- Console.WriteLine("percentAbove5k: " + percentAbove5k);
- Console.WriteLine("percentAbove10k: " + percentAbove10k);
- Console.WriteLine("percentAbove20k: " + percentAbove20k);
- Console.WriteLine("percentAbove30k: " + percentAbove30k);
- Console.WriteLine("percentAbove50k: " + percentAbove50k);
- Console.WriteLine("percentAbove100k: " + percentAbove100k);
- Console.WriteLine("percentAbove250k: " + percentAbove250k);
- Console.WriteLine("percentAbove500k: " + percentAbove500k);
- Console.WriteLine("percentAbove1m: " + percentAbove1m);
- }
- public string GetCSVString()
- {
- StringBuilder builder = new StringBuilder(name);
- builder.Append(SEPERATOR);
- builder.Append(numGames);
- builder.Append(SEPERATOR);
- builder.Append(FormatNumber(averagePrice));
- builder.Append(SEPERATOR);
- builder.Append(FormatNumber(medianPrice));
- builder.Append(SEPERATOR);
- builder.Append(FormatNumber(averageScore));
- builder.Append(SEPERATOR);
- builder.Append(FormatNumber(medianScore));
- builder.Append(SEPERATOR);
- builder.Append(FormatNumber(averageOwners));
- builder.Append(SEPERATOR);
- builder.Append(FormatNumber(medianOwners));
- builder.Append(SEPERATOR);
- builder.Append(FormatNumber(averagePlayers));
- builder.Append(SEPERATOR);
- builder.Append(FormatNumber(medianPlayers));
- builder.Append(SEPERATOR);
- builder.Append(FormatNumber(averageEarnings));
- builder.Append(SEPERATOR);
- builder.Append(FormatNumber(medianEarnings));
- builder.Append(SEPERATOR);
- builder.Append(FormatNumber(medianOwnersPerScore));
- builder.Append(SEPERATOR);
- builder.Append(FormatNumber(medianEarningsPerScore));
- builder.Append(SEPERATOR);
- builder.Append(FormatNumber(percentAbove5k));
- builder.Append(SEPERATOR);
- builder.Append(FormatNumber(percentAbove10k));
- builder.Append(SEPERATOR);
- builder.Append(FormatNumber(percentAbove20k));
- builder.Append(SEPERATOR);
- builder.Append(FormatNumber(percentAbove30k));
- builder.Append(SEPERATOR);
- builder.Append(FormatNumber(percentAbove50k));
- builder.Append(SEPERATOR);
- builder.Append(FormatNumber(percentAbove100k));
- builder.Append(SEPERATOR);
- builder.Append(FormatNumber(percentAbove250k));
- builder.Append(SEPERATOR);
- builder.Append(FormatNumber(percentAbove500k));
- builder.Append(SEPERATOR);
- builder.Append(FormatNumber(percentAbove1m));
- builder.Append(SEPERATOR);
- builder.Append('"');
- for (int i = 0; i < numGames; i++)
- {
- builder.Append(games[i].name);
- if (i < numGames - 1)
- {
- builder.Append(", ");
- }
- }
- builder.Append('"');
- return builder.ToString();
- }
- public static string GetHeaderLine()
- {
- StringBuilder builder = new StringBuilder();
- builder.Append("tag");
- builder.Append(SEPERATOR);
- builder.Append("numGames");
- builder.Append(SEPERATOR);
- builder.Append("averagePrice");
- builder.Append(SEPERATOR);
- builder.Append("medianPrice");
- builder.Append(SEPERATOR);
- builder.Append("averageScore");
- builder.Append(SEPERATOR);
- builder.Append("medianScore");
- builder.Append(SEPERATOR);
- builder.Append("averageOwners");
- builder.Append(SEPERATOR);
- builder.Append("medianOwners");
- builder.Append(SEPERATOR);
- builder.Append("averagePlayers");
- builder.Append(SEPERATOR);
- builder.Append("medianPlayers");
- builder.Append(SEPERATOR);
- builder.Append("averageEarnings");
- builder.Append(SEPERATOR);
- builder.Append("medianEarnings");
- builder.Append(SEPERATOR);
- builder.Append("medianOwnersPerScore");
- builder.Append(SEPERATOR);
- builder.Append("medianEarningsPerScore");
- builder.Append(SEPERATOR);
- builder.Append("5k");
- builder.Append(SEPERATOR);
- builder.Append("10k");
- builder.Append(SEPERATOR);
- builder.Append("20k");
- builder.Append(SEPERATOR);
- builder.Append("30k");
- builder.Append(SEPERATOR);
- builder.Append("50k");
- builder.Append(SEPERATOR);
- builder.Append("100k");
- builder.Append(SEPERATOR);
- builder.Append("250k");
- builder.Append(SEPERATOR);
- builder.Append("500k");
- builder.Append(SEPERATOR);
- builder.Append("1m");
- builder.Append(SEPERATOR);
- builder.Append("games");
- return builder.ToString();
- }
- static string FormatNumber(float number)
- {
- return Math.Round(number).ToString();
- }
- public static float GetAverage(float[] sourceNumbers)
- {
- float total = 0;
- for (int i = 0; i < sourceNumbers.Length; i++)
- {
- total += sourceNumbers[i];
- }
- return total / sourceNumbers.Length;
- }
- public static float GetMedian(float[] sourceNumbers)
- {
- float[] sortedPNumbers = (float[])sourceNumbers.Clone();
- Array.Sort(sortedPNumbers);
- int size = sortedPNumbers.Length;
- int mid = size / 2;
- float median = (size % 2 != 0) ? (float)sortedPNumbers[mid] : ((float)sortedPNumbers[mid] + (float)sortedPNumbers[mid - 1]) / 2;
- return median;
- }
- }
- public class Program
- {
- public static Dictionary<string, List<Game>> gamesByTag = new Dictionary<string, List<Game>>();
- public static Game[] games;
- static void Main(string[] args)
- {
- ScrapeGames();
- WriteCSV("steamspy.csv");
- Console.WriteLine("Done");
- Console.ReadLine();
- }
- static void WriteCSV(string filename)
- {
- Console.WriteLine("Aggregating tag stats");
- StringBuilder builder = new StringBuilder();
- // builder.AppendLine("sep=;");
- builder.AppendLine(TagData.GetHeaderLine());
- foreach (KeyValuePair<string, List<Game>> kvp in gamesByTag)
- {
- TagData data = new TagData(kvp.Key, kvp.Value);
- if (data.numGames >= 10)
- {
- builder.AppendLine(data.GetCSVString());
- }
- }
- string path = Directory.GetCurrentDirectory() + "\\" + filename;
- Console.WriteLine("Writing file: " + path);
- if (File.Exists(path))
- {
- File.Delete(path);
- }
- StreamWriter file = new StreamWriter(path);
- file.WriteLine(builder.ToString());
- file.Close();
- }
- static void ScrapeGames()
- {
- ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate;
- ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
- string jsonString = WebRequest("http://steamspy.com/api.php?request=all");
- // string jsonString = WebRequest("http://steamspy.com/api.php?request=appdetails&appid=730");
- Console.WriteLine("Parsing response");
- JSONObject json = new JSONObject(jsonString);
- games = new Game[json.FieldCount];
- Console.WriteLine("Parsing JSON objects");
- for (int i = 0; i < json.FieldCount; i++)
- {
- games[i] = new Game((JSONObject)json.Fields[i].GetValue());
- for (int j = 0; j < games[i].tags.Length; j++)
- {
- string tag = games[i].tags[j];
- List<Game> tagGames;
- if (gamesByTag.TryGetValue(tag, out tagGames))
- {
- tagGames.Add(games[i]);
- }
- else
- {
- tagGames = new List<Game>();
- tagGames.Add(games[i]);
- gamesByTag.Add(tag, tagGames);
- }
- }
- }
- }
- static string WebRequest(string url)
- {
- try
- {
- Console.WriteLine("Requesting: " + url);
- Stopwatch stopwatch = new Stopwatch();
- stopwatch.Start();
- using (WebClient webClient = new WebClient())
- {
- webClient.Headers.Add("User-Agent: Other");
- Stream stream = webClient.OpenRead(new Uri(url));
- using (StreamReader reader = new StreamReader(stream))
- {
- string result = reader.ReadToEnd();
- stopwatch.Stop();
- Console.WriteLine("Request succeeded: " + stopwatch.Elapsed.Milliseconds + "ms");
- return result;
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex);
- }
- return null;
- }
- private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
- {
- if (error == SslPolicyErrors.None) return true;
- Console.WriteLine("X509Certificate [{0}] Policy Error: '{1}'", cert.Subject, error);
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement