Ejemplo n.º 1
0
        /**
         *  loc : Save Location
          * url : login URL
          * uid : User ID
          * pwd : User Pwd
          */
        public JobResult getCVS(string loc , string uid, string pwd , List<string> items , ref int n , ref int max , ref System.ComponentModel.BackgroundWorker worker)
        {
            //string searchPage = @"http://www.unfranchise.com.tw/index.cfm?action=meetings.nmSvSe&mtgSrchType=b";
            string searchPage = @"https://tw.unfranchise.com/index.cfm?action=meetings.unfMeetingSearchResults";

            JobResult results = new JobResult();
            // Login into Unfranchise System.
            bool LoginSuccess = false;
            //Login Unfranchise.com.tw
            for (int i = 1; i <= 3; i++)
            {
                LoginSuccess = login(uid, pwd);
                if (LoginSuccess == true)
                    break;
                else
                    Thread.Sleep(3000);
            }

            if (LoginSuccess == false)
            {
                log.Error("登入失敗,無法執行下載作業");
                results.IsDownloadSuccess = false;
                results.ErrMsg.Add("登入失敗,無法執行下載作業");
                return results;
            }

            try
            {
                //Get all Hidden input tag in Search Page
                string searchMainPage = @"https://tw.unfranchise.com/index.cfm?action=meetings.unfMeetingSearch";
                string strSPageCnt = HTTPTool.DownloadContentsUTF8(Request.CookieContainer, searchMainPage, string.Empty, 2);
                HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
                htmlDoc.LoadHtml(strSPageCnt);
                var inputs = htmlDoc.DocumentNode.Descendants("input");
                foreach (var input in inputs)
                {
                     if ((input.Attributes["type"].Value == "hidden") && (input.Attributes["name"] != null))
                         items.Add(input.Attributes["name"].Value + "=" + input.Attributes["value"].Value);
                }
            }
            catch (Exception ex9)
            {
                log.Fatal("Error at getCVS()-Step0- (Get All Hide Input Tag):" + ex9.Message);
                throw new Exception("Error at getCVS()-Step0(Get All Hide Input Tag):" + ex9.Message);
            }

            //Compose Post Data
            StringBuilder sbPostData = new StringBuilder();
            for (int i = 0; i < items.Count; i++)
            {
                if (i == items.Count - 1)
                    sbPostData.Append(items[i]);
                else
                    sbPostData.Append(items[i]).Append("&");
            }
            string strPostData = sbPostData.ToString();
            //HttpWebRequest Request;
            byte[] data = new ASCIIEncoding().GetBytes(strPostData);

            // get each meeting data from all links
            StringBuilder meetings = new StringBuilder();
            try
            {
                #region 保留程式碼
                //Request = (HttpWebRequest)WebRequest.Create(searchPage);
                //Request.Method = "POST";
                //Request.ContentType = "application/x-www-form-urlencoded";
                //Request.ContentLength = data.Length;
                //Request.KeepAlive = true;
                //Request.CookieContainer = m_Cookies;

                ////send the request Data
                //Stream newStream = Request.GetRequestStream();
                //newStream.Write(data, 0, data.Length);
                //newStream.Close();
                ////Get Response
                //HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();

                //using (StreamReader sr = new StreamReader(Response.GetResponseStream(), Encoding.UTF8))
                //{
                //    string tmpStr = sr.ReadToEnd();
                //    List<string> MeetingLinks = new List<string>();
                //    string[] aryValue = GetAttribute(tmpStr, "a", "href");
                //    for (int k = 0; k < aryValue.Length; k++)
                //    {
                //        //只取出我要的Meeting Links
                //        if (aryValue[k].IndexOf("index.cfm?action=meetings.nmMtgDtl&ID=") >= 0)
                //            MeetingLinks.Add(aryValue[k].Trim());
                //    }
                //}
                #endregion
                DateTime t1 = DateTime.Now;

                string SearchResults = HTTPTool.DownloadContentsUTF8(Request.CookieContainer, searchPage, strPostData, 1);
                DateTime t2 = DateTime.Now;
                double dd = new TimeSpan(t2.Ticks - t1.Ticks).TotalSeconds;

                List<string> MeetingLinks = new List<string>();
                //確認是否被導到登入頁
                if (SearchResults.IndexOf("顯示活動的") < 0)
                    log.Error("無法取得查詢結果-_-|||");
                else
                    results.IsDownloadSuccess = true;

                //string[] aryValue = GetAttribute(SearchResults, "a", "href");
                //for (int k = 0; k < aryValue.Length; k++)
                //{
                //    //只取出我要的Meeting Links
                //    if (aryValue[k].IndexOf("index.cfm?action=meetings.nmMtgDtl&ID=") >= 0)
                //        MeetingLinks.Add(aryValue[k].Trim());
                //}
                HtmlAgilityPack.HtmlDocument resultDoc = new HtmlAgilityPack.HtmlDocument();
                resultDoc.LoadHtml(SearchResults);
                var links = resultDoc.DocumentNode.Descendants("a");
                foreach (var link in links)
                {
                    if ((link.Attributes["class"]!= null) && (link.Attributes["href"] != null))
                       if(link.Attributes["class"].Value == "event")
                         MeetingLinks.Add(link.Attributes["href"].Value);
                }
                max = MeetingLinks.Count;
                worker.RunWorkerAsync(0);

                //Get all Meeting Links Detail Contents.
                for (int j = 0; j < MeetingLinks.Count; j++)
                {
                    n = j;
                    string url = @"https://tw.unfranchise.com/" + MeetingLinks[j];
                    string SingleInfo = HTTPTool.DownloadContentsUTF8(m_Cookies , url, "", 2);
                    string strMeetingInfo = getMeetingInfo2(SingleInfo);
                    if (strMeetingInfo != string.Empty)
                        meetings.AppendLine(getMeetingInfo2(SingleInfo));
                    else
                    {
                        results.ERRORURL.Add(url);
                        results.ErrMsg.Add("此筆資料查無其細節內容(" + url + "), 行事曆產生將跳過此筆資料。");
                        results.IsDownloadSuccess = false;
                    }
                    Application.DoEvents();
                }

            }
            catch (Exception ex)
            {
                log.Fatal("Error at getCVS()-Step1:" + ex.Message);
                throw new Exception("Error at getCVS()-Step1:" + ex.Message);
            }

            //Compose to csvFile
            string header = @"""主旨"",""開始日期"",""開始時間"",""結束日期"",""結束時間"",""全天"",""提醒開啟/關閉"",""提醒日期"",""提醒時間"",""會議召集人"",""出席者"",""列席者"",""會議資源"",""地點"",""忙閒狀態"",""私人"",""津貼"",""帳目資訊"",""敏感度"",""描述"",""優先順序"",""類別""";
            try
            {
                //清除不必要的字詞
                meetings.Replace("&nbsp;", "");
                //輸出成CSV檔
                using (StreamWriter sw = new StreamWriter(loc , false,Encoding.UTF8))
                {
                    sw.WriteLine(header);
                    sw.Write(meetings.ToString());
                }
            }
            catch (Exception ex1)
            {
                log.Fatal(@"Error at getCVS()-Step 2:" + ex1.Message);
                //throw new Exception("Error at getCVS()-Step 2:" + ex1.Message);
                results.IsDownloadSuccess = false;
                results.ErrMsg.Add(@"Error at getCVS()-Step 2:" + ex1.Message);
            }

            //背景作業設為可取消
            worker.WorkerSupportsCancellation = true;
            return results;
        }
Ejemplo n.º 2
0
 private string OutputMsg(JobResult j)
 {
     StringBuilder sb = new StringBuilder();
     if (j.ERRORURL.Count > 0)
     {
         sb.Append("\r\n").Append("無法下載內容的URL如下:").Append("\r\n");
         foreach (string s in j.ERRORURL)
         {
             sb.Append(s).Append(";").Append("\r\n");
         }
         sb.Append("----------------------------------------------------------------");
     }
     if (j.ErrMsg.Count > 0)
     {
         sb.Append("\r\n").Append("執行過程發生之錯誤訊息清單如下:").Append("\r\n");
         foreach (string s2 in j.ErrMsg)
         {
             sb.Append(s2).Append(";").Append("\r\n");
         }
     }
     return sb.ToString();
 }