/MobileFlares/IridiumParser/PageParser.cs

# · C# · 139 lines · 122 code · 15 blank · 2 comment · 14 complexity · dc5537496ba9cdd1fdf67a4fbad49c98 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net;
  6. using System.IO;
  7. using System.Text.RegularExpressions;
  8. using System.Collections;
  9. using IridiumParser;
  10. namespace IridiumFlares
  11. {
  12. public class PageParser
  13. {
  14. private string content = null;
  15. private string url = null;
  16. private Match match = null;
  17. private ArrayList flareList = new ArrayList();
  18. //private const string RegTable = @"<th>Distance to.+Satellite</TH></tr>([\s\S]+)</table><p>";
  19. private const string RegTable = @"<th>Distance to([\s\S]+)</table><p>";
  20. private const string RegFlare = @"<tr>([\s\S]+?)</tr>";
  21. public PageParser(String _url)
  22. {
  23. this.url = _url;
  24. this.getFile();
  25. }
  26. public PageParser(StringBuilder content)
  27. {
  28. this.content = content.ToString();
  29. }
  30. private void getFile()
  31. {
  32. this.content = GetPage(url);
  33. }
  34. public Flare[] parse()
  35. {
  36. if (content == null) getFile();
  37. match = Regex.Match(content, RegTable);
  38. if (!match.Success)
  39. {
  40. Console.WriteLine("No table with flare information found!");
  41. return null;
  42. }
  43. getFlareContents(match.Groups[1].ToString());
  44. return (Flare[]) this.flareList.ToArray(typeof (Flare));
  45. }
  46. private void getFlareContents(String table)
  47. {
  48. int index = 0;
  49. int count = 0;
  50. while((match = Regex.Match(table, RegFlare)).Success){
  51. Group g = match.Groups[1];
  52. //Console.WriteLine("--------------------\n" + g + "\n--------------------\n");
  53. flareList.Add(FlareParser.parseHTML(g.ToString()));
  54. index = match.Index + g.ToString().Length;
  55. Console.WriteLine("Index: "+index);
  56. table = table.Substring(index);
  57. count++;
  58. }
  59. Console.WriteLine(count+ "flares found");
  60. }
  61. private String GetPage(string url)
  62. {
  63. WebResponse response = null;
  64. Stream stream = null;
  65. StreamReader reader = null;
  66. StringBuilder buffer = new StringBuilder();
  67. try
  68. {
  69. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  70. response = request.GetResponse();
  71. stream = response.GetResponseStream();
  72. if (!response.ContentType.ToLower().StartsWith("text/")) return null;
  73. reader = new StreamReader(stream);
  74. String line = null;
  75. while ((line = reader.ReadLine()) != null)
  76. {
  77. buffer.Append(line + "\r\n");
  78. }
  79. return buffer.ToString();
  80. }
  81. catch (WebException e)
  82. {
  83. System.Console.WriteLine("Can't download:" + e);
  84. return null;
  85. }
  86. catch (Exception e)
  87. {
  88. System.Console.WriteLine("Can't download:" + e);
  89. return null;
  90. }
  91. finally
  92. {
  93. if (reader != null) reader.Close();
  94. if (stream != null) stream.Close();
  95. if (response != null) response.Close();
  96. }
  97. }
  98. public Flare[] getFlaresWithCriteria(String _start, String _end, int brighterThan)
  99. {
  100. TimeSpan start;
  101. TimeSpan end;
  102. try
  103. {
  104. start = TimeSpan.Parse(_start);
  105. end = TimeSpan.Parse(_end);
  106. }
  107. catch (Exception)
  108. {
  109. throw new Exception("Could not parse time string");
  110. }
  111. var flares = from Flare f in flareList
  112. where f.Time.TimeOfDay >= start && f.Time.TimeOfDay <= end && f.Intensity < brighterThan
  113. select f;
  114. return flares.ToArray();
  115. }
  116. public Flare[] getFlaresWithCriteria(int brighterThan)
  117. {
  118. var flares = from Flare f in flareList
  119. where f.Intensity < brighterThan
  120. select f;
  121. return flares.ToArray();
  122. }
  123. }
  124. }