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; } }
private void SetWebSiteInfoStatus(string hashCode, StatusCode status) { WebSiteInfo webSiteInfo = GetWebSiteInfoByHashCode(hashCode); if (webSiteInfo != null) { webSiteInfo.Status = status; } }
private void KillProcess(string realHashCode) { WebSiteInfo webSiteInfo = GetWebSiteInfoByHashCode(realHashCode); if (webSiteInfo != null) { StopTask(webSiteInfo); } }
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; } }
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); } }
private WebSiteInfo GetWebSiteInfoByHashCode(string hashCode) { WebSiteInfo result = null; foreach (WebSiteInfo webSiteInfo in this.downloadList) { if (webSiteInfo.GetHashCode().ToString() == hashCode) { result = webSiteInfo; break; } } return(result); }
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); } }
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)); }
public DownLoaderProcessInfo(WebSiteInfo webSiteInfo, Process process, DateTime time) { this.webSiteInfo = webSiteInfo; this.process = process; this.time = time; }
protected abstract void DownLoad(string html, WebSiteInfo webSiteInfo);
internal static void StopTask(WebSiteInfo webSiteInfo) { KillProcess(webSiteInfo, false); }