using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.Xml;

public partial class tool_index : BasePage
{
    public SiteInfo si = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        //HtmlHead head = this.Page.Header;
        si = getSiteInfo();
        //HtmlMeta meta = new HtmlMeta();
        //meta.Name = "keywords";
        //meta.Content = si.SiteDesc;
        //head.Controls.Add(meta);
        //head.InnerHtml = head.InnerHtml.Replace("SiteDesc",si.SiteDesc);
        
        this.s3.Attributes.Add("onclick", "return checkDomain1('"+this.q.ClientID+"')");// "HLoading(1,'"+this.q.ClientID+"')");
        if (!IsPostBack)
        {
            SearchFromBaiDu("13sb.cn");
            SearchFromGoogle("13sb.cn");
            SearchFromYahoo("13sb.cn");
            SearchFromSoSo("13sb.cn");
            SearchFromYodao("13sb.cn");
            SearchFromLive("13sb.cn");
            getNewSite();
            
        }
    }
    protected void s3_Click(object sender, EventArgs e)
    {
        InsertNodes(this.q.Text.Trim());
        SearchFromBaiDu(null);
        SearchFromGoogle(null);
        SearchFromYahoo(null);
        SearchFromSoSo(null);
        SearchFromYodao(null);
        SearchFromLive(null);
        this.RegisterStartupScript(Guid.NewGuid().ToString(), "<script>HLoading(0)</script>");
        getNewSite();
    }
    void SearchFromBaiDu(string url)
    {
        string qText;
        string lText;

        if (string.IsNullOrEmpty(url))
        {
            qText = "http://www.baidu.com/s?si=&rn=20&tn=baiduadv&ie=gb2312&ct=0&wd=site%3A" + this.q.Text + "&cl=3";
            lText = "http://www.baidu.com/s?wd=domain%3A" + this.q.Text;
        }
        else
        {
            qText = "http://www.baidu.com/s?si=&rn=20&tn=baiduadv&ie=gb2312&ct=0&wd=site%3A" + url + "&cl=3"; ;
            lText = "http://www.baidu.com/s?wd=domain%3A" + url;
        }

        string link = WebClass.Get(lText);
        string input = WebClass.Get(qText);
        string pattern = @"百度一下，找到相关网页[\s\S]*?(?=篇，用时)";
        //string pattern1 = @"百度一下，找到相关网页[\s\S]*?(?=篇，用时)";
        string str6 = Regex.Match(input, pattern, RegexOptions.IgnoreCase).Value.Replace("百度一下，找到相关网页约", "").Replace("百度一下，找到相关网页", "").Replace("约",""); ;
        string str7 = Regex.Match(link, pattern, RegexOptions.IgnoreCase).Value.Replace("百度一下，找到相关网页约", "").Replace("百度一下，找到相关网页", "").Replace("约", "");
        if (string.IsNullOrEmpty(str6))
            str6 = "0";
        if (string.IsNullOrEmpty(str7))
            str7 = "0";

        this.qBaidu.InnerHtml = "<a href='" + qText + "' target='_blank'>" + str6 + "</a>";
        this.lBaidu.InnerHtml = "<a href='" + lText + "' target='_blank'>" + str7 + "</a>";
    }

    void SearchFromGoogle(string url)
    {
        string qText;
        string lText;

        if (string.IsNullOrEmpty(url))
        {
             qText = "http://www.google.cn/search?q=site%3A" + this.q.Text;
             lText = "http://www.google.cn/search?q=domain:" + this.q.Text + "&hl=zh-CN&newwindow=1&filter=0";
        }
        else
        {
            qText = "http://www.google.cn/search?q=site%3A" + url;
            lText = "http://www.google.cn/search?q=domain:" + url + "&hl=zh-CN&newwindow=1&filter=0";
        }
        string input = WebClass.Get(qText);
        string u = WebClass.Get(lText);

        string pattern = @"有<b>[\s\S]*?(?=</b>)";
        string pattern1 = @"有<b>[\s\S]*?(?=</b>)";
        input = Regex.Replace( input, @"\s", "" );
        u = Regex.Replace(u, @"\s", "");
        string str7 = Regex.Match(input, pattern1, RegexOptions.IgnoreCase).Value.Replace(" <b>", "");
        string str11 = Regex.Match(u, pattern, RegexOptions.IgnoreCase).Value.Replace("<b>", "");
        string str8 = "";
        string str9 = @" <b>[\s\S]*?(?=</b>)";
        str8 = str7.Replace("有", "");
        str11 = str11.Replace("有", "");
        if (string.IsNullOrEmpty(str8))
            str8 = "0";
        if (string.IsNullOrEmpty(str11))
            str11 = "0";
        this.qGoogle.InnerHtml = "<a href='" + qText + "' target='_blank'>" + str8 + "</a>";
        this.lGoogle.InnerHtml = "<a href='" + lText + "' target='_blank'>" + str11 + "</a>";
        //break;
    }

    void SearchFromYahoo(string url)
    {

        string qText;
        string lText;

        if (string.IsNullOrEmpty(url))
        {
            qText = "http://one.cn.yahoo.com/s?p=site%3A" + this.q.Text + "&pid=hp&v=web";
            lText = "http://one.cn.yahoo.com/s?p=linkdomain%3A" + this.q.Text + "&v=web&pid=hp";
        }
        else
        {
            qText = "http://one.cn.yahoo.com/s?p=site%3A" + url + "&pid=hp&v=web";
            lText = "http://one.cn.yahoo.com/s?p=linkdomain%3A" + url + "&v=web&pid=hp";
        }

        string input = WebClass.Get2(qText);
        string u = WebClass.Get2(lText);
        string pattern = @"找到相关网页约[\s\S]*?(?=条)";
        string str7 = Regex.Match(input, pattern, RegexOptions.IgnoreCase).Value.Replace("找到相关网页约","");

        string str = Regex.Match(u, pattern, RegexOptions.IgnoreCase).Value.Replace("找到相关网页约", "");
        if (string.IsNullOrEmpty(str7))
            str7 = "0";
        if (string.IsNullOrEmpty(str))
            str = "0";
        this.qYahoo.InnerHtml = "<a href='" + qText + "' target='_blank'>" + str7 + "</a>";
        this.lYahoo.InnerHtml = "<a href='" + lText + "' target='_blank'>" + str + "</a>";
    }

    void SearchFromSoSo(string url)
    {
        string qText;
        string lText;

        if (string.IsNullOrEmpty(url))
        {
            qText = "http://www.soso.com/q?pid=s.idx&w=site%3A" + this.q.Text;
            lText = "http://www.soso.com/q?gid=&cin=&sc=web&bs=linkdomain%3A" + this.q.Text + "&ch=w.uf&num=10&site=&w=link%3A" + this.q.Text;
        }
        else
        {
            qText = "http://www.soso.com/q?pid=s.idx&w=site%3A" + url;
            lText = "http://www.soso.com/q?gid=&cin=&sc=web&bs=linkdomain%3A" + url + "&ch=w.uf&num=10&site=&w=link%3A" + url;
        }
        //string qText = "http://www.soso.com/q?pid=s.idx&w=site%3A" + this.q.Text;
        //string lText = "http://www.soso.com/q?gid=&cin=&sc=web&bs=linkdomain%3A" + this.q.Text + "&ch=w.uf&num=10&site=&w=link%3A" + this.q.Text;
        string input = WebClass.Get(qText);
        string u = WebClass.Get(lText);
        string pattern = @"搜索到约[\s\S]*?(?=项)";
        string str7 = Regex.Match(input, pattern, RegexOptions.IgnoreCase).Value.Replace("搜索到约", "");

        string str = Regex.Match(u, pattern, RegexOptions.IgnoreCase).Value.Replace("搜索到约", "");
        if (string.IsNullOrEmpty(str7))
            str7 = "0";
        if (string.IsNullOrEmpty(str))
            str = "0";
        this.qSOSO.InnerHtml = "<a href='"+qText+"' target='_blank'>"+str7+"</a>";
        this.lSOSO.InnerHtml = "<a href='"+lText+"' target='_blank'>"+str+"</a>";
        
    }

    void SearchFromYodao(string url)
    {
        string qText;
        string lText;

        if (string.IsNullOrEmpty(url))
        {
            qText = "http://www.youdao.com/search?q=site%3A" + this.q.Text + "&btnIndex=%E6%90%9C+%E7%B4%A2&ue=utf8&keyfrom=web.index";
            lText = "http://www.youdao.com/search?lq=site%3A" + this.q.Text + "&ue=utf8&keyfrom=web.top&q=domain%3A" + this.q.Text + "&btnSearchTop=%E6%90%9C+%E7%B4%A2";
        }
        else
        {
            qText = "http://www.youdao.com/search?q=site%3A" + url+ "&btnIndex=%E6%90%9C+%E7%B4%A2&ue=utf8&keyfrom=web.index";
            lText = "http://www.youdao.com/search?lq=site%3A" + url + "&ue=utf8&keyfrom=web.top&q=domain%3A" + url + "&btnSearchTop=%E6%90%9C+%E7%B4%A2";
        }

        //string qText = "http://www.youdao.com/search?q=site%3A"+this.q.Text+"&btnIndex=%E6%90%9C+%E7%B4%A2&ue=utf8&keyfrom=web.index";
        //string lText = "http://www.youdao.com/search?lq=site%3A" + this.q.Text + "&ue=utf8&keyfrom=web.top&q=domain%3A" + this.q.Text + "&btnSearchTop=%E6%90%9C+%E7%B4%A2";
        string input = WebClass.Get2(lText);
        string u = WebClass.Get2(qText);
        string pattern = @"RESULT_NO[\s\S]*?(?=DURATION)";
        string str7 = Regex.Match(input, pattern, RegexOptions.IgnoreCase).Value.Replace("RESULT_NO=", "").Replace(" ", "");

        string str = Regex.Match(u, pattern, RegexOptions.IgnoreCase).Value.Replace("RESULT_NO=", "").Replace(" ","");
        if (string.IsNullOrEmpty(str7))
            str7 = "0";
        if (string.IsNullOrEmpty(str))
            str = "0";
        this.qYodao.InnerHtml = "<a href='" + qText + "' target='_blank'>" + str + "</a>";
        this.lYodao.InnerHtml = "<a href='" + lText + "' target='_blank'>" + str7 + "</a>";
    }
    void SearchFromLive(string url)
    {

          string qText;
        string lText;

        if (string.IsNullOrEmpty(url))
        {
            qText = "http://cnweb.search.live.com/results.aspx?q=site:" + this.q.Text + "&form=QBLH";
            lText = "http://cnweb.search.live.com/results.aspx?q=link:" + this.q.Text + "&form=QBLH";
        }
        else
        {
            qText = "http://cnweb.search.live.com/results.aspx?q=site:" + url + "&form=QBLH";
            lText = "http://cnweb.search.live.com/results.aspx?q=link:" + url + "&form=QBLH";
        }
          
        //string qText = "http://cnweb.search.live.com/results.aspx?q=site:"+this.q.Text+"&form=QBLH";
        //string lText = "http://cnweb.search.live.com/results.aspx?q=link:"+this.q.Text+"&form=QBLH";
        string input = WebClass.Get2(lText);
        string u = WebClass.Get2(qText);
        string pattern = @"搜索结果（共[\s\S]*?(?=条)";
        string str7 = Regex.Match(input, pattern, RegexOptions.IgnoreCase).Value.Replace("搜索结果（共", "").Replace(" ", "");

        string str = Regex.Match(u, pattern, RegexOptions.IgnoreCase).Value.Replace("搜索结果（共", "").Replace(" ", "");
        if (string.IsNullOrEmpty(str7))
            str7 = "0";
        if (string.IsNullOrEmpty(str))
            str = "0";
        this.qLive.InnerHtml = "<a href='" + qText + "' target='_blank'>" + str + "</a>";
        this.lLive.InnerHtml = "<a href='" + lText + "' target='_blank'>" + str7 + "</a>";
    }


    void InsertNodes(string s)
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath(NewQuery));
        XmlNode root = doc.SelectSingleNode("querys");
        XmlElement xe1 = doc.CreateElement("query");
        xe1.SetAttribute("queryKey", Guid.NewGuid().ToString());
        xe1.SetAttribute("queryDate", HttpContext.Current.Server.HtmlEncode(DateTime.Now.ToString("yyyy-MM-dd")));
        xe1.SetAttribute("queryUrl", HttpContext.Current.Server.HtmlEncode(s));


        root.AppendChild(xe1);

        doc.Save(HttpContext.Current.Server.MapPath(NewQuery));
        WebClass.ReMoveCacheByKey("querySite");
    }

    /// <summary>
    /// 获取最近查询TOP30
    /// </summary>
    void getNewSite()
    {
        string s = "";
        if (WebClass.GetCache("querySite") == null)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath(NewQuery));
            XmlNodeList nodes = doc.SelectNodes("querys/query");
            
            int j = 0;
            for (int i = nodes.Count - 1; i >= 0; i--)
            {
                j++;
                if (j == 30)
                    break;
                s += "<li><a href=\"http://" + nodes[i].Attributes["queryUrl"].Value + "\" target='_blank'>" + nodes[i].Attributes["queryUrl"].Value + "</a></li>";
            }
            WebClass.SetCache("querySite", s);
        }
        else
        {
            s = WebClass.GetCache("querySite").ToString();
        }
        this.Div1.InnerHtml = s;
    }
}
