Advertisement
Guest User

Untitled

a guest
Feb 16th, 2015
773
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net.Sockets;
  6. using System.Net;
  7. using System.IO;
  8. using System.Text.RegularExpressions;
  9. using System.Diagnostics;
  10. using OpenQA.Selenium.Firefox;
  11. using OpenQA.Selenium;
  12. using HtmlAgilityPack;
  13.  
  14. namespace BBP
  15. {
  16. class Program
  17. {
  18. static void Main(string[] args)
  19. {
  20. TcpListener listener = new TcpListener(IPAddress.Loopback, 8080);
  21. listener.Start();
  22. IWebDriver driver = new FirefoxDriver();
  23.  
  24. while (true)
  25. {
  26. TcpClient client = listener.AcceptTcpClient();
  27. System.Net.Sockets.NetworkStream ns = client.GetStream();
  28. System.Text.Encoding enc = System.Text.Encoding.UTF8;
  29. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  30. byte[] resBytes = new byte[256];
  31. do
  32. {
  33. //データの一部を受信する
  34. int resSize = ns.Read(resBytes, 0, resBytes.Length);
  35. //Readが0を返した時はクライアントが切断したと判断
  36. if (resSize == 0)
  37. {
  38. Console.WriteLine("クライアントが切断しました。");
  39. break;
  40. }
  41. //受信したデータを蓄積する
  42. ms.Write(resBytes, 0, resSize);
  43. } while (ns.DataAvailable);
  44. //受信したデータを文字列に変換
  45. string resMsg = enc.GetString(ms.ToArray());
  46. ms.Close();
  47. Debug.WriteLine(resMsg);
  48.  
  49. Regex regex = new Regex("http://([a-z]+\\.2ch\\.net):80/(\\w+)/dat/(\\d+)\\.dat");
  50. Match match = regex.Match(resMsg);
  51. if (match.Success)
  52. {
  53. string url = "http://" + match.Groups[1].Value + "/test/read.cgi/" + match.Groups[2].Value + "/" + match.Groups[3].Value + "/";
  54. //string url = "http://anago.2ch.net/test/read.cgi/software/1424024569/";
  55. driver.Navigate().GoToUrl(url);
  56. string title = driver.FindElement(By.CssSelector("h1")).Text;
  57. var htmlDoc = new HtmlAgilityPack.HtmlDocument();
  58. htmlDoc.LoadHtml(driver.PageSource);
  59. List<Res> ress = new List<Res>();
  60. foreach (HtmlNode node in htmlDoc.DocumentNode.SelectNodes(@"//dt"))
  61. {
  62. Res res = new Res();
  63. string[] datas = node.InnerText.Split(new char[] { ':' });
  64. res.Name = datas[1];
  65. res.Date = datas[2];
  66.  
  67. ress.Add(res);
  68. }
  69. HtmlNodeCollection bodys = htmlDoc.DocumentNode.SelectNodes(@"//dd");
  70. for (int i = 0; i < bodys.Count; i++)
  71. {
  72. ress[i].Body = bodys[i].InnerHtml.Replace("\n", "").Replace("\r","");
  73. }
  74.  
  75. string returns = "HTTP/1.1 200 OK\n\n";
  76. for (int i = 0; i < ress.Count; i++)
  77. {
  78. if (i == 0)
  79. returns += ress[i].Name + "<>" + ress[i].Mail + "<>" + ress[i].Date + "<>" + ress[i].Body + "<>" + title + "\n";
  80. else
  81. returns += ress[i].Name + "<>" + ress[i].Mail + "<>" + ress[i].Date + "<>" + ress[i].Body + "<>\n";
  82. }
  83.  
  84. byte[] sendBytes = Encoding.GetEncoding(932).GetBytes(returns);
  85. //データを送信する
  86. ns.Write(sendBytes, 0, sendBytes.Length);
  87. }
  88. //リスナを閉じる
  89. ns.Close();
  90. client.Close();
  91. }
  92. listener.Stop();
  93. Console.WriteLine("Listenerを閉じました。");
  94.  
  95. }
  96. }
  97. class Res
  98. {
  99. public string Name { get; set; }
  100. public string Mail { get; set; }
  101. public string Date { get; set; }
  102. public string Body { get; set; }
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement