Beispiel #1
0
 public void ShowAlias()
 {
     var newName1 = ShowAliases.RenameByAlias("The Americans 2013 1080p-YiFY");
     var newName2 = ShowAliases.RenameByAlias("The.Americans.2013.1080p-YiFY");
     var newName3 = ShowAliases.RenameByAlias("The Americans 1080p-YiFY");
     var newName4 = ShowAliases.RenameByAlias("The.Americans.1080p-YiFY");
 }
Beispiel #2
0
        public void ProcessDirectories()
        {
            if (_downloadingDirectory != null && _downloadingDirectory.Exists && _processingDirectory != null && _processingDirectory.Exists)
            {
                // first get files to move that are nonzero in size (downloading to processing)
                foreach (var file in _downloadingDirectory.GetFiles("*", SearchOption.AllDirectories).Where(o => _extensionsToCopy.Contains(o.Extension.ToLower()) && o.Length > 0 && o.Exists))
                {
                    try
                    {
                        // look for Season folder in processing directory, if it doesn't exist, create
                        if (file.Directory.Name.StartsWith("Season"))
                        {
                            var processingDirectorySeasonFolder = _processingDirectory.GetDirectories(file.Directory.Name).SingleOrDefault();
                            if (processingDirectorySeasonFolder == null)
                            {
                                processingDirectorySeasonFolder = _processingDirectory.CreateSubdirectory(file.Directory.Name);
                            }

                            // verify season folder exists
                            if (processingDirectorySeasonFolder != null)
                            {
                                if (!_isReadOnly)
                                {
                                    // move file
                                    file.MoveTo(System.IO.Path.Combine(processingDirectorySeasonFolder.FullName, file.Name.Replace(' ', '.')));
                                    WriteMessage($"Moved file to {file.FullName}");

                                    // increment count
                                    if (_processedShows.ContainsKey(_downloadingDirectory.Name))
                                    {
                                        _processedShows[_downloadingDirectory.Name]++;
                                    }
                                    else
                                    {
                                        _processedShows.Add(_downloadingDirectory.Name, 1);
                                    }
                                }
                                else
                                {
                                    WriteMessage($"Could not move read-only file {file.FullName}", OutputType.Error);
                                }
                            }
                        }
                    }

                    catch (Exception ex)
                    {
                        WriteMessage($"Error moving file {file.Name}: {ex.Message}", OutputType.Error);
                    }
                }

                // test directory to make sure we need to mirror back
                if (_processingDirectory.GetFiles("*", SearchOption.AllDirectories).Where(o => _extensionsToCopy.Contains(o.Extension.ToLower()) && o.Length > 0 && o.Exists).Any())
                {
                    // use ROBOCOPY to do actual mirroring
                    int    timeout = 30000;
                    var    output  = new StringBuilder();
                    var    error   = new StringBuilder();
                    string args    = String.Format(@"""{0}"" ""{1}"" {2} /CREATE /E /XD .actors", _processingDirectory.FullName, _downloadingDirectory.FullName, String.Join(" ", _extensionsToCopy.Select(o => "*" + o)));
                    if (!_isReadOnly)
                    {
                        // rename by alias
                        foreach (var f in _processingDirectory.GetFiles("*", SearchOption.AllDirectories).Where(o => _extensionsToCopy.Contains(o.Extension.ToLower()) && o.Length > 0 && o.Exists))
                        {
                            var newName = ShowAliases.RenameByAlias(f.Name);
                            try
                            {
                                var newNameAndPath = System.IO.Path.Combine(f.DirectoryName, newName);
                                f.MoveTo(newNameAndPath);
                            }
                            catch (Exception e)
                            {
                                WriteMessage($"Unable to move file '{f.Name}' to '{newName}': {e.Message}", OutputType.Error);
                            }
                        }

                        // start copy process
                        try
                        {
                            var p = new Process()
                            {
                                StartInfo = new ProcessStartInfo
                                {
                                    FileName               = @"C:\Windows\System32\robocopy.exe",
                                    Arguments              = args,
                                    RedirectStandardError  = true,
                                    RedirectStandardOutput = true,
                                    WindowStyle            = ProcessWindowStyle.Normal,
                                    CreateNoWindow         = true,
                                    UseShellExecute        = false,
                                    RedirectStandardInput  = true,
                                }
                            };
                            using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
                                using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
                                {
                                    // output events
                                    p.OutputDataReceived += (s, evt) =>
                                    {
                                        if (evt.Data == null)
                                        {
                                            outputWaitHandle.Set();
                                        }
                                        else
                                        {
                                            output.AppendLine(evt.Data);
                                        }
                                    };
                                    p.ErrorDataReceived += (s, evt) =>
                                    {
                                        if (evt.Data == null)
                                        {
                                            errorWaitHandle.Set();
                                        }
                                        else
                                        {
                                            output.AppendLine(evt.Data);
                                        }
                                    };

                                    // start process
                                    p.Start();
                                    p.BeginOutputReadLine();
                                    p.BeginErrorReadLine();

                                    if (p.WaitForExit(timeout) &&
                                        outputWaitHandle.WaitOne(timeout) &&
                                        errorWaitHandle.WaitOne(timeout))
                                    {
                                        // Process completed. Check process.ExitCode here.
                                    }
                                    else
                                    {
                                        // Timed out.
                                    }
                                }
                        }
                        catch (Exception ex)
                        {
                            WriteMessage(ex.Message, OutputType.Error);
                        }

                        // handle output
                        WriteMessage(output.ToString());
                        WriteMessage(error.ToString(), OutputType.Error);

                        // special: delete .actors folder in target
                        foreach (var deleteDir in _downloadingDirectory.GetDirectories(".actors", SearchOption.AllDirectories))
                        {
                            try
                            {
                                deleteDir.Delete(true);
                            }
                            catch (Exception e)
                            {
                                WriteMessage($"Can't delete folder {deleteDir.FullName}: {e.Message}", OutputType.Error);
                            }
                        }
                    }
                    else
                    {
                        // debug show what robocopy command would have been run
                        WriteMessage(args);
                    }
                }
            }
            else
            {
                WriteMessage("Please drop a valid subdirectory to begin.", OutputType.Error);
            }
        }