Ejemplo n.º 1
0
        /// <summary>
        /// Get all mp3 files in one library
        /// </summary>
        private static void getMp3FilesOfOneLibrary(Library library)
        {
            if (library.Mp3Files == null || library.Mp3Files.Count == 0)
            {
                Console.WriteLine("Yhtään Mp3 tiedostoa ei löytynyt.");
                return;
            }
            for (int i = library.Mp3Files.Count - 1; i >= 0; i--)
            {
                Mp3File mp3File  = library.Mp3Files[i];
                string  fileName = mp3File.Title;
                string  fileID   = mp3File.ID;
                double  length   = mp3File.Length;
                string  folder   = library.Title;

                if (!checkDoesMp3FileAlreadyExist(fileID, fileName, folder))
                {
                    if (length > 0)
                    {
                        Console.WriteLine(string.Format("Ladataan tiedostoa: {0} ({1} MB)...", fileName, Math.Round(length, 2)));
                    }
                    else
                    {
                        Console.WriteLine(string.Format("Ladataan tiedostoa: {0}...", fileName));
                    }

                    // Actual download
                    downloadOneMp3File(library, mp3File);
                }
                else
                {
                    Console.WriteLine(string.Format("Tiedosto on jo ladattuna: {0}", fileName));
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Download one Mp3 file
        /// </summary>
        private static void downloadOneMp3File(Library library, Mp3File mp3File)
        {
            string fullFolder = BaseFolderPath + library.Title;
            string fileName   = mp3File.Title;
            string url        = mp3File.Url;
            string id         = mp3File.ID;
            string fileNumber = getNextNumber(fullFolder);

            string fullFileName = string.Format("{0}\\{1} {2} ({3}).mp3", fullFolder, fileNumber, fileName, id);

            using (WebClient client = new WebClient()) {
                try {
                    Debug.WriteLine("> Ladataan " + url + "; " + fullFileName);
                    client.DownloadFile(url, fullFileName);
                    howManyNewFilesLoaded++;
                } catch (Exception ex) {
                    howManyErros++;
                    Debug.WriteLine("Error: " + ex.ToString());
                    Console.WriteLine("Error: " + ex.ToString());
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Get list of mp3 files in one library
        /// </summary>
        private void getMp3ListOfOneLibrary()
        {
            XmlTextReader rssReader;
            XmlDocument   rssDoc;
            XmlNode       nodeRss     = null;
            XmlNode       nodeChannel = null;
            XmlNode       nodeItem;

            Libraries[libraryIndex].Mp3Files = new List <Mp3File>();

            string fullUrl = string.Format(feedUrl, Libraries[libraryIndex].LibraryID);

            rssReader = new XmlTextReader(fullUrl);
            rssDoc    = new XmlDocument();
            try
            {
                rssDoc.Load(rssReader);
            }
            catch (Exception ex)
            {
                return;
            }

            // Loop for the <rss> tag
            for (int i = 0; i < rssDoc.ChildNodes.Count; i++)
            {
                // If it is the rss tag
                if (rssDoc.ChildNodes[i].Name == "rss")
                {
                    // <rss> tag found
                    nodeRss = rssDoc.ChildNodes[i];
                }
            }

            // Loop for the <channel> tag
            for (int i = 0; i < nodeRss.ChildNodes.Count; i++)
            {
                // If it is the channel tag
                if (nodeRss.ChildNodes[i].Name == "channel")
                {
                    // <channel> tag found
                    nodeChannel = nodeRss.ChildNodes[i];
                }
            }

            // Loop for the <title>, <link>, <description> and all the other tags
            for (int i = 0; i < nodeChannel.ChildNodes.Count; i++)
            {
                // If it is the item tag, then it has children tags which we will add as items to the ListView
                if (nodeChannel.ChildNodes[i].Name == "item")
                {
                    nodeItem = nodeChannel.ChildNodes[i];

                    // Get full path and filename etc.

                    XmlNode fileNameNode = nodeItem["enclosure"];
                    if (fileNameNode != null)
                    {
                        string fileNameUrl  = fileNameNode.Attributes["url"].Value.ToString();
                        string lengthString = "0";
                        if (fileNameNode.Attributes["length"] != null)
                        {
                            lengthString = fileNameNode.Attributes["length"].Value.ToString();
                        }
                        long lengthLong = 0;
                        long.TryParse(lengthString, out lengthLong);
                        double length = ((double)lengthLong) / 1024 / 1024;

                        string[] parts   = fileNameUrl.Split('?');
                        string   longUrl = parts[0];

                        string[] parts2     = longUrl.Split('/');
                        string   fileIDBase = "-" + parts2[parts2.Length - 1];
                        string[] parts3     = fileIDBase.Split('-');
                        string   fileID     = parts3[parts3.Length - 1];

                        fileID = fileID.Replace(".mp3", "");

                        // Make new Mp3File object

                        Mp3File m = new Mp3File();
                        m.Title = removeInvalidFileNameChars(nodeItem["title"].InnerText);
                        string toFind = Libraries[libraryIndex].Title + " - ";
                        if (m.Title.StartsWith(toFind))
                        {
                            m.Title = m.Title.Remove(0, toFind.Length);
                        }
                        m.Url    = longUrl;
                        m.Length = length;
                        m.ID     = fileID;

                        Libraries[libraryIndex].Mp3Files.Add(m);
                    }
                }
            }
        }