Advertisement
Guest User

Untitled

a guest
Jun 1st, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 18.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Net;
  6. using System.Net.Security;
  7. using System.Security.Cryptography.X509Certificates;
  8. using System.Text;
  9.  
  10.  
  11. /*
  12. {  
  13.    "appid":730,
  14.    "name":"Counter-Strike: Global Offensive",
  15.    "developer":"Valve",
  16.    "publisher":"Valve",
  17.    "score_rank":79,
  18.    "owners":28974829,
  19.    "owners_variance":149540,
  20.    "players_forever":28328069,
  21.    "players_forever_variance":147989,
  22.    "players_2weeks":9354209,
  23.    "players_2weeks_variance":87157,
  24.    "average_forever":17644,
  25.    "average_2weeks":831,
  26.    "median_forever":5058,
  27.    "median_2weeks":390,
  28.    "ccu":650895,
  29.    "price":"1499",
  30.    "tags":{  
  31.       "FPS":16943,
  32.       "Multiplayer":13624,
  33.       "Shooter":12713,
  34.       "Action":10799,
  35.       "Team-Based":10285,
  36.       "Competitive":9584,
  37.       "Tactical":8465,
  38.       "First-Person":7256,
  39.       "e-sports":6662,
  40.       "PvP":6330,
  41.       "Online Co-Op":5654,
  42.       "Military":4564,
  43.       "Co-op":4393,
  44.       "Strategy":4376,
  45.       "War":4312,
  46.       "Trading":3164,
  47.       "Realistic":3160,
  48.       "Difficult":3127,
  49.       "Fast-Paced":3061,
  50.       "Moddable":2471
  51.    }
  52. }
  53.  * */
  54.  
  55. public class Game
  56. {
  57.  
  58.     public int appid;
  59.     public string name;
  60.     public string developer;
  61.     public string publisher;
  62.     public int score_rank;
  63.     public int owners;
  64.     public int owners_variance;
  65.     public int players_forever;
  66.     public int players_forever_variance;
  67.     public int players_2weeks;
  68.     public int players_2weeks_variance;
  69.     public int average_forever;
  70.     public int average_2weeks;
  71.     public int median_forever;
  72.     public int median_2weeks;
  73.     public int ccu;
  74.     public float price;
  75.     public string[] tags;
  76.     public int earnings;
  77.     public bool isIndie;
  78.  
  79.     public Game(JSONObject json)
  80.     {
  81.         appid = ParseInt(json.Fields[0]);
  82.         name = (string)json.Fields[1].GetValue();
  83.         developer = (string)json.Fields[2].GetValue();
  84.         publisher = (string)json.Fields[3].GetValue();
  85.         score_rank = ParseInt(json.Fields[4]);
  86.         owners = ParseInt(json.Fields[5]);
  87.         owners_variance = ParseInt(json.Fields[6]);
  88.         players_forever = ParseInt(json.Fields[7]);
  89.         players_forever_variance = ParseInt(json.Fields[8]);
  90.         players_2weeks = ParseInt(json.Fields[9]);
  91.         players_2weeks_variance = ParseInt(json.Fields[10]);
  92.         average_forever = ParseInt(json.Fields[11]);
  93.         average_2weeks = ParseInt(json.Fields[12]);
  94.         median_forever = ParseInt(json.Fields[13]);
  95.         median_2weeks = ParseInt(json.Fields[14]);
  96.         ccu = ParseInt(json.Fields[15]);
  97.  
  98.         string priceString = (string)json.Fields[16].GetValue();
  99.  
  100.         if (priceString == null)
  101.         {
  102.             price = 0;
  103.         }
  104.         else
  105.         {
  106.             price = float.Parse(priceString) / 100f;
  107.         }
  108.  
  109.         earnings = (int)(price * owners);
  110.         isIndie = false;
  111.  
  112.         JSONArrayField tagArrayField = json.Fields[17] as JSONArrayField;
  113.         if (tagArrayField != null)
  114.         {
  115.             tags = new string[0];
  116.         }
  117.         else
  118.         {
  119.             JSONObject tagObject = (JSONObject)json.Fields[17].GetValue();
  120.             tags = new string[tagObject.FieldCount];
  121.  
  122.             for (int i = 0; i < tags.Length; i++)
  123.             {
  124.                 tags[i] = tagObject.Fields[i].Name;
  125.                 if (tags[i] == "Indie")
  126.                 {
  127.                     isIndie = true;
  128.                 }
  129.             }
  130.         }
  131.     }
  132.  
  133.     int ParseInt(JSONField field)
  134.     {
  135.         JSONIntField intField = field as JSONIntField;
  136.         if (intField != null)
  137.         {
  138.             return intField.Value;
  139.         }
  140.  
  141.         return -1;
  142.     }
  143.  
  144. }
  145.  
  146. public class TagData
  147. {
  148.     public string name;
  149.     public int numGames;
  150.     public float averagePrice;
  151.     public float medianPrice;
  152.     public float averageScore;
  153.     public float medianScore;
  154.     public float averageOwners;
  155.     public float medianOwners;
  156.     public float averagePlayers;
  157.     public float medianPlayers;
  158.     public float averageEarnings;
  159.     public float medianEarnings;
  160.     public float medianOwnersPerScore;
  161.     public float medianEarningsPerScore;
  162.     public float percentAbove5k;
  163.     public float percentAbove10k;
  164.     public float percentAbove20k;
  165.     public float percentAbove30k;
  166.     public float percentAbove50k;
  167.     public float percentAbove100k;
  168.     public float percentAbove250k;
  169.     public float percentAbove500k;
  170.     public float percentAbove1m;
  171.     public Game[] games;
  172.  
  173.     const char SEPERATOR = ',';
  174.  
  175.     public TagData(string tagName, List<Game> gameList)
  176.     {
  177.         for (int i = 0; i < gameList.Count; i++)
  178.         {
  179.             if (!AllowGame(gameList[i]))
  180.             {
  181.                 gameList.RemoveAt(i);
  182.                 i--;
  183.             }
  184.         }
  185.  
  186.         gameList.Sort((a, b) => b.owners.CompareTo(a.owners));
  187.  
  188.         games = gameList.ToArray();
  189.         name = tagName;
  190.         numGames = gameList.Count;
  191.  
  192.         if (numGames > 0)
  193.         {
  194.  
  195.             float[] allPrices = new float[numGames];
  196.             float[] allScores = new float[numGames];
  197.             float[] allOwners = new float[numGames];
  198.             float[] allPlayers = new float[numGames];
  199.             float[] allEarnings = new float[numGames];
  200.  
  201.             for (int i = 0; i < numGames; i++)
  202.             {
  203.                 allPrices[i] = gameList[i].price;
  204.                 allScores[i] = gameList[i].score_rank;
  205.                 allOwners[i] = gameList[i].owners;
  206.                 allPlayers[i] = gameList[i].players_forever;
  207.                 allEarnings[i] = gameList[i].earnings;
  208.  
  209.                 if (gameList[i].owners > 5000) percentAbove5k++;
  210.                 if (gameList[i].owners > 10000) percentAbove10k++;
  211.                 if (gameList[i].owners > 20000) percentAbove20k++;
  212.                 if (gameList[i].owners > 30000) percentAbove30k++;
  213.                 if (gameList[i].owners > 50000) percentAbove50k++;
  214.                 if (gameList[i].owners > 100000) percentAbove100k++;
  215.                 if (gameList[i].owners > 250000) percentAbove250k++;
  216.                 if (gameList[i].owners > 500000) percentAbove500k++;
  217.                 if (gameList[i].owners > 1000000) percentAbove1m++;
  218.             }
  219.  
  220.             percentAbove5k = (percentAbove5k / numGames) * 100f;
  221.             percentAbove10k = (percentAbove10k / numGames) * 100f;
  222.             percentAbove20k = (percentAbove20k / numGames) * 100f;
  223.             percentAbove30k = (percentAbove30k / numGames) * 100f;
  224.             percentAbove50k = (percentAbove50k / numGames) * 100f;
  225.             percentAbove100k = (percentAbove100k / numGames) * 100f;
  226.             percentAbove250k = (percentAbove250k / numGames) * 100f;
  227.             percentAbove500k = (percentAbove500k / numGames) * 100f;
  228.             percentAbove1m = (percentAbove1m / numGames) * 100f;
  229.  
  230.             averagePrice = GetAverage(allPrices);
  231.             medianPrice = GetMedian(allPrices);
  232.             averageScore = GetAverage(allScores);
  233.             medianScore = GetMedian(allScores);
  234.             averageOwners = GetAverage(allOwners);
  235.             medianOwners = GetMedian(allOwners);
  236.             averagePlayers = GetAverage(allPlayers);
  237.             medianPlayers = GetMedian(allPlayers);
  238.             averageEarnings = GetAverage(allEarnings);
  239.             medianEarnings = GetMedian(allEarnings);
  240.  
  241.             medianOwnersPerScore = medianOwners / medianScore;
  242.             medianEarningsPerScore = medianEarnings / medianScore;
  243.         }
  244.  
  245.     }
  246.  
  247.     bool AllowGame(Game game)
  248.     {
  249.         if (game.price < 9) return false;
  250.         if (game.price > 30) return false;
  251.         if (!game.isIndie) return false;
  252.         if (game.score_rank < 50) return false;
  253.         //   if (game.owners > 1000000) return false;
  254.  
  255.         return true;
  256.     }
  257.  
  258.     public void Log()
  259.     {
  260.         Console.WriteLine("tag: " + name);
  261.         Console.WriteLine("numGames: " + numGames);
  262.         Console.WriteLine("averagePrice: " + averagePrice);
  263.         Console.WriteLine("medianPrice: " + medianPrice);
  264.         Console.WriteLine("averageScore: " + averageScore);
  265.         Console.WriteLine("medianScore: " + medianScore);
  266.         Console.WriteLine("averageOwners: " + averageOwners);
  267.         Console.WriteLine("medianOwners: " + medianOwners);
  268.         Console.WriteLine("averagePlayers: " + averagePlayers);
  269.         Console.WriteLine("medianPlayers: " + medianPlayers);
  270.         Console.WriteLine("averageEarnings: " + averageEarnings);
  271.         Console.WriteLine("medianEarnings: " + medianEarnings);
  272.         Console.WriteLine("medianOwnersPerScore: " + medianOwnersPerScore);
  273.         Console.WriteLine("medianEarningsPerScore: " + medianEarningsPerScore);
  274.         Console.WriteLine("percentAbove5k: " + percentAbove5k);
  275.         Console.WriteLine("percentAbove10k: " + percentAbove10k);
  276.         Console.WriteLine("percentAbove20k: " + percentAbove20k);
  277.         Console.WriteLine("percentAbove30k: " + percentAbove30k);
  278.         Console.WriteLine("percentAbove50k: " + percentAbove50k);
  279.         Console.WriteLine("percentAbove100k: " + percentAbove100k);
  280.         Console.WriteLine("percentAbove250k: " + percentAbove250k);
  281.         Console.WriteLine("percentAbove500k: " + percentAbove500k);
  282.         Console.WriteLine("percentAbove1m: " + percentAbove1m);
  283.     }
  284.  
  285.     public string GetCSVString()
  286.     {
  287.  
  288.  
  289.         StringBuilder builder = new StringBuilder(name);
  290.         builder.Append(SEPERATOR);
  291.         builder.Append(numGames);
  292.         builder.Append(SEPERATOR);
  293.         builder.Append(FormatNumber(averagePrice));
  294.         builder.Append(SEPERATOR);
  295.         builder.Append(FormatNumber(medianPrice));
  296.         builder.Append(SEPERATOR);
  297.         builder.Append(FormatNumber(averageScore));
  298.         builder.Append(SEPERATOR);
  299.         builder.Append(FormatNumber(medianScore));
  300.         builder.Append(SEPERATOR);
  301.         builder.Append(FormatNumber(averageOwners));
  302.         builder.Append(SEPERATOR);
  303.         builder.Append(FormatNumber(medianOwners));
  304.         builder.Append(SEPERATOR);
  305.         builder.Append(FormatNumber(averagePlayers));
  306.         builder.Append(SEPERATOR);
  307.         builder.Append(FormatNumber(medianPlayers));
  308.         builder.Append(SEPERATOR);
  309.         builder.Append(FormatNumber(averageEarnings));
  310.         builder.Append(SEPERATOR);
  311.         builder.Append(FormatNumber(medianEarnings));
  312.         builder.Append(SEPERATOR);
  313.         builder.Append(FormatNumber(medianOwnersPerScore));
  314.         builder.Append(SEPERATOR);
  315.         builder.Append(FormatNumber(medianEarningsPerScore));
  316.         builder.Append(SEPERATOR);
  317.         builder.Append(FormatNumber(percentAbove5k));
  318.         builder.Append(SEPERATOR);
  319.         builder.Append(FormatNumber(percentAbove10k));
  320.         builder.Append(SEPERATOR);
  321.         builder.Append(FormatNumber(percentAbove20k));
  322.         builder.Append(SEPERATOR);
  323.         builder.Append(FormatNumber(percentAbove30k));
  324.         builder.Append(SEPERATOR);
  325.         builder.Append(FormatNumber(percentAbove50k));
  326.         builder.Append(SEPERATOR);
  327.         builder.Append(FormatNumber(percentAbove100k));
  328.         builder.Append(SEPERATOR);
  329.         builder.Append(FormatNumber(percentAbove250k));
  330.         builder.Append(SEPERATOR);
  331.         builder.Append(FormatNumber(percentAbove500k));
  332.         builder.Append(SEPERATOR);
  333.         builder.Append(FormatNumber(percentAbove1m));
  334.         builder.Append(SEPERATOR);
  335.  
  336.         builder.Append('"');
  337.         for (int i = 0; i < numGames; i++)
  338.         {
  339.  
  340.             builder.Append(games[i].name);
  341.  
  342.             if (i < numGames - 1)
  343.             {
  344.                 builder.Append(", ");
  345.             }
  346.         }
  347.         builder.Append('"');
  348.  
  349.         return builder.ToString();
  350.     }
  351.  
  352.     public static string GetHeaderLine()
  353.     {
  354.         StringBuilder builder = new StringBuilder();
  355.  
  356.         builder.Append("tag");
  357.         builder.Append(SEPERATOR);
  358.         builder.Append("numGames");
  359.         builder.Append(SEPERATOR);
  360.         builder.Append("averagePrice");
  361.         builder.Append(SEPERATOR);
  362.         builder.Append("medianPrice");
  363.         builder.Append(SEPERATOR);
  364.         builder.Append("averageScore");
  365.         builder.Append(SEPERATOR);
  366.         builder.Append("medianScore");
  367.         builder.Append(SEPERATOR);
  368.         builder.Append("averageOwners");
  369.         builder.Append(SEPERATOR);
  370.         builder.Append("medianOwners");
  371.         builder.Append(SEPERATOR);
  372.         builder.Append("averagePlayers");
  373.         builder.Append(SEPERATOR);
  374.         builder.Append("medianPlayers");
  375.         builder.Append(SEPERATOR);
  376.         builder.Append("averageEarnings");
  377.         builder.Append(SEPERATOR);
  378.         builder.Append("medianEarnings");
  379.         builder.Append(SEPERATOR);
  380.         builder.Append("medianOwnersPerScore");
  381.         builder.Append(SEPERATOR);
  382.         builder.Append("medianEarningsPerScore");
  383.         builder.Append(SEPERATOR);
  384.         builder.Append("5k");
  385.         builder.Append(SEPERATOR);
  386.         builder.Append("10k");
  387.         builder.Append(SEPERATOR);
  388.         builder.Append("20k");
  389.         builder.Append(SEPERATOR);
  390.         builder.Append("30k");
  391.         builder.Append(SEPERATOR);
  392.         builder.Append("50k");
  393.         builder.Append(SEPERATOR);
  394.         builder.Append("100k");
  395.         builder.Append(SEPERATOR);
  396.         builder.Append("250k");
  397.         builder.Append(SEPERATOR);
  398.         builder.Append("500k");
  399.         builder.Append(SEPERATOR);
  400.         builder.Append("1m");
  401.         builder.Append(SEPERATOR);
  402.         builder.Append("games");
  403.  
  404.         return builder.ToString();
  405.     }
  406.  
  407.     static string FormatNumber(float number)
  408.     {
  409.         return Math.Round(number).ToString();
  410.     }
  411.  
  412.     public static float GetAverage(float[] sourceNumbers)
  413.     {
  414.         float total = 0;
  415.         for (int i = 0; i < sourceNumbers.Length; i++)
  416.         {
  417.             total += sourceNumbers[i];
  418.         }
  419.         return total / sourceNumbers.Length;
  420.     }
  421.  
  422.     public static float GetMedian(float[] sourceNumbers)
  423.     {
  424.  
  425.         float[] sortedPNumbers = (float[])sourceNumbers.Clone();
  426.         Array.Sort(sortedPNumbers);
  427.         int size = sortedPNumbers.Length;
  428.         int mid = size / 2;
  429.         float median = (size % 2 != 0) ? (float)sortedPNumbers[mid] : ((float)sortedPNumbers[mid] + (float)sortedPNumbers[mid - 1]) / 2;
  430.         return median;
  431.     }
  432. }
  433.  
  434. public class Program
  435. {
  436.  
  437.     public static Dictionary<string, List<Game>> gamesByTag = new Dictionary<string, List<Game>>();
  438.     public static Game[] games;
  439.  
  440.     static void Main(string[] args)
  441.     {
  442.         ScrapeGames();
  443.         WriteCSV("steamspy.csv");
  444.         Console.WriteLine("Done");
  445.  
  446.         Console.ReadLine();
  447.     }
  448.  
  449.     static void WriteCSV(string filename)
  450.     {
  451.         Console.WriteLine("Aggregating tag stats");
  452.  
  453.         StringBuilder builder = new StringBuilder();
  454.         //  builder.AppendLine("sep=;");
  455.         builder.AppendLine(TagData.GetHeaderLine());
  456.         foreach (KeyValuePair<string, List<Game>> kvp in gamesByTag)
  457.         {
  458.             TagData data = new TagData(kvp.Key, kvp.Value);
  459.             if (data.numGames >= 10)
  460.             {
  461.                 builder.AppendLine(data.GetCSVString());
  462.             }
  463.         }
  464.  
  465.         string path = Directory.GetCurrentDirectory() + "\\" + filename;
  466.  
  467.         Console.WriteLine("Writing file: " + path);
  468.         if (File.Exists(path))
  469.         {
  470.             File.Delete(path);
  471.         }
  472.         StreamWriter file = new StreamWriter(path);
  473.         file.WriteLine(builder.ToString());
  474.  
  475.         file.Close();
  476.     }
  477.  
  478.  
  479.  
  480.  
  481.  
  482.     static void ScrapeGames()
  483.     {
  484.         ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate;
  485.         ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
  486.  
  487.         string jsonString = WebRequest("http://steamspy.com/api.php?request=all");
  488.         //  string jsonString = WebRequest("http://steamspy.com/api.php?request=appdetails&appid=730");
  489.  
  490.         Console.WriteLine("Parsing response");
  491.         JSONObject json = new JSONObject(jsonString);
  492.         games = new Game[json.FieldCount];
  493.  
  494.         Console.WriteLine("Parsing JSON objects");
  495.         for (int i = 0; i < json.FieldCount; i++)
  496.         {
  497.             games[i] = new Game((JSONObject)json.Fields[i].GetValue());
  498.  
  499.             for (int j = 0; j < games[i].tags.Length; j++)
  500.             {
  501.                 string tag = games[i].tags[j];
  502.                 List<Game> tagGames;
  503.                 if (gamesByTag.TryGetValue(tag, out tagGames))
  504.                 {
  505.                     tagGames.Add(games[i]);
  506.                 }
  507.                 else
  508.                 {
  509.                     tagGames = new List<Game>();
  510.                     tagGames.Add(games[i]);
  511.                     gamesByTag.Add(tag, tagGames);
  512.                 }
  513.             }
  514.         }
  515.     }
  516.  
  517.     static string WebRequest(string url)
  518.     {
  519.         try
  520.         {
  521.             Console.WriteLine("Requesting: " + url);
  522.  
  523.             Stopwatch stopwatch = new Stopwatch();
  524.             stopwatch.Start();
  525.             using (WebClient webClient = new WebClient())
  526.             {
  527.                 webClient.Headers.Add("User-Agent: Other");
  528.  
  529.                 Stream stream = webClient.OpenRead(new Uri(url));
  530.                 using (StreamReader reader = new StreamReader(stream))
  531.                 {
  532.                     string result = reader.ReadToEnd();
  533.                     stopwatch.Stop();
  534.                     Console.WriteLine("Request succeeded: " + stopwatch.Elapsed.Milliseconds + "ms");
  535.                     return result;
  536.                 }
  537.             }
  538.         }
  539.         catch (Exception ex)
  540.         {
  541.             Console.WriteLine(ex);
  542.         }
  543.  
  544.         return null;
  545.     }
  546.  
  547.  
  548.     private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
  549.     {
  550.         if (error == SslPolicyErrors.None) return true;
  551.  
  552.         Console.WriteLine("X509Certificate [{0}] Policy Error: '{1}'", cert.Subject, error);
  553.         return false;
  554.     }
  555.  
  556. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement