void worker_DoWork(object sender, DoWorkEventArgs e) { Node_Common node = e.Argument as Node_Common; DownloaderInfo info = new DownloaderInfo(m_worker, e, null); node.ParsePage(info); }
public override void ParsePage(DownloaderInfo asyncInfo) { if (IsParsed) { return; } lock (m_lockObj) { App.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new ParsingStartedDelegate(ParsingStarted), null); string path = string.Empty; bool succeeded = false; Exception exception = null; try { succeeded = ParsePageInternal(asyncInfo, ref path); } catch (Exception exp) { exception = exp; } if (succeeded) { App.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new ParsingCompleteDelegate(ParsingComplete), path); } else { App.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new ParsingCompleteDelegate(ParsingFailed), null); } if (exception != null) { throw exception; } } }
public void Download(string destinationPath, DownloaderInfo asyncInfo) { lock (m_lockObj) { App.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new ParsingStartedDelegate(DownloadStarted), null); Exception exception = null; try { DownloadInternal(destinationPath, asyncInfo); } catch (Exception exp) { exception = exp; } App.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new ParsingCompleteDelegate(DownloadComplete), null); if (exception != null) { throw exception; } } }
private bool ParsePageInternal(DownloaderInfo asyncInfo) { if (IsParsed) { return(true); } if (asyncInfo.Worker.CancellationPending) { asyncInfo.WorkArgs.Cancel = true; return(false); } if (asyncInfo.ProgressCallback != null) { asyncInfo.ProgressCallback("Starting page download for folder '" + Name + "'.", 0, null); } StringBuilder sb = null; HttpWebResponse response = null; try { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; string finalURL = "https://api.gigahost123.com/api/listS3?bucket=mp3.gigahost123.com&path=" + HttpUtility.UrlEncode(this.URL); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(finalURL); response = (HttpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); sb = new StringBuilder(); Int32 bufSize = 1024 * 100; Int32 bytesRead = 0; byte[] buf = new byte[bufSize]; if (asyncInfo.Worker.CancellationPending) { response.Close(); asyncInfo.WorkArgs.Cancel = true; return(false); } if (asyncInfo.ProgressCallback != null) { asyncInfo.ProgressCallback("Reading page for folder '" + Name + "'.", 20, null); } long total = 0; if (responseStream.CanSeek) { total = responseStream.Length; } else { total = 500 * 1024; //assuming 500 KB } while ((bytesRead = responseStream.Read(buf, 0, bufSize)) != 0) { sb.Append(Encoding.UTF8.GetString(buf, 0, bytesRead)); if (asyncInfo.Worker.CancellationPending) { response.Close(); asyncInfo.WorkArgs.Cancel = true; return(false); } if (asyncInfo.ProgressCallback != null) { asyncInfo.ProgressCallback("Reading page for folder '" + Name + "'.", 20 + (((double)sb.Length / (double)total) * 30), null); } } response.Close(); } catch (Exception exp) { if (response != null) { response.Close(); } throw new Exception("Page downloading failed.\n " + exp.Message); } response = null; string json_data = sb.ToString(); if (asyncInfo.Worker.CancellationPending) { asyncInfo.WorkArgs.Cancel = true; return(false); } if (asyncInfo.ProgressCallback != null) { asyncInfo.ProgressCallback("Parsing page for folder '" + Name + "'.", 50, null); } try { Online_Page page = JsonConvert.DeserializeObject <Online_Page>(json_data); if (page.folders.Length > 0) { if (asyncInfo.Worker.CancellationPending) { asyncInfo.WorkArgs.Cancel = true; return(false); } if (asyncInfo.ProgressCallback != null) { asyncInfo.ProgressCallback("Parsing page for folder '" + Name + "'.", 60, null); } double count = 0; foreach (Online_Folder folder_entry in page.folders) { string folder_url = "/" + folder_entry.Prefix; string[] parts = folder_url.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); string folder_name = parts[parts.Length - 1]; if (string.IsNullOrEmpty(folder_url) || string.IsNullOrEmpty(folder_name)) { throw new Exception("Missing name/url for a folder."); } Node_Directory subfolder = new Node_Directory(folder_name, folder_url); subfolder.Parent = this; AddChild(subfolder); count++; if (asyncInfo.ProgressCallback != null) { asyncInfo.ProgressCallback("Parsing page for folder '" + Name + "'.", 60 + (((double)count / (double)page.folders.Length) * 20), subfolder); } if (asyncInfo.Worker.CancellationPending) { asyncInfo.WorkArgs.Cancel = true; return(false); } } } if (page.files.Length > 0) { if (asyncInfo.Worker.CancellationPending) { asyncInfo.WorkArgs.Cancel = true; return(false); } if (asyncInfo.ProgressCallback != null) { asyncInfo.ProgressCallback("Parsing page for folder '" + Name + "'.", 80, null); } double count = 0; foreach (Online_File file_entry in page.files) { string song_url = "/" + file_entry.Key; string[] parts = song_url.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); string song_name = parts[parts.Length - 1]; if (string.IsNullOrEmpty(song_url) || string.IsNullOrEmpty(song_name)) { throw new Exception("Missing name/url for a song."); } Node_Song song = new Node_Song(song_name, song_url); song.Parent = this; AddChild(song); count++; if (asyncInfo.ProgressCallback != null) { asyncInfo.ProgressCallback("Parsing page for folder '" + Name + "'.", 80 + (((double)count / (double)page.files.Length) * 20), song); } if (asyncInfo.Worker.CancellationPending) { asyncInfo.WorkArgs.Cancel = true; return(false); } } } } catch (Exception exp) { throw exp; } if (asyncInfo.ProgressCallback != null) { asyncInfo.ProgressCallback("Done processing page for folder '" + Name + "'.", 100, null); } return(true); }
private void DownloadInternal(string destinationPath, DownloaderInfo asyncInfo) { if (asyncInfo.Worker.CancellationPending) { asyncInfo.WorkArgs.Cancel = true; return; } if (asyncInfo.ProgressCallback != null) { asyncInfo.ProgressCallback("Initializing data download for song '" + Name + "'.", 0, null); } if (string.IsNullOrEmpty(Mp3Path)) { throw new Exception("No song to download."); } if (asyncInfo.Worker.CancellationPending) { asyncInfo.WorkArgs.Cancel = true; return; } string tempFile = string.Empty; try { tempFile = Path.GetTempFileName(); } catch (Exception exp) { throw new Exception("Temp file creation failed.\n " + exp.Message); } if (asyncInfo.Worker.CancellationPending) { try { File.Delete(tempFile); } catch (Exception) { } asyncInfo.WorkArgs.Cancel = true; return; } if (asyncInfo.ProgressCallback != null) { asyncInfo.ProgressCallback("Starting data download for song '" + Name + "'.", 10, null); } HttpWebResponse response = null; FileStream outputFile = null; try { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Mp3Path); response = (HttpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); if (asyncInfo.Worker.CancellationPending) { response.Close(); try { File.Delete(tempFile); } catch (Exception) { } asyncInfo.WorkArgs.Cancel = true; return; } if (asyncInfo.ProgressCallback != null) { asyncInfo.ProgressCallback("Creating output file for song '" + Name + "'.", 20, null); } outputFile = new FileStream(tempFile, FileMode.Create, FileAccess.Write); Int32 bufSize = 1024 * 100; Int32 bytesRead = 0; byte[] buf = new byte[bufSize]; long total = 0; if (responseStream.CanSeek) { total = responseStream.Length; } else { total = 10 * 1024 * 1024; //assuming 10 MB } while ((bytesRead = responseStream.Read(buf, 0, bufSize)) != 0) { outputFile.Write(buf, 0, bytesRead); if (asyncInfo.Worker.CancellationPending) { response.Close(); outputFile.Close(); try { File.Delete(tempFile); } catch (Exception) { } asyncInfo.WorkArgs.Cancel = true; return; } if (asyncInfo.ProgressCallback != null) { asyncInfo.ProgressCallback("Downloading data for song '" + Name + "'.", 20 + (((double)outputFile.Length / (double)total) * 80), null); } } outputFile.Close(); response.Close(); } catch (Exception exp) { if (response != null) { response.Close(); } if (outputFile != null) { outputFile.Close(); try { File.Delete(tempFile); } catch (Exception) { } } throw new Exception("Song download from '" + URL + "' failed.\n " + exp.Message.Replace("\n", "\n ")); } response = null; outputFile = null; if (asyncInfo.Worker.CancellationPending) { try { File.Delete(tempFile); } catch (Exception) { } asyncInfo.WorkArgs.Cancel = true; return; } string pathOnDisk = destinationPath + "\\" + FullPath; string pathOfParent = Path.GetDirectoryName(pathOnDisk); try { System.IO.Directory.CreateDirectory(pathOfParent); } catch (Exception exp) { throw new Exception("Destination directory (" + pathOfParent + ") creation failed.\n " + exp.Message); } if (asyncInfo.Worker.CancellationPending) { try { File.Delete(tempFile); } catch (Exception) { } asyncInfo.WorkArgs.Cancel = true; return; } try { //delete the existing file if it exists File.Delete(pathOnDisk); } catch (Exception) { //don't need to do anything here } try { File.Move(tempFile, pathOnDisk); } catch (Exception exp) { throw new Exception("Copying of temp file to final file failed.\n " + exp.Message); } if (asyncInfo.ProgressCallback != null) { asyncInfo.ProgressCallback("Done downloading for song '" + Name + "'.", 100, null); } }
private bool ParsePageInternal(DownloaderInfo asyncInfo, ref string path) { if (IsParsed) { return(true); } if (asyncInfo.Worker.CancellationPending) { asyncInfo.WorkArgs.Cancel = true; return(false); } if (asyncInfo.ProgressCallback != null) { asyncInfo.ProgressCallback("Starting page download for song '" + Name + "'.", 0, null); } string mp3path = string.Empty; NotifyPropertyChanged("Mp3Path"); StringBuilder sb = null; HttpWebResponse response = null; try { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; string finalURL = "https://api.gigahost123.com/api/getDownloadUrl?bucket=mp3.gigahost123.com&key=" + HttpUtility.UrlEncode(this.URL); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(finalURL); response = (HttpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); sb = new StringBuilder(); Int32 bufSize = 1024 * 100; Int32 bytesRead = 0; byte[] buf = new byte[bufSize]; if (asyncInfo.Worker.CancellationPending) { response.Close(); asyncInfo.WorkArgs.Cancel = true; return(false); } if (asyncInfo.ProgressCallback != null) { asyncInfo.ProgressCallback("Reading page for song '" + Name + "'.", 20, null); } long total = 0; if (responseStream.CanSeek) { total = responseStream.Length; } else { total = 500 * 1024; //assuming 500 KB } while ((bytesRead = responseStream.Read(buf, 0, bufSize)) != 0) { sb.Append(Encoding.UTF8.GetString(buf, 0, bytesRead)); if (asyncInfo.Worker.CancellationPending) { response.Close(); asyncInfo.WorkArgs.Cancel = true; return(false); } if (asyncInfo.ProgressCallback != null) { asyncInfo.ProgressCallback("Reading page for song '" + Name + "'.", 20 + (((double)sb.Length / (double)total) * 30), null); } } response.Close(); } catch (Exception exp) { if (response != null) { response.Close(); } throw new Exception("Page downloading failed.\n " + exp.Message); } response = null; string json_data = sb.ToString(); if (asyncInfo.Worker.CancellationPending) { asyncInfo.WorkArgs.Cancel = true; return(false); } if (asyncInfo.ProgressCallback != null) { asyncInfo.ProgressCallback("Parsing page for song '" + Name + "'.", 50, null); } try { Online_Song song = JsonConvert.DeserializeObject <Online_Song>(json_data); if (asyncInfo.Worker.CancellationPending) { asyncInfo.WorkArgs.Cancel = true; return(false); } mp3path = song.url; NotifyPropertyChanged("Mp3Path"); if (asyncInfo.Worker.CancellationPending) { asyncInfo.WorkArgs.Cancel = true; return(false); } if (string.IsNullOrEmpty(mp3path)) { throw (new Exception("No song link found.")); } path = mp3path; } catch (Exception exp) { throw exp; } if (asyncInfo.ProgressCallback != null) { asyncInfo.ProgressCallback("Done processing page for song '" + Name + "'.", 100, null); } return(true); }
private void DownloadWorker_DoWork(object sender, DoWorkEventArgs workArgs) { errorFolders.Clear(); errorSongs.Clear(); List <Node_Common> downloadNodes = new List <Node_Common>(); foreach (Node_Common node in (workArgs.Argument as ObservableCollection <Node_Common>)) { downloadNodes.Add(node); } downloadNodes.Reverse(); if (m_downloadWorker.CancellationPending) { workArgs.Cancel = true; return; } ReportProgressPrimary("Starting downloads.", 0); Stack <Node_Common> nodesToProcess = new Stack <Node_Common>(); foreach (Node_Common node in downloadNodes) { if (m_downloadWorker.CancellationPending) { workArgs.Cancel = true; return; } m_logger.Addtext("Got download node - " + node.Name + " (" + node.URL + ")\n"); nodesToProcess.Push(node); } if (m_downloadWorker.CancellationPending) { workArgs.Cancel = true; return; } double count = 0; double total = nodesToProcess.Count; while (nodesToProcess.Count > 0) { Node_Common node = nodesToProcess.Pop(); if (m_downloadWorker.CancellationPending) { workArgs.Cancel = true; return; } if ((node.NodeType == Node_Common.Type.T_SONG) && (node.IsParsed)) { Node_Song song = node as Node_Song; m_logger.Addtext("Downloading song - " + song.Name + " (" + song.Mp3Path + ")\n"); ReportProgressPrimary("Downloading Song - " + song.Name, 10 + ((count / total) * 90)); count++; try { DownloaderInfo info = new DownloaderInfo(m_downloadWorker, workArgs, ReportProgressSecondary); song.Download(m_destinationPath, info); RemoveNodeFromUIList(song); } catch (Exception exp) { AddFailureNode(song, exp.Message); continue; } } else { m_logger.Addtext("Processing subnodes of - " + node.Name + "\n"); ReportProgressPrimary("Gathering song list - " + node.Name, 10 + ((count / total) * 90)); count++; try { DownloaderInfo info = new DownloaderInfo(m_downloadWorker, workArgs, ReportProgressSecondary); node.ParsePage(info); RemoveNodeFromUIList(node); } catch (Exception exp) { AddFailureNode(node, exp.Message); continue; } if (node.NodeType == Node_Common.Type.T_DIR) { List <Node_Common> subNodes = new List <Node_Common>(); foreach (Node_Common subnode in (node as Node_Directory).Children) { if (m_downloadWorker.CancellationPending) { workArgs.Cancel = true; return; } m_logger.Addtext("Added subnode for download - " + subnode.Name + " (" + subnode.URL + ")\n"); subNodes.Add(subnode); } subNodes.Reverse(); foreach (Node_Common subnode in subNodes) { nodesToProcess.Push(subnode); AddNodeToUIList(subnode); } total += subNodes.Count; } else if (node.NodeType == Node_Common.Type.T_SONG) { m_logger.Addtext("Added song for download - " + node.Name + " (" + (node as Node_Song).Mp3Path + ")\n"); nodesToProcess.Push(node); AddNodeToUIList(node); total++; } } } ReportProgressPrimary("Done downloading songs.", 100); workArgs.Result = true; }
public virtual void ParsePage(DownloaderInfo asyncInfo) { throw new NotImplementedException(); }