194 lines
6.4 KiB
C#
194 lines
6.4 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;
|
|
using System.Configuration;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace SongCrawler
|
|
{
|
|
|
|
class Program
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
if(args.Last() == "-sl")
|
|
{
|
|
UploadSongList(args);
|
|
} else
|
|
{
|
|
CrawlSongs(args);
|
|
}
|
|
}
|
|
|
|
private static void UploadSongList(string[] args)
|
|
{
|
|
string songlistpath = args[0];
|
|
if (args.Length != 2)
|
|
{
|
|
Console.WriteLine("usage: songcrawler jsonpath");
|
|
return;
|
|
}
|
|
IFirebaseConfig config = new FirebaseConfig
|
|
{
|
|
AuthSecret = ConfigurationManager.AppSettings["Firebase.Secret"],
|
|
BasePath = ConfigurationManager.AppSettings["Firebase.Path"]
|
|
};
|
|
FireSharp.FirebaseClient client = new FireSharp.FirebaseClient(config);
|
|
string firepath = "SongList";
|
|
Console.WriteLine("Loading current library");
|
|
List<SongList> songList = client.Get(firepath).ResultAs<List<SongList>>();
|
|
if (songList != null)
|
|
Console.WriteLine(string.Format("{0} songList loaded", songList.Count));
|
|
else
|
|
songList = new List<SongList>();
|
|
|
|
List<SongList> localSongList = JsonConvert.DeserializeObject<List<SongList>>(File.ReadAllText(songlistpath));
|
|
|
|
foreach (SongList sl in localSongList)
|
|
{
|
|
try
|
|
{
|
|
Console.WriteLine(string.Format("Checking for {0}", sl.Title));
|
|
if (!songList.Any(s => s.Title.ToLower() == sl.Title.ToLower())) songList.Add(sl);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
}
|
|
}
|
|
client.Set(firepath, songList);
|
|
}
|
|
|
|
private static void CrawlSongs(string[] args)
|
|
{
|
|
if (args.Length != 2)
|
|
{
|
|
Console.WriteLine("usage: songcrawler partyid songspath");
|
|
return;
|
|
}
|
|
string controller = args[0];
|
|
string songpath = args[1];
|
|
IFirebaseConfig config = new FirebaseConfig
|
|
{
|
|
AuthSecret = ConfigurationManager.AppSettings["Firebase.Secret"],
|
|
BasePath = ConfigurationManager.AppSettings["Firebase.Path"]
|
|
};
|
|
FireSharp.FirebaseClient client = new FireSharp.FirebaseClient(config);
|
|
string firepath = string.Format("controllers/{0}/songs", controller);
|
|
Console.WriteLine("Loading current library");
|
|
List<Song> songs = client.Get(firepath).ResultAs<List<Song>>();
|
|
if (songs != null)
|
|
Console.WriteLine(string.Format("{0} songs loaded", songs.Count));
|
|
else
|
|
songs = new List<Song>();
|
|
|
|
List<string> files = new List<string>();
|
|
files.AddRange(FindFiles("mp3", songpath));
|
|
files.AddRange(FindFiles("zip", songpath));
|
|
files.AddRange(FindFiles("mp4", songpath));
|
|
|
|
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));
|
|
if (!songs.Any(s => s.Path.ToLower() == song.Path.ToLower())) songs.Add(song);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
}
|
|
}
|
|
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.Trim();
|
|
song.Artist = tagFile.Tag.FirstPerformer.Trim();
|
|
song.Genre = tagFile.Tag.FirstGenre.Trim();
|
|
song.Year = (int)tagFile.Tag.Year;
|
|
}
|
|
catch
|
|
{
|
|
// do nothing;
|
|
}
|
|
song.Path = path;
|
|
return song;
|
|
}
|
|
}
|
|
}
|