fixed song crawler to handle zip files
This commit is contained in:
parent
c3e65fcb63
commit
b6588ff28d
@ -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<string> files = new List<string>();
|
||||
files.AddRange(Directory.GetFiles(songpath, "*.mp3", SearchOption.AllDirectories));
|
||||
files.AddRange(Directory.GetFiles(songpath, "*.zip", SearchOption.AllDirectories));
|
||||
List<Song> songs = new List<Song>();
|
||||
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
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -55,6 +55,8 @@
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.IO.Compression" />
|
||||
<Reference Include="System.IO.Compression.FileSystem" />
|
||||
<Reference Include="System.Net" />
|
||||
<Reference Include="System.Net.Http.Extensions, Version=2.2.28.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.Net.Http.2.2.28\lib\net45\System.Net.Http.Extensions.dll</HintPath>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user