示例#1
0
        private void process_Exited(object sender, EventArgs e)
        {
            string html         = string.Empty;
            string hashCode     = string.Empty;
            string htmlPath     = string.Empty;
            string realHashCode = string.Empty;

            try
            {
                // Download main pictures
                Process process = (Process)sender;
                htmlPath = process.StartInfo.Arguments.Split(' ')[3] + ".html";

                hashCode = Path.GetFileNameWithoutExtension(htmlPath);

                realHashCode = hashCode.Replace('_', '-');

                using (StreamReader sw = new StreamReader(File.OpenRead(htmlPath)))
                {
                    html = sw.ReadToEnd();
                }
            }
            catch
            {
                SetWebSiteInfoStatus(realHashCode, StatusCode.Failed);
                return;
            }

            if (string.IsNullOrEmpty(html) == false)
            {
                try
                {
                    WebSiteInfo webSiteInfo = GetWebSiteInfoByHashCode(realHashCode);

                    logger.Info(string.Format("Read html {0} finished", webSiteInfo.Url));

                    DownLoad(html, webSiteInfo);

                    SetWebSiteInfoStatus(realHashCode, StatusCode.Finish);

                    //Kiss this process
                    KillProcess(realHashCode);
                }
                catch
                {
                    KillProcess(realHashCode);
                    SetWebSiteInfoStatus(realHashCode, StatusCode.Failed);
                    return;
                }

                File.Delete(htmlPath);
            }
            else
            {
                logger.Info(string.Format("Read html failed."));
                KillProcess(realHashCode);
                SetWebSiteInfoStatus(realHashCode, StatusCode.Failed);
                return;
            }
        }
示例#2
0
        private void SetWebSiteInfoStatus(string hashCode, StatusCode status)
        {
            WebSiteInfo webSiteInfo = GetWebSiteInfoByHashCode(hashCode);

            if (webSiteInfo != null)
            {
                webSiteInfo.Status = status;
            }
        }
示例#3
0
        private void KillProcess(string realHashCode)
        {
            WebSiteInfo webSiteInfo = GetWebSiteInfoByHashCode(realHashCode);

            if (webSiteInfo != null)
            {
                StopTask(webSiteInfo);
            }
        }
示例#4
0
 internal static void ClearDownLoader()
 {
     WebSiteInfo[] list = new WebSiteInfo[processPool.Keys.Count];
     processPool.Keys.CopyTo(list, 0);
     foreach (WebSiteInfo webSiteInfo in list)
     {
         StopTask(webSiteInfo);
         webSiteInfo.Status = StatusCode.Wait;
     }
 }
示例#5
0
 private static void KillProcess(WebSiteInfo webSiteInfo, bool enableRaisingEvernts)
 {
     if (processPool.ContainsKey(webSiteInfo))
     {
         Process process = processPool[webSiteInfo].Process;
         process.EnableRaisingEvents = enableRaisingEvernts;
         if (process != null && process.HasExited == false)
         {
             process.Kill();
         }
         processPool.Remove(webSiteInfo);
     }
 }
示例#6
0
        private WebSiteInfo GetWebSiteInfoByHashCode(string hashCode)
        {
            WebSiteInfo result = null;

            foreach (WebSiteInfo webSiteInfo in this.downloadList)
            {
                if (webSiteInfo.GetHashCode().ToString() == hashCode)
                {
                    result = webSiteInfo;
                    break;
                }
            }
            return(result);
        }
示例#7
0
        private string CalculateFolderName(WebSiteInfo webSiteInfo)
        {
            string basicName = webSiteInfo.Url.GetHashCode().ToString().Replace('-', '_');

            string[] folders = Directory.GetDirectories(PRODUCTS_FOLDER);

            // whether it's exsited.
            string folderName = ExistedFolder(basicName, folders);

            if (folderName == null)
            {
                string preFolderName = GetPreFolderName(folders);
                return(Path.Combine(PRODUCTS_FOLDER, preFolderName + basicName));
            }
            else
            {
                return(folderName);
            }
        }
示例#8
0
        protected override void DownLoad(string html, WebSiteInfo webSiteInfo)
        {
            logger.Info(string.Format("Create folders for website: {0}", webSiteInfo.Url));
            DirectoryInfo directory;

            lock (locker)
            {
                string folderName = CalculateFolderName(webSiteInfo);
                directory = new DirectoryInfo(folderName);
                if (directory.Exists == true)
                {
                    directory.Delete(true);
                }
                directory.Create();
            }

            DirectoryInfo mainDirectory = new DirectoryInfo(Path.Combine(directory.FullName, "main"));

            mainDirectory.Create();

            DirectoryInfo ditailsDirectory = new DirectoryInfo(Path.Combine(directory.FullName, "details"));

            ditailsDirectory.Create();

            string titile = TextUtility.RemoveSpecialChar(GetTitle(html));

            File.Create(Path.Combine(directory.FullName, titile + ".txt"));

            logger.Info(string.Format("Start download main pictures for website: {0}", webSiteInfo.Url));

            // Download main pictures
            DownLoadMainPictures(html, mainDirectory);

            logger.Info(string.Format("Start download detail pictures for website: {0}", webSiteInfo.Url));

            DownLoadDetailPicture(html, ditailsDirectory);

            logger.Info(string.Format("Download pictures finished for website: {0}", webSiteInfo.Url));
        }
示例#9
0
 public DownLoaderProcessInfo(WebSiteInfo webSiteInfo, Process process, DateTime time)
 {
     this.webSiteInfo = webSiteInfo;
     this.process     = process;
     this.time        = time;
 }
示例#10
0
 protected abstract void DownLoad(string html, WebSiteInfo webSiteInfo);
示例#11
0
 internal static void StopTask(WebSiteInfo webSiteInfo)
 {
     KillProcess(webSiteInfo, false);
 }