Beispiel #1
0
        static void Main(string[] args)
        {
            //args = new string[] { "oid", "13684" };

            if (args.Length < 1 || args.Length > 3)
            {
                Console.WriteLine("Invalid arguments");
                return;
            }

            string arg1 = args[0];
            string arg2 = args.Length >= 2 ? args[1] : null;
            string arg3 = args.Length >= 3 ? args[2] : null;

            RunMode mode;
            int value;
            if (string.Equals(arg1, "rename", StringComparison.OrdinalIgnoreCase))
            {
                var currentDiretory = Environment.CurrentDirectory;
                var oldFilename = System.IO.Path.Combine(currentDiretory, arg2);
                
                var record = new RecordingData().GetRecordingByFilename(oldFilename);
                if (record == null)
                {
                    throw new Exception("File not found in database");
                }

                var newFilename = System.IO.Path.Combine(currentDiretory, arg3);
                Console.WriteLine("Renaming " + oldFilename + " to " + newFilename);
                new RecordingData().UpdateFilename(record, newFilename);
                System.IO.File.Move(oldFilename, newFilename);
                return;
            }
            else if (string.Equals(arg1, "all", StringComparison.OrdinalIgnoreCase))
            {
                mode = RunMode.All;
                value = -1;
            }
            else if (string.Equals(arg1, "failed", StringComparison.OrdinalIgnoreCase))
            {
                mode = RunMode.Failed;
                value = -1;
            }
            else if (string.Equals(arg1, "oid", StringComparison.OrdinalIgnoreCase))
            {
                mode = RunMode.Oid;
                value = int.Parse(arg2);
            }
            else
            {
                Console.WriteLine("Invalid arguments");
                return;
            }

            new Program().Run(mode, value);
        }
Beispiel #2
0
        public void Run(RunMode mode, int value)
        {
            var stopwatch = new System.Diagnostics.Stopwatch();
            stopwatch.Start();

            try
            {    
                DatabaseFactory.CreateMetaDb();

                LogUtil.Log("----------------------------------");
                LogUtil.Log("Starting: " + DateTime.Now);
                LogUtil.Log("MODE: " + mode.ToString());
                if (mode == RunMode.Oid)
                    LogUtil.Log("OID: " + value);

                var tmDbSync = new TmDbSync();
                var tmDbMatch = new TmDbMatch();
                var tmDbRename = new TmDbRename();
                var tvDbSync = new TvDbSync();
                var tvDbMatch = new TvDbMatch();
                var tvDbRename = new TvDbRename();
                var recordData = new RecordingData();

                List<Recording> records = null;
                if (mode != RunMode.Oid)
                    records = recordData.GetRecordings().ToList();
                else
                    records = new[] { recordData.GetRecording(value) }.ToList();

                if (mode == RunMode.Failed)
                {
                    //var matches = MatchData.GetMatches().Where(a => !a.Success);
                    //records = records.Where(a => matches.Any(b => b.Oid == a.Oid)).ToList();
                    var successes = MatchData.GetMatches().Where(a => a.Success);
                    records = records.Where(a => !successes.Any(b => b.Oid == a.Oid)).ToList();
                }

                //Sync
                foreach (var record in records)
                {
                    try
                    {
                        if (!record.IsMovie)
                            tvDbSync.Sync(record);
                        else
                            tmDbSync.Sync(record);
                    }
                    catch (Exception ex)
                    {
                        LogUtil.Log("Synchronization Error");
                        LogUtil.Log(ex.Message);
                    }
                }

                //Match & Rename
                foreach (var record in records)
                {
                    string targetId = null;

                    try
                    {
                        if (!record.IsMovie)
                        {
                            float best = -1f;
                            var result = tvDbMatch.Match(record, out best);
                            var series = result.Item1;
                            var episode = result.Item2;
                            tvDbRename.Rename(record, series, episode);
                            targetId = episode != null ? episode.Id : null; //save target id
                        }
                        else
                        {
                            float best = -1f;
                            var movie = tmDbMatch.Match(record, out best);
                            tmDbRename.Rename(record, movie);
                            targetId = movie != null ? movie.Id : null; //save target id                            
                        }
                    }
                    finally
                    {
                        //Record success/failure
                        var oid = record.Oid;
                        var type = record.IsMovie ? MediaType.MovieType : MediaType.SeriesType;
                        var success = targetId != null;
                        MatchData.SaveMatch(new Match(oid, type, success, targetId));
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.Log("Critical Error");
                LogUtil.Log(ex.Message);
                LogUtil.Log(ex.StackTrace.ToString());
            }

            LogUtil.Log("Finished: " + DateTime.Now);
            LogUtil.Log("Ellapsed Time: " + (stopwatch.ElapsedMilliseconds / 1000) + " seconds");
            LogUtil.Log("----------------------------------");
        }