Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net.Sockets;
- using System.Net;
- using System.IO;
- using System.Text.RegularExpressions;
- using System.Diagnostics;
- using OpenQA.Selenium.Firefox;
- using OpenQA.Selenium;
- using HtmlAgilityPack;
- namespace BBP
- {
- class Program
- {
- static void Main(string[] args)
- {
- TcpListener listener = new TcpListener(IPAddress.Loopback, 8080);
- listener.Start();
- IWebDriver driver = new FirefoxDriver();
- while (true)
- {
- TcpClient client = listener.AcceptTcpClient();
- System.Net.Sockets.NetworkStream ns = client.GetStream();
- System.Text.Encoding enc = System.Text.Encoding.UTF8;
- System.IO.MemoryStream ms = new System.IO.MemoryStream();
- byte[] resBytes = new byte[256];
- do
- {
- //データの一部を受信する
- int resSize = ns.Read(resBytes, 0, resBytes.Length);
- //Readが0を返した時はクライアントが切断したと判断
- if (resSize == 0)
- {
- Console.WriteLine("クライアントが切断しました。");
- break;
- }
- //受信したデータを蓄積する
- ms.Write(resBytes, 0, resSize);
- } while (ns.DataAvailable);
- //受信したデータを文字列に変換
- string resMsg = enc.GetString(ms.ToArray());
- ms.Close();
- Debug.WriteLine(resMsg);
- Regex regex = new Regex("http://([a-z]+\\.2ch\\.net):80/(\\w+)/dat/(\\d+)\\.dat");
- Match match = regex.Match(resMsg);
- if (match.Success)
- {
- string url = "http://" + match.Groups[1].Value + "/test/read.cgi/" + match.Groups[2].Value + "/" + match.Groups[3].Value + "/";
- //string url = "http://anago.2ch.net/test/read.cgi/software/1424024569/";
- driver.Navigate().GoToUrl(url);
- string title = driver.FindElement(By.CssSelector("h1")).Text;
- var htmlDoc = new HtmlAgilityPack.HtmlDocument();
- htmlDoc.LoadHtml(driver.PageSource);
- List<Res> ress = new List<Res>();
- foreach (HtmlNode node in htmlDoc.DocumentNode.SelectNodes(@"//dt"))
- {
- Res res = new Res();
- string[] datas = node.InnerText.Split(new char[] { ':' });
- res.Name = datas[1];
- res.Date = datas[2];
- ress.Add(res);
- }
- HtmlNodeCollection bodys = htmlDoc.DocumentNode.SelectNodes(@"//dd");
- for (int i = 0; i < bodys.Count; i++)
- {
- ress[i].Body = bodys[i].InnerHtml.Replace("\n", "").Replace("\r","");
- }
- string returns = "HTTP/1.1 200 OK\n\n";
- for (int i = 0; i < ress.Count; i++)
- {
- if (i == 0)
- returns += ress[i].Name + "<>" + ress[i].Mail + "<>" + ress[i].Date + "<>" + ress[i].Body + "<>" + title + "\n";
- else
- returns += ress[i].Name + "<>" + ress[i].Mail + "<>" + ress[i].Date + "<>" + ress[i].Body + "<>\n";
- }
- byte[] sendBytes = Encoding.GetEncoding(932).GetBytes(returns);
- //データを送信する
- ns.Write(sendBytes, 0, sendBytes.Length);
- }
- //リスナを閉じる
- ns.Close();
- client.Close();
- }
- listener.Stop();
- Console.WriteLine("Listenerを閉じました。");
- }
- }
- class Res
- {
- public string Name { get; set; }
- public string Mail { get; set; }
- public string Date { get; set; }
- public string Body { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement