博客備份小工具3

    接着 博客轉發小工具2 ,又弄了一個第三版。主要功能有:博客備份到本地、瀏覽備份到本地的博客、關鍵字搜索本地的博客和轉發博客能夠選擇我的分類 填寫Tag標籤。其實想了想,轉發博客幹嗎非要在本地客戶端轉發,直接在博客園的頁面用js不就能夠達到目的麼。想是這麼想,還沒嘗試。等我寫完了這個博客就去試試。。繼續回到這個小工具,說實在的本人作的這個小工具界面醜的那是、、反正就是很醜很醜啦。沒辦法,沒有美工的那種藝術細胞。還有就是,整個功能其實真沒什麼技術含量,仍是html的抓取,winfrom在工做中也沒用過,就平時偶爾玩玩,望大神們勿噴~~。  javascript

    不過我以爲這個小工具的做用:第1、能夠用來備註本身的博客,本身留個底。第2、某些朋友的工做環境可能沒有外網(我之前的公司就沒有),這樣的話能夠用來備份一些博友的系類文章當資料。【要是能夠的話,把系類文章導成dpf文檔那就更爽了。但是我一直沒成功~】。html

    也許有寫人會說:啥啥啥的早就有相似的軟件了。我不知道,我沒用過,我以爲本身作的用着開心。就當練手吧。java

   開始說內容了。node

 

功能一:備份博客到本地

備份的博客是博客園推薦的前150個。也能夠本身手動輸入要備份的url。web

1.加載推薦博客

var docment = new HtmlAgilityPack.HtmlDocument();
docment.LoadHtml(BlogCommon.PostReqest(BlogUrlString.推薦博客url));
var nodes = docment.DocumentNode.SelectNodes("//ul//a");
List<object> list = new List<object>();
for (int i = 0; i < nodes.Count; i++)
{
    var url = "http://www.cnblogs.com" + nodes[i].Attributes["href"].Value;
    var name = "NO." + (i + 1).ToString() + " " + nodes[i].InnerText;
    list.Add(new { url = url, name = name });
}
list_tuijian.DataSource = list;
list_tuijian.ValueMember = "url";
list_tuijian.DisplayMember = "name";

 

2.加載隨筆分類、檔案和標籤

 首先選中博客改變url,而後url文本框的TextChanged事件裏面加載 隨筆分類、檔案和標籤數據。數據庫

var docment = new HtmlAgilityPack.HtmlDocument();
docment.LoadHtml(BlogCommon.GetRequest(BlogUrlString.我的博客分類url(strname)));
//加載隨筆類型
LoadTree(docment, "//div[@class='catListPostCategory']//a", tree_suibiType, "隨筆分類");
tree_suibiType.CheckBoxes = true;

// 加載隨筆檔案
LoadTree(docment, "//div[@class='catListPostArchive']//a", tree_suibiTime, "隨筆檔案");
tree_suibiTime.CheckBoxes = true;

//// 加載文章類型
//LoadTree(docment, "//div[@class='catListArticleCategory']//a", tree_wenzhangType, "文章類型");
//tree_suibiType.CheckBoxes = true;

docment.LoadHtml(BlogCommon.GetRequest(BlogUrlString.標籤url(strname)));
// 加載文章類型
LoadTree(docment, "//div[@id='taglist']//a", tree_tag, "標籤");
tree_tag.CheckBoxes = true;

 

public void LoadTree(HtmlAgilityPack.HtmlDocument docment, string where, TreeView trre, string TreeName)
{
    try
    {
        trre.Nodes.Clear();
        var nodes = docment.DocumentNode.SelectNodes(where);
        if (nodes == null || nodes.Count <= 0)
            return;
        List<object> list = new List<object>();

        TreeNode treenodeS = new TreeNode() { Name = "", Text = TreeName };
        for (int i = 0; i < nodes.Count; i++)
        {
            TreeNode treenode = new TreeNode();
            var url = nodes[i].Attributes["href"].Value;
            var name = nodes[i].InnerText;
            list.Add(new { url = url, name = name });
            treenode.Text = name;
            treenode.Name = url;
            treenodeS.Nodes.Add(treenode);
        }
        trre.Nodes.Add(treenodeS);
        trre.ExpandAll();//展開節點
    }
    catch (Exception ex)
    {
        MessageBox.Show("加載" + TreeName + "錯誤" + ex.Message);
    }
}

 

3.點擊備份

點擊備份,讀取選中的複選框所包含的全部url。ide

#region 獲取某個選中樹節點 的全部url地址

        public List<string[]> getShuibiType_all_url(string url, ref List<string[]> list_urlS)
        {
            label1.Text = "正在讀取" + url;
            var docment = new HtmlAgilityPack.HtmlDocument();
            docment.LoadHtml(BlogCommon.GetRequest(url));
            // a class entrylistItemTitle  entrylistTitle
            var nodes = docment.DocumentNode.SelectNodes("//div[@class='entrylist']//div[@class='entrylistPosttitle']//a");
            if (nodes == null)
                nodes = docment.DocumentNode.SelectNodes("//div[@id='content']//div[@class='post post-list-item']/h2/a");
            var entrylistTitle = docment.DocumentNode.SelectNodes("//h1[@class='entrylistTitle']");
            if (entrylistTitle == null)
                entrylistTitle = docment.DocumentNode.SelectNodes("//div[@id='content']/h2");
            var posts_title = entrylistTitle != null ? entrylistTitle[0].InnerText : "未分類";
            if (nodes != null && nodes.Count > 0)
            {
                for (int j = 0; j < nodes.Count; j++)
                {
                    label1.Text = "正在讀取[" + posts_title + "]" + (j + 1) + "/" + nodes.Count + "" + "url地址";

                    list_urlS.Add(
                        new string[] 
                        { 
                            nodes[j].Attributes["href"].Value,
                            nodes[j].InnerText,
                            entrylistTitle.Count>=1?entrylistTitle[0].InnerText:"未分類"
                        });
                }
            }
            return list_urlS;
        }

        #endregion

 

點擊備份,保存到本地。工具

#region 保存內容到文件
        /// <summary>
        /// 保存內容到文件 
        /// </summary>
        /// <param name="list_str">根據地址集合</param>
        /// <param name="type"></param>
        /// <param name="docment"></param>
        public void SaveFile(List<string[]> list_str, string type, HtmlAgilityPack.HtmlDocument docment)
        {
            if (list_str == null || list_str.Count <= 0)
                return;
            for (int i = 0; i < list_str.Count; i++)
            {
                var url = list_str[i][0];
                var name = url.Split('/').Length >= 4 ? url.Split('/')[3] : "未分類";
                if (list_str[i].Length == 3)
                {
                    try
                    {
                        label1.Text = "正在請求[" + name + "/" + type + "]頁面" + (i + 1) + "/" + list_str.Count + "";

                        #region 文件路徑處理
                        //過濾文件名的特殊字符
                        string[] rep = new string[] { "&mdash;", ":", "<", ">", "?", "*", "/", "|", "\"" };
                        string fileNmae = list_str[i][1].Replace("\\", string.Empty);
                        for (int j = 0; j < rep.Length; j++)
                        {
                            if (rep[j].Length == 0)
                                continue;
                            fileNmae = fileNmae.Replace(rep[j], string.Empty);
                        }
                        var Paht = ForwardPath + name + "\\" + type + "\\" + list_str[i][2] + "\\";
                        for (int j = 0; j < rep.Length; j++)
                        {
                            if (rep[j].Length == 0)
                                continue;
                            Paht = Paht.Replace(rep[j], string.Empty);
                        }
                        Paht = FileHelp.PathBlogs + Paht;
                        var FilePath = Paht + fileNmae + ".html"; 
                        #endregion

                        if (radioButton2.Checked && File.Exists(FilePath))
                        {
                            label1.Text = "頁面數據存在[" + name + "/" + type + "]頁面" + (i + 1) + "/" + list_str.Count + "";
                            continue;
                        } 

                        var html = BlogCommon.GetRequest(url);
                        docment.LoadHtml(html);                  
                        var Nodes = docment.DocumentNode.SelectNodes("//div[@class='postBody']");
                        if (Nodes == null)
                            Nodes = docment.DocumentNode.SelectNodes("//div[@id='cnblogs_post_body']");
                        html = Nodes[0].InnerHtml;
                        docment.DocumentNode.InnerHtml = "<div id='my_html_postBody'>" + html + "</div>";

                        #region 去掉a標籤的超連接
                        //去掉a標籤的超連接
                        var html_aS = docment.DocumentNode.SelectNodes("//a");
                        if (html_aS != null)
                            for (int z = 0; z < html_aS.Count; z++)
                                if (html_aS[z].Attributes["href"] != null && html_aS[z].Attributes["href"].Value[0] != '#')
                                    html_aS[z].Attributes["href"].Value = "javascript:void()"; 
                        #endregion 

                        label1.Text = "準備保存[" + name + "/" + type + "]內容" + (i + 1) + "/" + list_str.Count + "~";
                        FileHelp.CreatePath(Paht + @"imgs\");
                        saveImg(docment, Paht + @"imgs\", label1.Text + "...");
                        docment.CreateAttribute("url", url);

                        //profile_block                        
                        docment.DocumentNode.InnerHtml = "<div id='mytext_url'>" + url + "</div>"
                                                        + "<div id='mytext_BlogTitleName'>" + name + "</div>"
                                                        + docment.DocumentNode.InnerHtml;

                        docment.Save(FilePath, Encoding.UTF8);
                        File.SetCreationTime(FilePath, new DateTime(FileHelp.Ticks));
                        LuceneHelp.CreateIndexFilePath(FilePath);
                        //FileHelps.SaveFile(Paht, fileNmae + ".txt", html);
                        label1.Text = "保存成功" + (i + 1) + "/" + list_str.Count + "";
                    }
                    catch (Exception ex)
                    {
                        string mess = url + "\r\n" + ex.Message + "\r\n" + ex.StackTrace + "\r\n\r\n";
                        string myPath = FileHelp.PathBlogs + ForwardPath + name + "\\" + type + "\\";
                        FileHelp.SaveFile(myPath, "err.txt", mess, false);
                    }
                }
                else
                    FileHelp.SaveFile(FileHelp.PathBlogs + ForwardPath + name + "\\" + type + "\\", "err.txt", "異常373位置", false);
            }
        }
        #endregion
View Code

 

功能二:瀏覽本地博客

1.遞歸加載目錄下因此博客文件

(這個方法不行,若是文件多的話,會有點卡。之後要改爲點擊加載下層目錄)post

public TreeNodeCollection loadTree(string path, TreeNodeCollection treeS, bool isNe = true)
{
    List<string[]> list = FileHelp.FileSystemInfo(path, "3");
    string[] str = new string[] { "", "" };
    if (list != null)
    {

        for (int i = 0, j = 0; i < list.Count; i++, j++)
        {
            if (list[i][0] == "imgs")
            {
                j--;
                continue;
            }
            TreeNode tree1 = new TreeNode() { Text = list[i][0] };
            treeS.Add(tree1);
            loadTree(list[i][1], treeS[j].Nodes);
        }
    }
    return treeS;
}

 

2.點擊加載博客內容(這裏使用到了webBrowser控件來顯示博客內容)

 #region 點擊tree樹  加載對應內容
        /// <summary>
        /// 點擊tree樹  加載對應內容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Action == TreeViewAction.ByMouse || e.Action == TreeViewAction.ByKeyboard)
            {
                htmlPath = FileHelp.PathBlogs + Forward.ForwardPath + e.Node.FullPath;
                string html = FileHelp.GetFile(htmlPath);
                HtmlAgilityPack.HtmlDocument docment = new HtmlAgilityPack.HtmlDocument();
                docment.LoadHtml(html);
                try
                {
                    var text_html = docment.DocumentNode.SelectNodes("//div[@id='my_html_postBody']")[0].InnerHtml;
                    var mytext_url = docment.DocumentNode.SelectNodes("//div[@id='mytext_url']")[0].InnerHtml;
                    webBrowser1.DocumentText = text_html;
                    text_url.Text = mytext_url;
                }
                catch (Exception)
                { }
            }
        }
        #endregion

功能三:本地瀏覽關鍵字搜索

搜索使用的Lucenne.net。這裏面的水太深了。這裏就使用到了點皮毛。這裏給出一些資料地址ui

1.首先在備份的時候就須要建立搜索索引。

/// <summary>
/// 建立索引
/// </summary>
/// <param name="analyzer"></param>
/// <param name="title"></param>
/// <param name="content"></param>
private static void AddIndex(IndexWriter writer, string title, string content, string filePath, string id, string mytext_BlogTitleName = "Blog", string ClickQuantity = "0")
{
    try
    {
        // Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));

        Document doc = new Document();
        doc.Add(new Field("Title", title, Field.Store.YES, Field.Index.ANALYZED));//存儲且索引
        //doc.Add(new Field("Content", content, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));// Field.Store.YES, Field.Index.ANALYZED));//存儲且索引
        doc.Add(new Field("Content", content, Field.Store.YES, Field.Index.ANALYZED));
        doc.Add(new Field("filePath", filePath, Field.Store.YES, Field.Index.NOT_ANALYZED));//存儲且索引
        doc.Add(new Field("BlogTitleName", mytext_BlogTitleName, Field.Store.YES, Field.Index.NOT_ANALYZED));
        doc.Add(new Field("id", id, Field.Store.YES, Field.Index.NOT_ANALYZED));
        doc.Add(new Field("ClickQuantity", ClickQuantity, Field.Store.YES, Field.Index.NOT_ANALYZED));
        //防止重複索引
        writer.DeleteDocuments(new Term("id", id));
        //AddTime
        writer.AddDocument(doc);
    }
    catch (FileNotFoundException fnfe)
    {
        throw fnfe;
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

 

2.點擊搜索

#region 點擊搜索
        private void but_select_Click(object sender, EventArgs e)
        {
            new Thread(delegate()
            {
                try
                {
                    lab_meg.Text = "正在搜索.....";
                    var htm = "";
                    var list = LuceneHelp.SelectData(FileHelp.PathBlogs + "Index\\", txt_select.Text.Trim(), this.lab_meg);
                    var count = 150;
                    if (list.Count >= count)
                    {
                        var mess = MessageBox.Show("搜索結果過多,是否只顯示前" + count + "條~", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
                        if (mess == System.Windows.Forms.DialogResult.Yes)
                        {
                            int index = 0;
                            foreach (var item in list)
                            {
                                index++;
                                htm += "<a name='click_load_file' href='#' filePath='" + item.FilePath + "'>" +
                                         item.FileName + "</a><br/><div>" + item.Content
                                         + "<div><br/><div style='width:100%;text-align:right;'><span style='margin-right:20px;background-color:#e1d2d2'>做者["
                                         + item.BlogTitleName + "]&nbsp;&nbsp;搜索(" + item.ClickQuantity + ")</span></div><br/><br/>";
                                if (index >= count)
                                    break;
                            }
                        }
                        else
                            foreach (var item in list)
                            {
                                htm += "<a name='click_load_file' href='#' filePath='" + item.FilePath + "'>" +
                                         item.FileName + "</a><br/><div>" + item.Content
                                         + "<div><br/><div style='width:100%;text-align:right;'><span style='margin-right:20px;background-color:#e1d2d2'>做者["
                                         + item.BlogTitleName + "]&nbsp;&nbsp;搜索(" + item.ClickQuantity + ")</span></div><br/><br/>";
                            }

                    }
                    else
                    {
                        foreach (var item in list)
                        {
                            htm += "<a name='click_load_file' href='#' filePath='" + item.FilePath + "'>" +
                                     item.FileName + "</a><br/><div>" + item.Content
                                     + "<div><br/><div style='width:100%;text-align:right;'><span style='margin-right:20px;background-color:#e1d2d2'>做者["
                                     + item.BlogTitleName + "]&nbsp;&nbsp;搜索(" + item.ClickQuantity + ")</span></div><br/><br/>";
                        }
                    }
                    webBrowser1.DocumentText = htm;
                    htmlList = htm;
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.Message);
                    lab_meg.Text = "搜索出錯~" + ex.Message;
                }
            }).Start();
        }
        #endregion

 

下載地址:

環境:vs2013 版本:.Net Framework4.5 數據庫:無 

程序下載 源碼下載  (源碼都給您了,順手點個讚唄~~)

相關文章
相關標籤/搜索