天天看點

C#編寫的QQ中轉站檔案續期工具 - 基于mshtml

主要源代碼如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using mshtml;

namespace QQFtnExtend
{
    public partial class MainForm : Form
    {
        private const string QQMAILURL = "http://mail.qq.com";
        private int state = 0;
        private int count = 0;
        private string validDuration = "7天";
        private HTMLAnchorElement aFtn;

        // configuration
        private string username = "milkyjing01";
        private string password = "a123456";
        private bool rememberMe = false;
        private int pageSize = 20;
        private bool oneByOne = false;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.LoadConfig();
            this.axWebBrowserMain.Navigate(QQMAILURL);
        }

        private void axWebBrowserMain_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
        {
            if (this.state == 0)    // login of qq mail
            {
                try
                {
                    HTMLDocument doc = (HTMLDocument)axWebBrowserMain.Document;
                    HTMLInputElement textUsername = (HTMLInputElement)doc.getElementById("uin");
                    if (textUsername == null)
                    {
                        if (this.rememberMe)
                        {
                            this.state++;
                        }
                        return;
                    }
                    HTMLInputElement textPassword = (HTMLInputElement)doc.getElementById("pp");
                    if (textPassword == null)
                    {
                        return;
                    }
                    HTMLInputElement btnLogin = (HTMLInputElement)doc.getElementById("btlogin");
                    if (btnLogin == null)
                    {
                        return;
                    }
                    textUsername.value = this.username;
                    textPassword.value = this.password;
                    btnLogin.click();
                    this.state++;
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.ToString());
                }
            }
            else if (this.state == 1)    // homepage of qq mail
            {
                try
                {
                    HTMLDocument doc = (HTMLDocument)axWebBrowserMain.Document;
                    this.aFtn = (HTMLAnchorElement)doc.getElementById("folder_ftn");
                    if (this.aFtn == null)
                    {
                        return;
                    }
                    int pos = this.aFtn.href.IndexOf('&');
                    string ftnMainUrl = this.aFtn.href.Substring(0, pos); //&t=exs_ftn_files&pagesize=20&filetype=0&listtype=self&sorttype=time&up=down
                    this.aFtn.href = ftnMainUrl + "&t=exs_ftn_files&pagesize=" + this.pageSize.ToString() + "&filetype=0&listtype=self&sorttype=time&up=down";
                    this.aFtn.click();
                    this.state++;
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.ToString());
                }
            }
            else if (this.state - 2 == this.count)    // ftn of qq mail
            {
                try
                {
                    HTMLDocument mainDoc = (HTMLDocument)axWebBrowserMain.Document;
                    IHTMLFramesCollection2 frames = (IHTMLFramesCollection2)mainDoc.frames;
                    if (frames == null)
                    {
                        return;
                    }
                    object idx = 2;
                    IHTMLWindow2 wnd = (IHTMLWindow2)frames.item(ref idx);
                    if (wnd == null)
                    {
                        return;
                    }
                    HTMLDocument doc = (HTMLDocument)wnd.document;    // iframe of folder_ftn
                    if (doc == null)
                    {
                        return;
                    }
                    IHTMLDOMChildrenCollection childNodes;
                    IHTMLDOMTextNode textNode;
                    IHTMLElementCollection links = (IHTMLElementCollection)doc.getElementsByTagName("a");
                    if (this.count == 0)   // get valid duration
                    {
                        foreach (HTMLAnchorElement a in links)
                        {
                            if (a.hasChildNodes())
                            {
                                childNodes = (IHTMLDOMChildrenCollection)a.childNodes;
                                if (childNodes.item(0) is IHTMLDOMTextNode)
                                {
                                    textNode = (IHTMLDOMTextNode)childNodes.item(0);
                                    if (textNode.data.IndexOf('天') != -1)
                                    {
                                        this.validDuration = textNode.data;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    // completion condition
                    string expiredDuration = "";
                    IHTMLElementCollection spans = (IHTMLElementCollection)doc.getElementsByTagName("span");
                    foreach (HTMLSpanElement s in spans)
                    {
                        if (s.hasChildNodes())
                        {
                            childNodes = (IHTMLDOMChildrenCollection)s.childNodes;
                            if (childNodes.item(0) is IHTMLDOMTextNode)
                            {
                                textNode = (IHTMLDOMTextNode)childNodes.item(0);
                                if (textNode.data.IndexOf('天') != -1 || textNode.data.IndexOf('時') != -1)
                                {
                                    expiredDuration = textNode.data;
                                    break;
                                }
                            }
                        }
                    }
                    if (expiredDuration == this.validDuration)
                    {
                        this.state = -1;
                        MessageBox.Show("續期完成, 請您留意下次續期時間!", "QQFtnExtend - 資訊", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    // end of completion condition
                    if (this.oneByOne)
                    {
                        foreach (HTMLAnchorElement a in links)
                        {
                            if (a.hasChildNodes())
                            {
                                childNodes = (IHTMLDOMChildrenCollection)a.childNodes;
                                if (childNodes.item(0) is IHTMLDOMTextNode)
                                {
                                    textNode = (IHTMLDOMTextNode)childNodes.item(0);
                                    if (textNode.data == "續期")
                                    {
                                        a.click();
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        foreach (HTMLAnchorElement a in links)
                        {
                            if (a.hasChildNodes())
                            {
                                childNodes = (IHTMLDOMChildrenCollection)a.childNodes;
                                if (childNodes.item(0) is IHTMLDOMTextNode)
                                {
                                    textNode = (IHTMLDOMTextNode)childNodes.item(0);
                                    if (textNode.data == "完成")
                                    {
                                        a.click();
                                        break;
                                    }
                                }
                            }
                        }
                        HTMLInputElement btnExtend = (HTMLInputElement)doc.getElementById("btn_extend1");
                        if (btnExtend == null)
                        {
                            return;
                        }
                        btnExtend.click();
                    }
                    this.aFtn.click();    // to make CompleteDocument event trigger again, though this is not the best way
                    this.count++;
                    this.state++;
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.ToString());
                }
            }
        }

        private void LoadConfig()
        {
            FileStream fs = new FileStream("settings.txt", FileMode.OpenOrCreate);
            StreamReader sr = new StreamReader(fs);
            int pos;
            string tempLine;
            // username
            tempLine = sr.ReadLine();
            if (tempLine == null)
            {
                sr.Close();
                fs.Close();
                this.DefaultConfig();
                return;
            }
            pos = tempLine.IndexOf(':');
            if (pos == -1)
            {
                sr.Close();
                fs.Close();
                this.DefaultConfig();
                return;
            }
            this.username = tempLine.Substring(pos + 1).Trim();
            // password
            tempLine = sr.ReadLine();
            if (tempLine == null)
            {
                sr.Close();
                fs.Close();
                this.DefaultConfig();
                return;
            }
            pos = tempLine.IndexOf(':');
            if (pos == -1)
            {
                sr.Close();
                fs.Close();
                this.DefaultConfig();
                return;
            }
            this.password = tempLine.Substring(pos + 1).Trim();
            // rememberMe
            tempLine = sr.ReadLine();
            if (tempLine == null)
            {
                sr.Close();
                fs.Close();
                this.DefaultConfig();
                return;
            }
            pos = tempLine.IndexOf(':');
            if (pos == -1)
            {
                sr.Close();
                fs.Close();
                this.DefaultConfig();
                return;
            }
            this.rememberMe = (tempLine.Substring(pos + 1).Trim() == "y") ? true : false;
            // pageSize
            tempLine = sr.ReadLine();
            if (tempLine == null)
            {
                sr.Close();
                fs.Close();
                this.DefaultConfig();
                return;
            }
            pos = tempLine.IndexOf(':');
            if (pos == -1)
            {
                sr.Close();
                fs.Close();
                this.DefaultConfig();
                return;
            }
            this.pageSize = Int32.Parse(tempLine.Substring(pos + 1).Trim());
            if (this.pageSize < 1) this.pageSize = 1;
            if (this.pageSize > 500) this.pageSize = 500;
            // extend oneByOne
            tempLine = sr.ReadLine();
            if (tempLine == null)
            {
                sr.Close();
                fs.Close();
                this.DefaultConfig();
                return;
            }
            pos = tempLine.IndexOf(':');
            if (pos == -1)
            {
                sr.Close();
                fs.Close();
                this.DefaultConfig();
                return;
            }
            this.oneByOne = (tempLine.Substring(pos + 1).Trim() == "y") ? true : false;
            //
            sr.Close();
            fs.Close();
        }

        private void DefaultConfig()
        {
            FileStream fs = new FileStream("settings.txt", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write("帳号: milkyjing01");
            sw.WriteLine();
            sw.Write("密碼: a123456");
            sw.WriteLine();
            sw.Write("記住登入狀态(y/n): n");
            sw.WriteLine();
            sw.Write("每頁顯示檔案數: 20");
            sw.WriteLine();
            sw.Write("檔案逐個續期(y/n): n");
            sw.Close();
            fs.Close();
            this.username = "milkyjing01";
            this.password = "a123456";
            this.rememberMe = false;
            this.pageSize = 20;
            this.oneByOne = false;
        }
    }
}
           

完整源代碼:

QQFtnExtend.rar

可執行程式:

QQ中轉續期工具V1.2.rar

繼續閱讀