using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Chilkat;
namespace HtmlProcessor
{
public class XiaoNeiFriend
{
string friendName;
string href;
public string FriendName
{
get { return friendName; }
}
public string Href
{
get { return href; }
}
public XiaoNeiFriend(string name,string link)
{
friendName = name;
href = link;
}
public override string ToString()
{
return friendName + " " + href;
//return base.ToString();
}
}
public class HtmlParser
{
//string htmlFileName;
//string xmlFileName = @"d:/xiaonei/out_wang.xml";
bool hasPrivacyLock=false;
bool isBoy = true;//sex
System.DateTime currentTime;
Random rnd;
public List<XiaoNeiFriend> friendList = new List<XiaoNeiFriend>();
public bool IsLocked
{
get { return hasPrivacyLock; }
}
public bool IsBoy
{
get { return isBoy; }
}
public HtmlParser()//string fileName)
{
}
public string AbstractFriends(string allText)
{
//StreamReader reader = new StreamReader(htmlFileName);
//string allText=reader.ReadToEnd();
hasPrivacyLock = false;
isBoy = true;
//未加只有好友才能访问限制
int index = allText.IndexOf("<h4>他的好友</h4>");
if (-1==index)
{
index = allText.IndexOf("<h4>她的好友</h4>");
isBoy = false;
}
if (-1==index)
{
isBoy = true;
//可能是自己的主页
index = allText.IndexOf("<h4>好友</h4>");
if (-1==index)
{
//也可能是好友才能访问限制
index = allText.IndexOf("<h1>他的好友<span>");
if (-1==index)
{
index = allText.IndexOf("<h1>她的好友<span>");
if (-1==index)
{
isBoy = true;
Console.WriteLine("出现未知情况,可能这人没有好友!");
return "";
}
hasPrivacyLock = true;
isBoy = false;
}
hasPrivacyLock = true;
}
}
allText=allText.Substring(index);
int indexOfLi = allText.IndexOf("<li>");
if (-1!=indexOfLi)
{
allText = allText.Substring(indexOfLi);
int indexOfEndDiv = allText.IndexOf("</ul>");
if (-1 != indexOfEndDiv)
{
allText = allText.Substring(0, indexOfEndDiv);
//int indexOfEndLi = allText.LastIndexOf("</li>");
//if (-1!=indexOfEndLi)
//{
// //具有好友锁定时的情况
// //Console.WriteLine("Can't find </li>!");
// //return "";
// allText = allText.Substring(0, indexOfEndLi);
//}
//将之转化为良定义的xml文件
allText=allText.Replace("&", "&");//转义&字符
StringBuilder sb = new StringBuilder();
sb.Append("<root>/r/n");
sb.Append(allText);
sb.Append("/r/n</root>");
return sb.ToString();
}
else
{
Console.WriteLine("Can't find </ul>!");
return "";
}
}
else
{
Console.WriteLine("Can't find <li>!");
return "";
}
}
//获取所有的friend;暂时没有用到
public bool GetAllFriends(string fn)
{
Chilkat.Xml xml = new Chilkat.Xml();
if (!xml.LoadXml(fn))
{
Console.WriteLine("Failed to load xml string!");
return false;
}
Chilkat.Xml Li = xml.FirstChild();
while (Li!=null)
{
Chilkat.Xml link = Li.GetChildWithTag("a");
if (null==link)
{
return false;
}
string href=link.GetAttrValue("href");
string title = link.GetAttrValue("title");
if (String.IsNullOrEmpty(href)||string.IsNullOrEmpty(title))
{
return false;
}
XiaoNeiFriend friend = new XiaoNeiFriend(title, href);
friendList.Add(friend);
Li = Li.NextSibling();
}
return true;
}
public XiaoNeiFriend GetARandomRecord(string fn)
{
Chilkat.Xml xml = new Chilkat.Xml();
if (!xml.LoadXml(fn))
{
Console.WriteLine("Failed to load xml string!");
return null;
}
int numOfChild = xml.NumChildren;
//Console.WriteLine("num of children:{0}", numOfChild);
if (numOfChild==0)
{
Console.WriteLine("This person do not has a friend!");
return null;
}
currentTime = DateTime.Now;
// Console.WriteLine(currentTime.Millisecond);
rnd = new Random(currentTime.Millisecond);
int nth = rnd.Next(numOfChild);
//Console.WriteLine("rand={0}", nth);
Chilkat.Xml nthRecd=xml.GetNthChildWithTag("li",nth);
if (nthRecd != null)
{
Chilkat.Xml link =nthRecd.GetChildWithTag("a");
if (null == link)
{
return null;
}
string href = link.GetAttrValue("href");
string title = link.GetAttrValue("title");
if (String.IsNullOrEmpty(href) || string.IsNullOrEmpty(title))
{
return null;
}
return new XiaoNeiFriend(title, href);
}
return null;
}
}
}