102 lines
3.2 KiB
C#
102 lines
3.2 KiB
C#
using FireSharp.Config;
|
|
using FireSharp.Interfaces;
|
|
using FireSharp.Response;
|
|
using Herse.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.IO.Compression;
|
|
|
|
namespace SongCrawler
|
|
{
|
|
class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
string controller = args[0];
|
|
string songpath = args[1];
|
|
IFirebaseConfig config = new FirebaseConfig
|
|
{
|
|
AuthSecret = "9BQHUEJhScgK2FP10hvlToxTlGQpXT94Cvx01piO",
|
|
BasePath = "https://herse.firebaseio.com/"
|
|
};
|
|
FireSharp.FirebaseClient client = new FireSharp.FirebaseClient(config);
|
|
string firepath = string.Format("controllers/{0}/songs", controller);
|
|
|
|
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>();
|
|
string id3path = null;
|
|
Song song;
|
|
foreach (string filepath in files)
|
|
{
|
|
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
|
|
};
|
|
}
|
|
}
|
|
}
|