Guest User

Eve kill API Json

a guest
May 12th, 2012
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.70 KB | None | 0 0
  1. namespace EveKillboard.Services.EveKillServices
  2. {
  3.     using System;
  4.     using System.Globalization;
  5.     using System.Collections.Generic;
  6.     using System.IO;
  7.     using System.Linq;
  8.     using System.Net;
  9.     using Core.Services;
  10.     using Newtonsoft.Json;
  11.     using Newtonsoft.Json.Converters;
  12.     using System.Diagnostics;
  13.  
  14.     public sealed class KillmailFeedProvider : IKillmailFeedServices
  15.     {
  16.         /// <summary>
  17.         ///
  18.         /// </summary>
  19.         private enum OutputOptionsEnum
  20.         {
  21.             NotDefined = 0,
  22.             /// <summary>
  23.             /// Show the URL to EVE-KILL.
  24.             /// </summary>
  25.             Url = 1,
  26.             /// <summary>
  27.             /// Show the Timestamp.
  28.             /// </summary>
  29.             TimeStamp = 2,
  30.             /// <summary>
  31.             /// Show the internal EVE-KILL kill ID.
  32.             /// </summary>
  33.             InternalId = 4,
  34.             /// <summary>
  35.             /// Show the CCP API kill ID.
  36.             /// </summary>
  37.             CcpId = 8,
  38.             /// <summary>
  39.             /// Show the Victim Name.
  40.             /// </summary>
  41.             VictimName = 16,
  42.             /// <summary>
  43.             /// Show the Victim External ID.
  44.             /// </summary>
  45.             VictimId = 32,
  46.             /// <summary>
  47.             /// Show the Victim Corp Name.
  48.             /// </summary>
  49.             VictimCorporationName = 64,
  50.             /// <summary>
  51.             /// Show the Victim Alliance Name.
  52.             /// </summary>
  53.             VictimAllianceName = 128,
  54.             /// <summary>
  55.             /// Show the Victim Ship Name.
  56.             /// </summary>
  57.             VictimShipName = 256,
  58.             /// <summary>
  59.             /// Show the Victim Ships Class Name.
  60.             /// </summary>
  61.             VictimnShipClassName = 512,
  62.             /// <summary>
  63.             /// Show the Victim Ships External ID.
  64.             /// </summary>
  65.             VictimShipId = 1024,
  66.             /// <summary>
  67.             /// Show the FB Pilots name.
  68.             /// </summary>
  69.             FinalBlowPilotName = 2048,
  70.             /// <summary>
  71.             /// Show the FB Pilots corp name.
  72.             /// </summary>
  73.             FinalBlowPilotCorporationName = 4096,
  74.             /// <summary>
  75.             /// Show the FB Pilots alliance name.
  76.             /// </summary>
  77.             FinalBlowPilotAllianceName = 8192,
  78.             /// <summary>
  79.             /// Show the count of involved pilots.
  80.             /// </summary>
  81.             InvolvedCount = 16384,
  82.             /// <summary>
  83.             /// Show the Solar System the kill happened in.
  84.             /// </summary>
  85.             System = 32768,
  86.             /// <summary>
  87.             /// Show the Solar System true security.
  88.             /// </summary>
  89.             SystemSecurity = 65536,
  90.             /// <summary>
  91.             /// Show the Regions name.
  92.             /// </summary>
  93.             RegionName = 131072,
  94.             /// <summary>
  95.             /// Show the ISK value of the kill (total loss (ship + modules)).
  96.             /// </summary>
  97.             TotalValue = 262144,
  98.             /// <summary>
  99.             /// Show the pilots involved.
  100.             /// </summary>
  101.             InvolvedPilots = 524288,
  102.             /// <summary>
  103.             /// Show the items that was destroyed and dropped.
  104.             /// </summary>
  105.             Items = 1048576,
  106.             /// <summary>
  107.             /// Default output option
  108.             /// </summary>
  109.             Default = TimeStamp | InternalId | CcpId | VictimName | VictimId | VictimCorporationName | VictimAllianceName | VictimShipId | System | InvolvedPilots | Items
  110.         }
  111.  
  112.        
  113.  
  114.         private JsonSerializer _serializer = null;
  115.  
  116.         public KillmailFeedProvider()
  117.         {
  118.             _serializer = JsonSerializer.Create(new JsonSerializerSettings() { DefaultValueHandling = DefaultValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore });
  119.             _serializer.Converters.Add(new FeedKillmailConverter());
  120.             _serializer.Converters.Add(new FeedKillmailInvolvedConverter());
  121.             _serializer.Converters.Add(new FeedKillmailInvolvedListConverter());
  122.             _serializer.Converters.Add(new DroppedKillmailItemListConverter());
  123.             _serializer.Converters.Add(new DestroyedKillmailItemListConverter());
  124.         }
  125.  
  126.         ~KillmailFeedProvider()
  127.         {
  128.             _serializer = null;
  129.         }
  130.  
  131.         public void Dispose()
  132.         {
  133.             GC.SuppressFinalize(this);
  134.             _serializer = null;
  135.         }
  136.  
  137.         public IEnumerable<IFeedKillmail> GetKillmails(int[] allianceIds)
  138.         {
  139.             var result = new List<IFeedKillmail>();
  140.             try
  141.             {
  142.                 var week = CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(DateTime.UtcNow.Date, CultureInfo.InvariantCulture.DateTimeFormat.CalendarWeekRule, CultureInfo.InvariantCulture.DateTimeFormat.FirstDayOfWeek);
  143.  
  144.                 foreach (var allianceId in allianceIds)
  145.                 {
  146.                    
  147.                     var uri = Uri.EscapeUriString(string.Format("http://eve-kill.net/epic/combinedAllianceExtID:{1}/mask:{0}", (Int32)OutputOptionsEnum.Default, allianceId,week));
  148.  
  149.                     var request = (HttpWebRequest)WebRequest.Create(uri);
  150.                     request.KeepAlive = false;
  151.                     request.Timeout = 10000;
  152.  
  153.                     var response = request.GetResponse();
  154.                     var st = response.GetResponseStream();
  155.                     if(st ==null) continue;
  156.  
  157.                     string content = null;
  158.                     using (st)
  159.                     using (var reader = new StreamReader(st))
  160.                         content = reader.ReadToEnd();
  161.  
  162.                     using (var jreader = new JsonTextReader(new StringReader(content)))
  163.                     {
  164.                         var kills = _serializer.Deserialize(jreader, typeof (List<IFeedKillmail>)) as List<IFeedKillmail>;
  165.                         if (kills != null && kills.Any())
  166.                             result.AddRange(kills);
  167.                     }
  168.                 }
  169.                
  170.             }
  171.             catch (Exception)
  172.             {
  173.             }
  174.             ClearNames(result);
  175.             return result;
  176.         }
  177.  
  178.         private static void ClearNames(IEnumerable<IFeedKillmail> kills)
  179.         {
  180.             foreach (var kill in kills)
  181.             {
  182.                 kill.VictimAllianceName = GetClearName(kill.VictimAllianceName);
  183.                 kill.VictimFactionName = GetClearName(kill.VictimFactionName);
  184.  
  185.                 foreach (var involved in kill.Involved)
  186.                 {
  187.                     involved.AllianceName = GetClearName(involved.AllianceName);
  188.                     involved.FactionName = GetClearName(involved.FactionName);
  189.                 }
  190.             }
  191.         }
  192.  
  193.         private static string GetClearName(string name)
  194.         {
  195.             var temp = string.IsNullOrEmpty(name)? null : name.Replace("None", null).Replace("none", null).Replace("Unknown", null).Replace("unknown",null).Trim();
  196.             return string.IsNullOrEmpty(temp) ? null : temp;
  197.         }
  198.  
  199.         private class FeedKillmailConverter : CustomCreationConverter<IFeedKillmail>
  200.         {
  201.             public override IFeedKillmail Create(Type objectType)
  202.             {
  203.                 return new FeedKillmail();
  204.             }
  205.         }
  206.  
  207.         private class FeedKillmailInvolvedConverter : CustomCreationConverter<IFeedKillmailInvolved>
  208.         {
  209.             public override IFeedKillmailInvolved Create(Type objectType)
  210.             {
  211.                 return new FeedKillmailInvolved();
  212.             }
  213.         }
  214.         private class FeedKillmailInvolvedListConverter : CustomCreationConverter<IEnumerable<IFeedKillmailInvolved>>
  215.         {
  216.             public override IEnumerable<IFeedKillmailInvolved> Create(Type objectType)
  217.             {
  218.                 return new List<FeedKillmailInvolved>();
  219.             }
  220.         }
  221.         private class DroppedKillmailItemListConverter : CustomCreationConverter<IEnumerable<DroppedKillmailItem>>
  222.         {
  223.             public override IEnumerable<DroppedKillmailItem> Create(Type objectType)
  224.             {
  225.                 return new List<DroppedKillmailItem>();
  226.             }
  227.         }
  228.         private class DestroyedKillmailItemListConverter : CustomCreationConverter<IEnumerable<DestroyedKillmailItem>>
  229.         {
  230.             public override IEnumerable<DestroyedKillmailItem> Create(Type objectType)
  231.             {
  232.                 return new List<DestroyedKillmailItem>();
  233.             }
  234.         }
  235.  
  236.  
  237.         [JsonObjectAttribute(MemberSerialization.OptIn)]
  238.         internal class FeedKillmail : IFeedKillmail
  239.         {
  240.             [JsonProperty(PropertyName = "timestamp")]
  241.             public DateTime Date { get; set; }
  242.             [JsonProperty(PropertyName = "internalID")]
  243.             public int InternalId { get; set; }
  244.             [JsonProperty(PropertyName = "externalID")]
  245.             public int CcpId { get; set; }
  246.             [JsonProperty(PropertyName = "victimExternalID")]
  247.             public int VictimId { get; set; }
  248.             [JsonProperty(PropertyName = "victimName")]
  249.             public string VictimName { get; set; }
  250.             [JsonProperty(PropertyName = "victimCorpName")]
  251.             public string VictimCorporationName { get; set; }
  252.             [JsonIgnore]
  253.             public int VictimCorporationId { get; set; }
  254.             [JsonProperty(PropertyName = "victimAllianceName")]
  255.             public string VictimAllianceName { get; set; }
  256.             [JsonIgnore]
  257.             public int VictimAllianceId { get; set; }
  258.             [JsonProperty(PropertyName = "victimFactionName")]
  259.             public string VictimFactionName { get; set; }
  260.             [JsonIgnore]
  261.             public int VictimFactionId { get; set; }
  262.             [JsonProperty(PropertyName = "victimShipID")]
  263.             public int VictimShipId { get; set; }
  264.             [JsonIgnore]
  265.             public string VictimShipName { get; set; }
  266.             [JsonProperty(PropertyName = "solarSystemName")]
  267.             public string SystemName { get; set; }
  268.             [JsonIgnore]
  269.             public int SystemId { get; set; }
  270.             [JsonProperty(PropertyName = "items")]
  271.             public KillmailItems KillItems { get; set; }
  272.             [JsonProperty(PropertyName = "involved")]
  273.             public IEnumerable<IFeedKillmailInvolved> Involved { get; set; }
  274.             [JsonIgnore]
  275.             public IEnumerable<IFeedKillmailItem> Items
  276.             {
  277.                 get
  278.                 {
  279.                     var items = new List<FeedKillmailItem>();
  280.                     if (KillItems == null) return items;
  281.  
  282.                     items.AddRange(
  283.                         KillItems.Destroyed.Select(
  284.                             i =>
  285.                             new FeedKillmailItem()
  286.                                 {
  287.                                     ItemId = i.ItemId,
  288.                                     Quantity = i.Quantity,
  289.                                     IsDropped = false,
  290.                                     IsDestroyed = true,
  291.                                     IsCargo = i.Flag == 5,
  292.                                     IsDroneBay = i.Flag == 87,
  293.                                     IsImplant = i.Flag == 89,
  294.                                     IsCopy = false
  295.                                 }));
  296.  
  297.                     items.AddRange(
  298.                         KillItems.Dropped.Select(
  299.                             i =>
  300.                             new FeedKillmailItem()
  301.                                 {
  302.                                     ItemId = i.ItemId,
  303.                                     Quantity = i.Quantity,
  304.                                     IsDropped = true,
  305.                                     IsDestroyed = false,
  306.                                     IsCargo = i.Flag == 5,
  307.                                     IsDroneBay = i.Flag == 87,
  308.                                     IsImplant = i.Flag == 89,
  309.                                     IsCopy = false
  310.                                 }));
  311.  
  312.                     return items;
  313.                 }
  314.                 set { }
  315.             }
  316.             [JsonIgnore]
  317.             public int DamageReceived
  318.             {
  319.                 get { return Involved.Sum(i => i.DamageDone); }
  320.                 set { }
  321.             }
  322.         }
  323.  
  324.         [JsonObjectAttribute(MemberSerialization.OptIn)]
  325.         internal class KillmailItems
  326.         {
  327.             [JsonProperty("destroyed")]
  328.             public IEnumerable<DestroyedKillmailItem> Destroyed { get; set; }
  329.             [JsonProperty("dropped")]
  330.             public IEnumerable<DroppedKillmailItem> Dropped { get; set; }
  331.         }
  332.  
  333.         [JsonObjectAttribute(MemberSerialization.OptIn)]
  334.         internal class DestroyedKillmailItem
  335.         {
  336.             [JsonProperty("typeID")]
  337.             public int ItemId { get; set; }
  338.             [JsonProperty("flag")]
  339.             public int Flag { get; set; }
  340.             [JsonProperty("qtyDestroyed")]
  341.             public int Quantity { get; set; }
  342.         }
  343.         [JsonObjectAttribute(MemberSerialization.OptIn)]
  344.         internal class DroppedKillmailItem
  345.         {
  346.             [JsonProperty("typeID")]
  347.             public int ItemId { get; set; }
  348.             [JsonProperty("flag")]
  349.             public int Flag { get; set; }
  350.             [JsonProperty("qtyDropped")]
  351.             public int Quantity { get; set; }
  352.         }
  353.  
  354.         [JsonObjectAttribute(MemberSerialization.OptIn)]
  355.         internal class FeedKillmailInvolved : IFeedKillmailInvolved
  356.         {
  357.             [JsonProperty(PropertyName = "characterID")]
  358.             public int CharacterId { get; set; }
  359.             [JsonProperty(PropertyName = "characterName")]
  360.             public string CharacterName { get; set; }
  361.             [JsonProperty(PropertyName = "corporationID")]
  362.             public int CorporationId { get; set; }
  363.             [JsonProperty(PropertyName = "corporationName")]
  364.             public string CorporationName { get; set; }
  365.             [JsonProperty(PropertyName = "allianceID")]
  366.             public int AllianceId { get; set; }
  367.             [JsonProperty(PropertyName = "allianceName")]
  368.             public string AllianceName { get; set; }
  369.             [JsonProperty(PropertyName = "factionID")]
  370.             public int FactionId { get; set; }
  371.             [JsonProperty(PropertyName = "factionName")]
  372.             public string FactionName { get; set; }
  373.             [JsonProperty(PropertyName = "securityStatus")]
  374.             public float SecurityStatus { get; set; }
  375.             [JsonProperty(PropertyName = "damageDone")]
  376.             public int DamageDone { get; set; }
  377.             [JsonProperty(PropertyName = "finalBlow")]
  378.             public bool IsFinalBlow { get; set; }
  379.             [JsonProperty(PropertyName = "shipTypeID")]
  380.             public int ShipId { get; set; }
  381.             [JsonIgnore]
  382.             public string ShipName { get; set; }
  383.             [JsonProperty(PropertyName = "weaponTypeID")]
  384.             public int WeaponId { get; set; }
  385.             [JsonIgnore]
  386.             public string WeaponName { get; set; }
  387.         }
  388.         internal class FeedKillmailItem : IFeedKillmailItem
  389.         {
  390.             public int ItemId { get; set; }
  391.             public string ItemName { get; set; }
  392.             public int Quantity { get; set; }
  393.             public bool IsDestroyed { get; set; }
  394.             public bool IsDropped { get; set; }
  395.             public bool IsCargo { get; set; }
  396.             public bool IsDroneBay { get; set; }
  397.             public bool IsImplant { get; set; }
  398.             public bool IsCopy { get; set; }
  399.         }
  400.     }
  401. }
Advertisement
Add Comment
Please, Sign In to add comment