Esempio n. 1
0
        protected override void Process(String[] args)
        {
            OnProcessStarting();
            TriggerActions(args.ToList());

            List <Track> tracks;

            do
            {
                using (DataContext dataContext = DataContextFactory.GetInstance())
                {
                    LoggerBundle.Debug("Preloading data...");
                    Stopwatch sw = new Stopwatch();
                    sw.Start();
                    tracks = _includeFailed
                        ? dataContext.SetTracks.Where(x => !x.LastFingerprintCalculation.HasValue || null != x.FingerprintError)
                             .Take(_config.BufferSize)
                             .ToList()
                        : dataContext.SetTracks.Where(x => !x.LastFingerprintCalculation.HasValue)
                             .Take(_config.BufferSize)
                             .ToList();

                    sw.Stop();
                    LoggerBundle.Debug($"Getting data finished in {sw.ElapsedMilliseconds}ms");
                }
                LoggerBundle.Inform($"Batch contains {tracks.Count} record(s).");

                foreach (Track track in tracks)
                {
                    LoggerBundle.Trace($"Initializing process for track '{track}'...");
                    while (_buffer.Count >= _config.ParallelProcesses)
                    {
                        Thread.Sleep(1);
                    }

                    LoggerBundle.Trace($"Starting process for track '{track}'...");
                    Process p = new Process
                    {
                        StartInfo =
                        {
                            FileName                 = FingerprintCalculationExecutablePath
                            , Arguments              = $"-json \"{track.Path}\""
                            , CreateNoWindow         = true
                            , RedirectStandardError  = true
                            , RedirectStandardInput  = true
                            , RedirectStandardOutput = true
                            , UseShellExecute        = false
                        }
                    };
                    p.OutputDataReceived += (_, arguments) => HandleStdOutput(arguments, track);
                    p.ErrorDataReceived  += (_, arguments) => HandleErrOutput(arguments, track);
                    _buffer.Add(track);
                    LoggerBundle.Trace($"Starting computation process for file '{track}'...");
                    p.Start();
                    p.BeginOutputReadLine();
                    p.BeginErrorReadLine();
                    LoggerBundle.Debug($"Computation process started for file '{track}'");
                }
            }while (tracks.Count > 0);
        }
Esempio n. 2
0
        private static Dictionary <String, PluginBase> InitializePlugin(List <PluginBase> pluginsToLoad)
        {
            Dictionary <String, PluginBase> plugins = new Dictionary <String, PluginBase>();

            pluginsToLoad.ForEach(x =>
            {
                // initialize
                Boolean initialized = x.Initialize();

                if (!initialized)
                {
                    LoggerBundle.Warn($"Plugin '{x.Name}' cannot be initialized. This plugin will be deactivated.");
                    return;
                }

                LoggerBundle.Debug($"Plugin '{x.Name}' initialized successfully. Validating...");

                // validate
                String pcName = x.Name.ToLower().Trim();
                if (plugins.ContainsKey(pcName))
                {
                    LoggerBundle.Warn($"Plugin '{x.Name}' does not pass validation because a plugin with the same name has already been registered. This plugin will be deactivated.");
                }

                LoggerBundle.Debug($"Plugin '{x.Name}' passed validation");

                // add to plugin registry
                plugins.Add(pcName, x);
                LoggerBundle.Inform($"Plugin '{x.Name}' activated");
            });
            return(plugins);
        }
Esempio n. 3
0
        public static T Request <T>(String path) where T : class
        {
            LoggerBundle.Debug($"Requested configuration '{path}'");

            if (!File.Exists(path))
            {
                LoggerBundle.Trace($"File '{path}' not found. Trying to create it...");
                FileInterface.Save(Activator.CreateInstance <T>(), path);
                LoggerBundle.Trace($"Successfully created file '{path}'");

                LoggerBundle.Inform(
                    $"Changes to the newly created file '{path}' will take effect after restarting the executable. Adjust default value as needed and restart the application.");
            }

            LoggerBundle.Trace($"Trying to read file '{path}'...");
            (T result, Boolean success) = FileInterface.Read <T>(path);
            if (!success)
            {
                LoggerBundle.Warn(new ProcessAbortedException());
                return(null);
            }

            LoggerBundle.Debug($"Successfully read configuration file '{path}'");
            return(result);
        }
Esempio n. 4
0
        public AcoustIdApiHandler(String apiKey)
        {
            LoggerBundle.Trace($"Initializing AcoustId API Handler with Kes '{apiKey}'");
            _apiKey = apiKey;

            LoggerBundle.Inform(
                $"Notice: The AcoustId API is throttled to a maximum of {MAX_REQUESTS_PER_SECOND} requests per second due to their policy.");
        }
Esempio n. 5
0
        protected override void Process(String[] args)
        {
            OnProcessStarting();
            TriggerActions(args.ToList());

            List <String> paths = args.Distinct().Select(x => x.Trim()).ToList();

            if (paths.Count.Equals(0))
            {
                LoggerBundle.Fatal(new ArgumentException("no argument given"));
                Environment.Exit(1);
            }

            foreach (String path in paths)
            {
                LoggerBundle.Inform($"Processing path '{path}'...");
                if (!Directory.Exists(path))
                {
                    LoggerBundle.Warn($"Path '{path}' not found. Skipping.");
                    continue;
                }

                LoggerBundle.Debug("Preloading data...");
                List <String> tracks;
                Stopwatch     sw = new Stopwatch();
                sw.Start();
                using (DataContext dataContext = DataContextFactory.GetInstance())
                {
                    tracks = dataContext.SetTracks.AsNoTracking().Select(x => x.Path).ToList();
                }
                sw.Stop();
                LoggerBundle.Debug($"Getting data finished in {sw.ElapsedMilliseconds}ms");

                List <String>       buffer = new List <String>();
                DataSource <String> ds     = new PathDataSource(path, _config.Extensions);

                LoggerBundle.Inform($"Start to crawl path '{path}'...");
                foreach (String file in ds.Get())
                {
                    buffer.Add(file);

                    Int32 bufferCount = buffer.Count;
                    if (bufferCount < _config.BufferSize)
                    {
                        if (bufferCount % (_config.BufferSize < 1337 ? _config.BufferSize : 1337) == 0)
                        {
                            LoggerBundle.Trace($"Adding files to buffer [{bufferCount}/{_config.BufferSize}] ...");
                        }
                        continue;
                    }

                    ProcessBuffer(ref buffer, ref tracks);
                }

                ProcessBuffer(ref buffer, ref tracks);
            }
        }
Esempio n. 6
0
 private static void Main(String[] args)
 {
     // mainly for debugging
     LoggerBundle.Trace("TEST");
     LoggerBundle.Debug("TEST");
     LoggerBundle.Inform("TEST");
     LoggerBundle.Warn(new Exception());
     LoggerBundle.Error(new Exception());
     LoggerBundle.Fatal(new Exception());
 }
Esempio n. 7
0
        private void CleanInvisible()
        {
            List <Track> data;

            do
            {
                LoggerBundle.Inform("Removing invisible data");
                LoggerBundle.Debug("Getting data...");
                using (DataContext dataContext = DataContextFactory.GetInstance())
                {
                    data = dataContext.SetTracks.Where(x => null != x.LastAcoustIdApiCall).Where(x => 1 > x.AcoustIdResults.Count).OrderBy(x => x.UniqueId).Take(_config.BufferSize).ToList();

                    LoggerBundle.Inform($"Batch containing: {data.Count} entries");

                    foreach (Track track in data)
                    {
                        try
                        {
                            LoggerBundle.Debug($"Processing track '{track}'...");

                            if (File.Exists(track.Path))
                            {
                                File.Delete(track.Path);
                            }

                            dataContext.SetTracks.Remove(track);
                            dataContext.SaveChanges();

                            LoggerBundle.Trace("Processing done");
                        }
                        catch (Exception ex)
                        {
                            LoggerBundle.Error(ex);
                        }
                    }
                }
            }while (data.Count > 0);
        }
Esempio n. 8
0
        private Program(String[] args)
        {
            LoggerBundle.Inform("Initializing...");

            (Boolean success, ProgramConfig config) = ProcessProgramArguments(args);
            if (!success)
            {
                LoggerBundle.Fatal("Program arguments could not be parsed correctly");
                Environment.Exit(1);
            }

            try
            {
                CreateGlobalDirectories();

                List <PluginBase> pluginsToLoad         = LoadPlugins();
                Dictionary <String, PluginBase> plugins = InitializePlugin(pluginsToLoad);

                String pluginName = config.PluginName.ToLower();
                if (!plugins.ContainsKey(pluginName))
                {
                    LoggerBundle.Warn($"No active plugin with name '{pluginName}' found");
                    LoggerBundle.Inform("The following plugin names were registered on startup: ");
                    plugins.Keys.ToList().ForEach(x => LoggerBundle.Inform($"+ {x}"));
                    return;
                }

                LoggerBundle.Inform("Executing plugin...");
                plugins[pluginName].Work(config.Args.ToArray());
                LoggerBundle.Inform("Execution finished.");
            }
            catch (Exception ex)
            {
                LoggerBundle.Error(ex);
            }
        }
Esempio n. 9
0
 public MusicBrainzApiHandler()
 {
     LoggerBundle.Inform(
         $"Notice: The MusicBrainz API is throttled to a maximum of {MAX_REQUESTS_PER_SECOND} requests per second due to their policy.");
 }
Esempio n. 10
0
        protected override void Process(String[] args)
        {
            base.OnProcessStarting();
            TriggerActions(args.ToList());

            List <Track> data;

            do
            {
                using (DataContext dataContext = DataContextFactory.GetInstance())
                {
                    LoggerBundle.Debug("Loading batch...");

                    data = _includeFailed
                        ? dataContext.SetTracks
                           .Where(x => null != x.LastFingerprintCalculation &&
                                  null == x.FingerprintError &&
                                  null == x.LastAcoustIdApiCall ||
                                  x.LastAcoustIdApiCall.HasValue && null != x.AcoustIdApiError)
                           .Take(_config.BufferSize)
                           .ToList()
                        : dataContext.SetTracks
                           .Where(x => null != x.LastFingerprintCalculation &&
                                  null == x.FingerprintError &&
                                  null == x.LastAcoustIdApiCall)
                           .Take(_config.BufferSize)
                           .ToList();

                    LoggerBundle.Inform($"Batch containing {data.Count} entries");

                    foreach (Track track in data)
                    {
                        LoggerBundle.Debug($"Posting metadata of track '{track}'...");

                        track.LastAcoustIdApiCall = DateTime.Now;

                        Object response = _apiHandler.Post(track.Duration ?? 0d, track.Fingerprint);
                        LoggerBundle.Trace($"Response: {response}");

                        switch (response)
                        {
                        case JsonErrorAcoustId jea:
                        {
                            LoggerBundle.Warn(new AcoustIdApiException($"Error {jea.Error.Code}: {jea.Error.Message}"));
                            track.AcoustIdApiError     = jea.Error.Message;
                            track.AcoustIdApiErrorCode = jea.Error.Code;
                            break;
                        }

                        case JsonAcoustIdRequest air:
                        {
                            HandleResponse(dataContext, track, air);
                            break;
                        }

                        default:
                        {
                            LoggerBundle.Trace(Logger.DefaultLogFlags & ~LogFlags.SuffixNewLine
                                               , "Trying to serialize unknown response object...");
                            String serializedResponse = "<unknown>";
                            try
                            {
                                serializedResponse = JsonConvert.SerializeObject(response);
                            }
                            catch (Exception ex)
                            {
                                LoggerBundle.Error(ex);
                            }
                            LoggerBundle.Trace(Logger.DefaultLogFlags
                                               & ~LogFlags.PrefixTimeStamp
                                               & ~LogFlags.PrefixLoggerType
                                               , "Ok.");
                            LoggerBundle.Warn(new AcoustIdApiException($"Unknown response: {serializedResponse}"));
                            track.AcoustIdApiError = serializedResponse;
                            break;
                        }
                        }

                        dataContext.SaveChanges();
                    }
                }
            }while (data.Count > 0);
        }
Esempio n. 11
0
        protected override void Process(String[] args)
        {
            OnProcessStarting();
            TriggerActions(args.ToList());

            List <MusicBrainzRecord> data;

            do
            {
                LoggerBundle.Debug("Getting data...");
                using (DataContext dataContext = DataContextFactory.GetInstance())
                {
                    data = dataContext.SetMusicBrainzRecords.Where(x => null == x.LastMusicBrainzApiCall)
                           .Include(x => x.MusicBrainzAliasMusicBrainzRecords)
                           .ThenInclude(x => x.MusicBrainzAlias)
                           .Include(x => x.MusicBrainzArtistCreditMusicBrainzRecords)
                           .ThenInclude(x => x.MusicBrainzArtistCredit)
                           .Include(x => x.MusicBrainzReleaseMusicBrainzRecords)
                           .ThenInclude(x => x.MusicBrainzRelease)
                           .Include(x => x.MusicBrainzTagMusicBrainzRecords)
                           .ThenInclude(x => x.MusicBrainzTag)
                           .OrderBy(x => x.UniqueId)
                           .Take(_config.BatchSize)
                           .ToList();

                    LoggerBundle.Inform($"Batch containing: {data.Count} entries");

                    foreach (MusicBrainzRecord mbr in data)
                    {
                        try
                        {
                            LoggerBundle.Debug($"Processing record '{mbr}'...");

                            DateTime requestTime = DateTime.Now;
                            Object   o           = _api.Get(mbr.MusicbrainzId);

                            Stopwatch sw = new Stopwatch();
                            sw.Start();

                            switch (o)
                            {
                            case JsonMusicBrainzRequest req:
                                HandleResponse(mbr, req, dataContext);
                                break;

                            case JsonErrorMusicBrainz err:
                                mbr.MusicBrainzApiCallError = err.Error?.Trim() ?? "<unknown>";
                                LoggerBundle.Warn(new MusicBrainzApiException($"Error: {mbr.MusicBrainzApiCallError}"));
                                break;
                            }

                            mbr.LastMusicBrainzApiCall = requestTime;
                            dataContext.SaveChanges();

                            sw.Stop();
                            LoggerBundle.Debug($"Processing done in {sw.ElapsedMilliseconds}ms");
                        }
                        catch (Exception ex)
                        {
                            LoggerBundle.Error(ex);
                        }
                    }
                }
            }while (data.Count > 0);
        }
Esempio n. 12
0
 private void OnActionHelp()
 {
     LoggerBundle.Inform(GetHelp());
 }
Esempio n. 13
0
 protected virtual void OnProcessStopping()
 {
     LoggerBundle.Inform($"A process of plugin '{Name}' is stopping");
 }
Esempio n. 14
0
        private void AddUser()
        {
            LoggerBundle.Debug("Starting process to add new user...");
            try
            {
                // read username
                LoggerBundle.Inform(Logger.DefaultLogFlags & ~LogFlags.SuffixNewLine, "Enter a username: "******"";

                if (String.IsNullOrWhiteSpace(username))
                {
                    LoggerBundle.Fatal(new ArgumentException("Username cannot be empty"));
                    Environment.Exit(1);
                }

                // check existance
                LoggerBundle.Debug("Checking if user already exists...");
                Boolean exists;
                using (DataContext dataContext = DataContextFactory.GetInstance())
                {
                    exists = dataContext.SetUsers.Any(x => x.Username.ToLower().Equals(username.ToLower()));
                }
                if (exists)
                {
                    LoggerBundle.Fatal(new ArgumentException("Username already exists"));
                    Environment.Exit(1);
                }

                LoggerBundle.Trace("User not found database. Allowed to proceed forward");

                // get password
                LoggerBundle.Inform(Logger.DefaultLogFlags & ~LogFlags.SuffixNewLine, "Enter a password: "******"Confirm password: "******"Passwords do not match"));
                    Environment.Exit(1);
                }

                // hash password
                Sha512HashPipe hashPipe = new Sha512HashPipe();
                String         hashedPw = hashPipe.Process(pw1);

                // save model
                User user = new User
                {
                    Username   = username
                    , Password = hashedPw
                };
                using (DataContext dataContext = DataContextFactory.GetInstance())
                {
                    dataContext.SetUsers.Add(user);
                    dataContext.SaveChanges();
                }
                LoggerBundle.Inform(
                    $"Successfully created user '{user.Username}' created with unique identifier '{user.UniqueId}'");
            }
            catch (Exception ex)
            {
                LoggerBundle.Error(ex);
            }
        }