public void Enqueue(DownloadState d, int Priority) { this.prioritylist[Priority - 1].Enqueue(d); lock (this.flatlist) { this.flatlist.Add(d); } }
public void AddFile(PostItem p, string url, string filename, Downloader.Callback <PostItem> callback) { lock (this.Files) { DownloadState downloadState = new DownloadState(Downloader.DownloadType.File, p, url, filename, null, callback); this.Files.Enqueue(downloadState, 5); } this.StartDownload(); }
public void DownloadPostHTML(Post post, Downloader.Callback <PostItem> callback) { int num = 3; if (post.Name == null) { num = 1; } PostItemHtml postItemHtml = new PostItemHtml(post, post.Url); DownloadState downloadState = new DownloadState(Downloader.DownloadType.String, postItemHtml, post.Url, null, null, callback); lock (this.Files) { this.Files.Enqueue(downloadState, num); } this.StartDownload(); }
public DownloadState Dequeue() { DownloadState downloadState = null; for (int i = 0; i < (int)this.prioritylist.Length; i++) { Queue <DownloadState> downloadStates = this.prioritylist[i]; if (downloadStates.Count > 0) { downloadState = downloadStates.Dequeue(); lock (this.flatlist) { this.flatlist.Remove(downloadState); } return(downloadState); } } return(downloadState); }
private void client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e) { DownloadState userState = (DownloadState)e.UserState; if (e.Error == null) { try { userState.postItem.Post.Website.Cookies = (sender as MyWebClient).CookieContainer; if (userState.type == Downloader.DownloadType.String) { byte[] result = e.Result; string str = (new UTF8Encoding()).GetString(result); if (str.Length >= 3 || userState.postItem.DownloadErrorCount != 0) { userState.postItem.Post.HTML = str; } else { PostItem downloadErrorCount = userState.postItem; downloadErrorCount.DownloadErrorCount = downloadErrorCount.DownloadErrorCount + 1; this.AddFile(userState.postItem, userState.postItem.URL, userState.Filename, userState.callback); return; } } else if (userState.type == Downloader.DownloadType.File) { using (FileStream fileStream = new FileStream(userState.Filename, FileMode.Create, FileAccess.Write)) { fileStream.Write(e.Result, 0, (int)e.Result.Length); } lock (userState.postItem.Post.Website) { Website website = userState.postItem.Post.Website; website.ViewnewItems = website.ViewnewItems - 1; userState.postItem.Post.Website.Viewstatus = string.Concat("Downloaded ", Path.GetFileName(userState.Filename)); userState.postItem.Post.Website.LastLoadedImage = userState.Filename; if (userState.postItem is PostItemPhoto) { Website photoCount = userState.postItem.Post.Website; photoCount.PhotoCount = photoCount.PhotoCount + 1; } else if (userState.postItem is PostItemVideo) { Website videoCount = userState.postItem.Post.Website; videoCount.VideoCount = videoCount.VideoCount + 1; } } if (userState.Client.ResponseHeaders != null) { try { string str1 = userState.Client.ResponseHeaders.Get("Last-Modified"); if (str1 != null) { File.SetCreationTime(userState.Filename, DateTime.Parse(str1)); } } catch (Exception exception) { Console.WriteLine(exception.Message); } } } } catch (Exception exception1) { Console.WriteLine(exception1.Message); } } else { WebException error = e.Error as WebException; if (error != null && error.Response != null) { if (userState.postItem.DownloadErrorCount != 0) { this.ErrorList.Add(userState); lock (userState.postItem.Post.Website) { Website viewnewItems = userState.postItem.Post.Website; viewnewItems.ViewnewItems = viewnewItems.ViewnewItems - 1; } } else if (userState.postItem is PostItemPhoto) { string thumb = (userState.postItem as PostItemPhoto).Thumb; PostItem postItem = userState.postItem; postItem.DownloadErrorCount = postItem.DownloadErrorCount + 1; this.AddFile(userState.postItem, thumb, userState.Filename, userState.callback); } } } if (userState.callback != null) { userState.callback(userState.postItem); } this.StartDownload(); }
private void StartDownload() { if (this.status == 0) { return; } if (this.WC.Count > this.MaxConcurrentDownloads) { for (int i = 0; i < (int)this.WC.ToArray().Length; i++) { MyWebClient array = this.WC.ToArray()[i]; if (!array.IsBusy) { array.Dispose(); this.WC.Remove(array); Console.WriteLine("Deleted webclient"); } } } if (this.WC.Count < this.MaxConcurrentDownloads) { MyWebClient myWebClient = new MyWebClient(); myWebClient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(this.client_DownloadDataCompleted); this.WC.Add(myWebClient); Console.WriteLine("New webclient"); } MyWebClient[] myWebClientArray = this.WC.ToArray(); for (int j = 0; j < (int)myWebClientArray.Length; j++) { MyWebClient cookies = myWebClientArray[j]; lock (cookies) { if (!cookies.IsBusy) { lock (this.Files) { if (this.Files.Count <= 0) { bool flag = true; for (int k = 0; k < (int)this.WC.ToArray().Length; k++) { if (this.WC.ToArray()[k].IsBusy) { flag = false; } } if (flag) { this.OnDownloadFinished(EventArgs.Empty); } } else { DownloadState cookieContainer = this.Files.Dequeue(); cookieContainer.Client = cookies; if (cookieContainer.postItem.Post.Website.Cookies == null) { cookieContainer.postItem.Post.Website.Cookies = new CookieContainer(); } cookies.CookieContainer = cookieContainer.postItem.Post.Website.Cookies; try { cookies.DownloadDataAsync(cookieContainer.Uri, cookieContainer); } catch (Exception exception) { Thread.Sleep(1000); this.Files.Enqueue(cookieContainer, 1); Console.WriteLine(string.Concat("Requeued ", cookieContainer.Uri)); } } } } } } }