Ejemplo n.º 1
0
 private void ParsingFailed(object result)
 {
     IsParsing = false;
     IsParsed  = false;
     if (m_children.Count == 0)
     {
         Node_Song dummySong = new Node_Song("Dummy", "Dummy");
         m_children.Add(dummySong);
     }
 }
Ejemplo n.º 2
0
        public override void Init()
        {
            m_children.Clear();
            m_needToPopulate = false;

            Node_Song dummySong = new Node_Song("Dummy", "Dummy");

            dummySong.Init();
            m_children.Add(dummySong);

            if (m_childrenView == null)
            {
                m_childrenView         = new CollectionViewSource();
                m_childrenView.Source  = m_children;
                m_childrenView.Filter += new FilterEventHandler(childrenView_Filter);
            }
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        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;
        }