/FFN/FFN.cs

https://github.com/marcneuwirth/FantasyFootballNerdAPI.NET · C# · 195 lines · 92 code · 35 blank · 68 comment · 3 complexity · 2685b33167ad5591f3d0ffcc94cff205 MD5 · raw file

  1. using System;
  2. using System.Data;
  3. using System.Net;
  4. namespace FFN
  5. {
  6. ///<summary>
  7. /// A simple C# class to retrieve data from FantasyFootballNerd.com (FFN)
  8. /// Class is available free of charge, however you will need an API Key to return results.
  9. /// The class is intended to retrieve data from FFN, but please store the data locally to remain
  10. /// bandwidth-friendly.
  11. ///</summary>
  12. ///<author>Marc Neuwirth <marc.neuwirth@gmail.com></author>
  13. ///<version>1.0 2010-09-5</version>
  14. public class FFN
  15. {
  16. private string _ApiKey;
  17. /// <summary>
  18. /// Fantasy Football Nerd API URL
  19. /// </summary>
  20. private string _FFN = "http://api.fantasyfootballnerd.com/";
  21. /// <summary>
  22. /// API XML pages
  23. /// </summary>
  24. private string _Schedule = "ffnScheduleXML.php";
  25. private string _Players = "ffnPlayersXML.php";
  26. private string _PlayerDetails = "ffnPlayerDetailsXML.php";
  27. private string _Rankings = "ffnRankingsXML.php";
  28. private string _Injuries = "ffnInjuriesXML.php";
  29. private string _SitStart = "ffnSitStartXML.php";
  30. /// <summary>
  31. /// Your API Key given to you by registering at FantasyFootballNerd.com
  32. /// </summary>
  33. public string apiKey { get { return _ApiKey; } }
  34. /// <summary>
  35. /// Error Message - a holder for any error that we may encounter
  36. /// </summary>
  37. public string ErrorMessage;
  38. /// <summary>
  39. /// Constructor
  40. /// </summary>
  41. /// <param name="ApiKey">Your ApiKey</param>
  42. public FFN(string ApiKey)
  43. {
  44. this._ApiKey = ApiKey;
  45. }
  46. /// <summary>
  47. /// Helper class to get FFN Xml from FFN
  48. /// </summary>
  49. /// <param name="request">Http Request of the Xml</param>
  50. /// <returns>Requested Xml Page</returns>
  51. private DataSet GetDataSet(HttpWebRequest request)
  52. {
  53. using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
  54. {
  55. // Load data into a dataset
  56. DataSet xml = new DataSet();
  57. xml.ReadXml(response.GetResponseStream());
  58. return xml;
  59. }
  60. }
  61. /// <summary>
  62. /// Returns the Schedule DataTable, the Season and the Timezone
  63. /// </summary>
  64. /// <param name="Season">The current season</param>
  65. /// <param name="Timezone">The timezone</param>
  66. /// <returns>The schedule datatable returned from FFN API</returns>
  67. public DataTable GetSchedule(out string Season, out string Timezone)
  68. {
  69. HttpWebRequest request = WebRequest.Create(String.Format("{0}{1}?apiKey={2}",
  70. _FFN, _Schedule, _ApiKey)) as HttpWebRequest;
  71. DataSet xml = GetDataSet(request);
  72. Season = xml.Tables["Schedule"].Rows[0]["Season"].ToString();
  73. Timezone = xml.Tables["Schedule"].Rows[0]["Timezone"].ToString();
  74. return xml.Tables["Game"];
  75. }
  76. /// <summary>
  77. /// Returns the a DataTable with all of the NFL Players. This does not need to be called
  78. /// more than once per week as it doesn't change with much frequency.
  79. /// </summary>
  80. /// <returns>The Players datatable returned from FFN API</returns>
  81. public DataTable GetPlayers()
  82. {
  83. HttpWebRequest request = WebRequest.Create(String.Format("{0}{1}?apiKey={2}",
  84. _FFN, _Players, _ApiKey)) as HttpWebRequest;
  85. DataSet xml = GetDataSet(request);
  86. return xml.Tables["Player"];
  87. }
  88. /// <summary>
  89. /// Get the Player Details of the requested Player
  90. /// </summary>
  91. /// <param name="PlayerId">The Player Id of the requested Player</param>
  92. /// <param name="FirstName">The first name of the requested player</param>
  93. /// <param name="LastName">The last name of the requested player</param>
  94. /// <param name="Team">The team of the requested player</param>
  95. /// <param name="Position">The position of the requested player</param>
  96. /// <returns>The DataTable of Player Details</returns>
  97. public DataTable GetPlayerDetails(string PlayerId, out string FirstName, out string LastName,
  98. out string Team, out string Position)
  99. {
  100. HttpWebRequest request = WebRequest.Create(String.Format("{0}{1}?apiKey={2}&playerId={3}",
  101. _FFN, _PlayerDetails, _ApiKey, PlayerId)) as HttpWebRequest;
  102. DataSet xml = GetDataSet(request);
  103. FirstName = xml.Tables["PlayerDetails"].Rows[0]["FirstName"].ToString();
  104. LastName = xml.Tables["PlayerDetails"].Rows[0]["LastName"].ToString();
  105. Team = xml.Tables["PlayerDetails"].Rows[0]["Team"].ToString();
  106. Position = xml.Tables["PlayerDetails"].Rows[0]["Position"].ToString();
  107. return xml.Tables["Article"];
  108. }
  109. /// <summary>
  110. /// Get the current preseason draft rankings
  111. /// </summary>
  112. /// <param name="Position">The position to retrieve. Options: ALL, QB, RB, WR, TE, DEF, K</param>
  113. /// <param name="Limit">How many results to return. Pass an integer between 1 and 1000</param>
  114. /// <param name="SOS">Return the Strength of Schedule for every player? Pass a 1 for yes, 0 for no</param>
  115. /// <returns>The DataTable for the current Draft Rankings</returns>
  116. public DataTable GetDraftRankings(string Position, string Limit, string SOS)
  117. {
  118. HttpWebRequest request = WebRequest.Create(
  119. String.Format("{0}{1}?apiKey={2}&position={3}&limit={4}&sos={5}",
  120. _FFN, _Rankings, _ApiKey,Position,Limit,SOS)) as HttpWebRequest;
  121. DataSet xml = GetDataSet(request);
  122. return xml.Tables["Player"];
  123. }
  124. /// <summary>
  125. /// Get the current injury list
  126. /// </summary>
  127. /// <param name="Week">The week number to retrieve injuries for (1-17)</param>
  128. /// <returns>Returns the Injuries for the selected week</returns>
  129. public DataTable GetInjuries(string Week)
  130. {
  131. HttpWebRequest request = WebRequest.Create(String.Format("{0}{1}?apiKey={2}&week={3}",
  132. _FFN, _Injuries, _ApiKey, Week)) as HttpWebRequest;
  133. DataSet xml = GetDataSet(request);
  134. return xml.Tables["Injuries"];
  135. }
  136. /// <summary>
  137. /// Gets the selected week's projections
  138. /// </summary>
  139. /// <param name="Week">The week to return results for (1-17)</param>
  140. /// <param name="Position">The position to retrieve. Options: QB, RB, WR, TE, DEF, K</param>
  141. /// <returns>The DataTable for the selected week's projections</returns>
  142. public DataTable GetSitStart(string Week, string Position)
  143. {
  144. HttpWebRequest request = WebRequest.Create(String.Format("{0}{1}?apiKey={2}&week={3}&position={4}",
  145. _FFN, _SitStart, _ApiKey, Week, Position)) as HttpWebRequest;
  146. DataSet xml = GetDataSet(request);
  147. DataTable Players = xml.Tables["Player"];
  148. if (Players != null)
  149. {
  150. Players.Columns.Add("PPR_ProjectedPoints");
  151. DataTable Projections = xml.Tables["Projections"];
  152. for (int i = 0; i < Players.Rows.Count; i++)
  153. {
  154. Players.Rows[i]["PPR_ProjectedPoints"] = Projections.Rows[i]["PPR"];
  155. }
  156. }
  157. return Players;
  158. }
  159. }
  160. }