From b6588ff28d60aebfcd7b6c938df0233e1d045349 Mon Sep 17 00:00:00 2001 From: Don Archer Date: Sat, 6 Aug 2016 15:14:01 -0700 Subject: [PATCH] fixed song crawler to handle zip files --- SongCrawler/Program.cs | 73 +++++++++++++++++++++++++++++++--- SongCrawler/SongCrawler.csproj | 2 + 2 files changed, 69 insertions(+), 6 deletions(-) diff --git a/SongCrawler/Program.cs b/SongCrawler/Program.cs index db1c182..1999f61 100644 --- a/SongCrawler/Program.cs +++ b/SongCrawler/Program.cs @@ -8,6 +8,7 @@ using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.IO.Compression; namespace SongCrawler { @@ -25,16 +26,76 @@ namespace SongCrawler FireSharp.FirebaseClient client = new FireSharp.FirebaseClient(config); string firepath = string.Format("controllers/{0}/songs", controller); - - string[] files = Directory.GetFiles(songpath, "*.mp3"); + List files = new List(); + files.AddRange(Directory.GetFiles(songpath, "*.mp3", SearchOption.AllDirectories)); + files.AddRange(Directory.GetFiles(songpath, "*.zip", SearchOption.AllDirectories)); List songs = new List(); - foreach (string file in files) + string id3path = null; + Song song; + foreach (string filepath in files) { - TagLib.File tagFile = TagLib.File.Create(file); - songs.Add(new Song() { Title = tagFile.Tag.Title, Artist = tagFile.Tag.FirstPerformer, Path = file, Genre = tagFile.Tag.FirstGenre, Year = (int)tagFile.Tag.Year }); + Console.WriteLine(filepath); + var ext = Path.GetExtension(filepath).ToLower(); + switch (ext) + { + case ".mp3": + id3path = filepath; + break; + case ".zip": + id3path = UnzipMp3(filepath); + break; + } + song = ReadId3(id3path); + CheckTitle(song); + song.Path = filepath; + songs.Add(song); } - SetResponse response = client.Set(firepath, songs); } + + private static void CheckTitle(Song song) + { + if(string.IsNullOrEmpty(song.Title)) + { + string file = Path.GetFileNameWithoutExtension(song.Path); + string[] parts = file.Split('-'); + if (parts.Length == 1) + { + song.Title = parts[0].Trim(); + } + else if (parts.Length > 1) + { + song.Artist = parts[parts.Length - 2].Trim(); + song.Title = parts[parts.Length - 1].Trim(); + } + } + } + + private static string UnzipMp3(string filepath) + { + string file = Path.GetFileNameWithoutExtension(filepath); + try + { + ZipFile.ExtractToDirectory(filepath, "c:\\temp"); + } + catch + { + // do nothing fancy + } + return "c:\\temp\\" + file + ".mp3"; + } + + static Song ReadId3(string path) + { + TagLib.File tagFile = TagLib.File.Create(path); + return new Song() + { + Title = tagFile.Tag.Title, + Artist = tagFile.Tag.FirstPerformer, + Path = path, + Genre = tagFile.Tag.FirstGenre, + Year = (int)tagFile.Tag.Year + }; + } } } diff --git a/SongCrawler/SongCrawler.csproj b/SongCrawler/SongCrawler.csproj index 1b2df11..e42ec59 100644 --- a/SongCrawler/SongCrawler.csproj +++ b/SongCrawler/SongCrawler.csproj @@ -55,6 +55,8 @@ + + ..\packages\Microsoft.Net.Http.2.2.28\lib\net45\System.Net.Http.Extensions.dll