PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/SmushMySite.Logic/ReleaseInfo.cs

https://github.com/deanhume/SmushMySite
C# | 128 lines | 95 code | 32 blank | 1 comment | 26 complexity | c5ae209a0c6e23216dc7e9de98e29a08 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Xml.Linq;
  8. namespace SmushMySite.Logic
  9. {
  10. public class ReleaseInfo
  11. {
  12. public Version Version { get; set; }
  13. public string Url { get; set; }
  14. public ReleaseStatus Status { get; set; }
  15. }
  16. public class ReleaseInfoCollection : List<ReleaseInfo>, IComparer<ReleaseInfo>
  17. {
  18. public ReleaseInfoCollection()
  19. : base()
  20. {
  21. }
  22. public ReleaseInfoCollection(IEnumerable<ReleaseInfo> collection)
  23. : base(collection)
  24. {
  25. }
  26. public static ReleaseInfoCollection FromRss(byte[] bytes)
  27. {
  28. using (MemoryStream stream = new MemoryStream(bytes))
  29. {
  30. return FromRss(stream);
  31. }
  32. }
  33. public static ReleaseInfoCollection FromRss(MemoryStream stream)
  34. {
  35. ReleaseInfoCollection releases = new ReleaseInfoCollection();
  36. // regex to match a valid release version
  37. Regex regex = new Regex(@"\d+.\d+.\d+");
  38. XDocument document = XDocument.Load(stream);
  39. foreach (var item in document.Element("rss").Element("channel").Descendants("item"))
  40. {
  41. string title = item.Element("title").Value;
  42. Match match = regex.Match(title);
  43. if (!match.Success)
  44. continue;
  45. string titleLower = title.ToLower();
  46. if (titleLower.Contains("deleted") || titleLower.Contains("removed"))
  47. continue;
  48. ReleaseStatus status = ReleaseStatus.Stable;
  49. if (titleLower.Contains("alpha"))
  50. status = ReleaseStatus.Alpha;
  51. else if (titleLower.Contains("beta"))
  52. status = ReleaseStatus.Beta;
  53. Version version = new Version(match.Groups[0].Value);
  54. if (releases.Exists(r => r.Version == version))
  55. continue;
  56. string link = item.Element("link").Value;
  57. ReleaseInfo release = new ReleaseInfo()
  58. {
  59. Status = status,
  60. Url = link,
  61. Version = version,
  62. };
  63. releases.Add(release);
  64. }
  65. return releases;
  66. }
  67. public ReleaseInfo GetLatest(ReleaseStatus minimumStatus)
  68. {
  69. if (this.Count < 1)
  70. return null;
  71. var filteredReleases = this.Where(r => ((int)r.Status) >= ((int)minimumStatus)).ToList();
  72. if (filteredReleases.Count < 1)
  73. return null;
  74. filteredReleases.Sort(this);
  75. return filteredReleases[filteredReleases.Count - 1];
  76. }
  77. #region IComparer<ReleaseInfo> Members
  78. public int Compare(ReleaseInfo x, ReleaseInfo y)
  79. {
  80. if (x == null && y == null)
  81. return 0;
  82. if (x == null)
  83. return -1;
  84. if (y == null)
  85. return 1;
  86. if (x.Version == null && y.Version == null)
  87. return 0;
  88. if (x.Version == null)
  89. return -1;
  90. if (y.Version == null)
  91. return 1;
  92. return x.Version.CompareTo(y.Version);
  93. }
  94. #endregion
  95. }
  96. }