Advertisement
tankcr

Untitled

May 1st, 2014
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.70 KB | None | 0 0
  1. using System;
  2. using iTunesLib;
  3. using System.Xml;
  4. using System.Xml.Linq;
  5. using System.IO;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text.RegularExpressions;
  9. using TagLib;
  10. using ITLRU;
  11. using System.Xml.Serialization;
  12. namespace BackupLib
  13. {
  14.     class Program
  15.     {
  16.       static void Main(string[] args)
  17.         {
  18.             // iTunes classes
  19.             iTunesAppClass itunes = new iTunesAppClass();
  20.             IITLibraryPlaylist mainLibrary = itunes.LibraryPlaylist;
  21.             IITTrackCollection ittracks = mainLibrary.Tracks;          
  22.             List<string> files = new List<string>();
  23.             List<string> musicfiles = new List<string>();
  24.             List<string> badfiles = new List<string>();
  25.             List<string> songlist = new List<string>();
  26.          
  27.           int state = (int)itunes.PlayerState;
  28.           //Console.Write(state); System.Threading.Thread.Sleep(5500);
  29.          
  30.           if (state == 1){ Console.Write("Itunes will be Pausing to ensure Data Integrity");System.Threading.Thread.Sleep(5500); itunes.Pause(); }
  31.           //Console.WriteLine state;
  32.          
  33.           string[] extensions = { "*.mp3", "*.mp4", "*.m4a", "*.m4v", "*.m4p", "*.m4b", "*.flac"};
  34.             string filepath = "G:\\iTunes Media";
  35.             foreach (string extension in extensions)
  36.             {
  37.                 files.AddRange(Directory.GetFiles(filepath, extension, SearchOption.AllDirectories));
  38.             }
  39.  
  40.             foreach (string file in files)
  41.             {
  42.                 try {string taglibfile = TagLib.File.Create(file).Tag.Title; musicfiles.Add(file); Console.WriteLine(taglibfile); }
  43.                 catch {badfiles.Add(file); }
  44.             }
  45.  
  46.             //string pattern = "[\\~#%&*{}/:<>?|\"-]";
  47.             //string replacement = "_";
  48.             //Regex regEx = new Regex(pattern);
  49.             //string sanitized = Regex.Replace(regEx.Replace(input, replacement), @"\s+", " ");
  50.          
  51.           XDocument baddoc = new XDocument
  52.                 (new XElement("Corrupt",
  53.                     badfiles.Select(badfile =>
  54.                     new XElement("File", badfile))));
  55.                 baddoc.Save("G:\\ITL\\badfiles.xml");
  56.           // foreach(string musicfile in musicfiles)
  57.            //{ String Title = (TagLib.File.Create(musicfile).Tag.Title); }
  58.      
  59.           XDocument doc = new XDocument();
  60.               //foreach(string musicfile in musicfiles){
  61.               //string Title = (TagLib.File.Create(musicfile).Tag.Title);
  62.               //}
  63.               XElement songsElement = new XElement("Songs");
  64.               foreach(var musicfile in musicfiles)
  65.               {
  66.                   XElement songElement = new XElement("Song");
  67.                   string songTitle = TagLib.File.Create(musicfile).Tag.Title;
  68.                   string songPath = musicfile;
  69.                   string songArtist = (TagLib.File.Create(musicfile).Tag.Performers);
  70.                   songlist.Add("Adding "+songTitle+", Artist "+songArtist+", at path "+songPath+" to DirLib file.");
  71.                   XElement titleElement = new XElement("Title",songTitle);
  72.                   XElement pathElement = new XElement("Path", musicfile);
  73.                   XElement artistElement = new XElement("Artist",songArtist);
  74.                   songElement.Add(titleElement);
  75.                   songElement.Add(pathElement);
  76.                   songElement.Add(artistElement);
  77.                   songsElement.Add(songElement);
  78.               }
  79.           System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", songlist);    
  80.           doc.Add(songsElement);      
  81.           doc.Save("G:\\ITL\\DirLib.xml");
  82.                 itunes.Play();
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement