private void ProcessQueue() { if (downloadQueue.Count > 0) { var downloadParams = (DownloadParams)downloadQueue.Dequeue(); CurrentDownloadFile = SavePath + Helpers.GetDirectorySeparator() + downloadParams.FileName; if (AsyncMode) { wc.DownloadFileAsync(new Uri(downloadParams.ToString()), CurrentDownloadFile); } else { wc.DownloadFile(new Uri(downloadParams.ToString()), CurrentDownloadFile); // since we are in blocking mode we have to set success explicitly DownloadedFiles.Last().Success = true; ProcessQueue(); } } else { if (OnDownloadAllCompletedEvent != null) { OnDownloadAllCompletedEvent(this, new EventArgs()); } } }
public void Download() { try { if (DownloadParams.ToDate < DownloadParams.FromDate) { throw new PSEGetException("Invalid date range."); } DownloadedFiles.Clear(); //this.FailedDownloadFiles.Clear(); DateTime reportDate = DownloadParams.FromDate; do { var downloadParams = (DownloadParams)DownloadParams.Clone(); downloadParams.FileName = downloadParams.FileName.Replace("%dd", String.Format("{0:00}", reportDate.Day)); downloadParams.FileName = downloadParams.FileName.Replace("%mm", String.Format("{0:00}", reportDate.Month)); downloadParams.FileName = downloadParams.FileName.Replace("%yyyy", String.Format("{0:00}", reportDate.Year)); if (reportDate.DayOfWeek == DayOfWeek.Saturday || reportDate.DayOfWeek == DayOfWeek.Sunday) { reportDate = reportDate.AddDays(1); continue; } downloadQueue.Enqueue(downloadParams); reportDate = reportDate.AddDays(1); } while (reportDate <= DownloadParams.ToDate); if (downloadQueue.Count > 0) { if (OnStartDownloadProcess != null) { OnStartDownloadProcess(this, null); } } else { throw new PSEGetException("There were no pse reports found in the dates you specified."); } ProcessQueue(); } catch (Exception e) { OnReportDownloadCompletedEvent(this, new AsyncCompletedEventArgs(e, false, null)); if (downloadQueue.Count == 0) { OnDownloadAllCompletedEvent(this, new EventArgs()); } } }
private void ProcessQueue() { if (downloadQueue.Count > 0) { var downloadParams = (DownloadParams)downloadQueue.Dequeue(); int count = downloadCount - downloadQueue.Count; CurrentDownloadFile = SavePath + Helpers.GetDirectorySeparator() + downloadParams.FileName; if (AsyncMode) { wc.DownloadFileAsync(new Uri(downloadParams.ToString()), CurrentDownloadFile); } else { try { float progress = count > 0 ? ((float)count / (float)downloadCount) * 100f : 0f; Console.Write($"Downloading {Path.GetFileNameWithoutExtension(CurrentDownloadFile)} [{count}/{downloadCount}] ({progress.ToString("0")}%) ... "); wc.DownloadFile(new Uri(downloadParams.ToString()), CurrentDownloadFile); // since we are in blocking mode we have to set success explicitly DownloadedFiles.Last().Success = true; Console.WriteLine($"SUCCESS!"); } catch (WebException ex) { Console.WriteLine($"\nERROR: {ex.Message} ({Path.GetFileNameWithoutExtension(CurrentDownloadFile)})"); if (string.IsNullOrEmpty(errorLogs.Where(x => x.Contains(Path.GetFileNameWithoutExtension(CurrentDownloadFile))).FirstOrDefault())) { errorLogs.Add(Path.GetFileNameWithoutExtension(CurrentDownloadFile) + " - " + ex.Message); } } ProcessQueue(); } } else { if (OnDownloadAllCompletedEvent != null) { OnDownloadAllCompletedEvent(this, new EventArgs()); } errorLogs = errorLogs.OrderBy(x => x).ToList(); File.WriteAllLines("logs.txt", errorLogs.ToArray()); } }
public void Download() { if (DownloadParams.ToDate < DownloadParams.FromDate) { throw new PSEGetException("Invalid date range."); } DownloadedFiles.Clear(); downloadQueue.Clear(); DateTime reportDate = DownloadParams.FromDate; do { var downloadParams = (DownloadParams)DownloadParams.Clone(); downloadParams.FileName = downloadParams.FileName.Replace("%dd", String.Format("{0:00}", reportDate.Day)); downloadParams.FileName = downloadParams.FileName.Replace("%mm", String.Format("{0:00}", reportDate.Month)); downloadParams.FileName = downloadParams.FileName.Replace("%yyyy", String.Format("{0:00}", reportDate.Year)); if (reportDate.DayOfWeek == DayOfWeek.Saturday || reportDate.DayOfWeek == DayOfWeek.Sunday) { reportDate = reportDate.AddDays(1); continue; } downloadQueue.Enqueue(downloadParams); reportDate = reportDate.AddDays(1); } while (reportDate <= DownloadParams.ToDate); if (downloadQueue.Count > 0) { } else { throw new PSEGetException("Nothing to download"); } ProcessQueue(); }
/// <summary> /// Deserializes the object from XML. /// </summary> /// <param name="reader">The xml reader from which to deserialize the object.</param> public void ReadXml(XmlReader reader) { bool booIsEmpty = reader.IsEmptyElement; if (booIsEmpty) { return; } reader.ReadStartElement(); reader.ReadStartElement("sourceUri"); XmlSerializer xsrSerializer = new XmlSerializer(typeof(string)); SourceUri = new Uri((string)xsrSerializer.Deserialize(reader)); reader.ReadEndElement(); reader.ReadStartElement("sourcePath"); xsrSerializer = new XmlSerializer(typeof(string)); SourcePath = (string)xsrSerializer.Deserialize(reader); reader.ReadEndElement(); booIsEmpty = reader.IsEmptyElement; try { reader.ReadStartElement("sourceName"); if (!booIsEmpty) { while (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == "string") { xsrSerializer = new XmlSerializer(typeof(string)); SourceName.Add((string)xsrSerializer.Deserialize(reader)); } reader.ReadEndElement(); } } catch { } finally { if (SourceName.Count == 0) { SourceName.Add("Default"); } } reader.ReadStartElement("defaultSourcePath"); xsrSerializer = new XmlSerializer(typeof(string)); DefaultSourcePath = (string)xsrSerializer.Deserialize(reader); reader.ReadEndElement(); try { reader.ReadStartElement("status"); xsrSerializer = new XmlSerializer(typeof(TaskStatus)); Status = (TaskStatus)xsrSerializer.Deserialize(reader); reader.ReadEndElement(); } catch { Status = TaskStatus.Paused; } booIsEmpty = reader.IsEmptyElement; reader.ReadStartElement("downloadFiles"); if (!booIsEmpty) { while (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == "file") { DownloadFiles.Add(new Uri(reader["path"])); booIsEmpty = reader.IsEmptyElement; reader.ReadStartElement("file"); if (!booIsEmpty) { reader.ReadEndElement(); } } reader.ReadEndElement(); } booIsEmpty = reader.IsEmptyElement; reader.ReadStartElement("downloadedFiles"); if (!booIsEmpty) { while (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName == "file") { DownloadedFiles.Add(reader["path"]); booIsEmpty = reader.IsEmptyElement; reader.ReadStartElement("file"); if (!booIsEmpty) { reader.ReadEndElement(); } } reader.ReadEndElement(); } reader.ReadEndElement(); }
public void DownloadFile(string resource, string saveAs) { DownloadedFiles.Add(new DownloadedFile(resource, saveAs, Cookies)); }