130 lines
4.1 KiB
C#
130 lines
4.1 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(FindFiles("mp3", songpath));
|
|
files.AddRange(FindFiles("zip", songpath));
|
|
files.AddRange(FindFiles("mp4", songpath));
|
|
|
|
List<Song> songs = new List<Song>();
|
|
Song song = null;
|
|
int i = 0;
|
|
foreach (string filepath in files)
|
|
{
|
|
i++;
|
|
try
|
|
{
|
|
song = MakeSong(filepath);
|
|
Console.WriteLine(string.Format("{0:000000}/{1} - {2}", i, files.Count, song.Title));
|
|
songs.Add(song);
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
}
|
|
}
|
|
SetResponse response = client.Set(firepath, songs);
|
|
}
|
|
|
|
private static Song MakeSong(string filepath)
|
|
{
|
|
|
|
Song song = null;
|
|
var ext = Path.GetExtension(filepath).ToLower();
|
|
switch (ext)
|
|
{
|
|
case ".mp3":
|
|
case ".mp4":
|
|
song = ReadId3(filepath);
|
|
break;
|
|
case ".zip":
|
|
song = ReadId3FromZip(filepath);
|
|
break;
|
|
}
|
|
CheckTitle(song);
|
|
song.Path = filepath;
|
|
return song;
|
|
}
|
|
|
|
private static string[] FindFiles(string ext, string path)
|
|
{
|
|
Console.Write(string.Format("\rscanning {0} for {1} - ", path, ext));
|
|
string[] files = Directory.GetFiles(path, "*." + ext, SearchOption.AllDirectories);
|
|
Console.WriteLine(string.Format("{0} found", files.Length));
|
|
return files;
|
|
}
|
|
|
|
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 Song ReadId3FromZip(string zippath)
|
|
{
|
|
ZipFile.ExtractToDirectory(zippath, "c:\\temp");
|
|
string filepath = Directory.GetFiles("c:\\temp", "*.mp3")[0];
|
|
Song song = ReadId3(filepath);
|
|
foreach (string file in Directory.GetFiles("c:\\temp")) File.Delete(file);
|
|
return song;
|
|
}
|
|
|
|
static Song ReadId3(string path)
|
|
{
|
|
Song song = new Song();
|
|
TagLib.File tagFile;
|
|
try
|
|
{
|
|
tagFile = TagLib.File.Create(path);
|
|
song.Title = tagFile.Tag.Title;
|
|
song.Artist = tagFile.Tag.FirstPerformer;
|
|
song.Genre = tagFile.Tag.FirstGenre;
|
|
song.Year = (int)tagFile.Tag.Year;
|
|
}
|
|
catch
|
|
{
|
|
// do nothing;
|
|
}
|
|
song.Path = path;
|
|
return song;
|
|
}
|
|
}
|
|
}
|