Skip to content

Commit

Permalink
Clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
Webreaper committed May 17, 2021
1 parent 707af9e commit 5e3e7a2
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 40 deletions.
Empty file modified README.md
100644 → 100755
Empty file.
10 changes: 7 additions & 3 deletions Settings.json
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
{
"radarr": {
"url" : "http://192.168.1.30:7878",
"apiKey" : "87a3763f384241c2a33dfeea625681ca",
"apiKey" : "87a3763f384241c2a33dfeea625681c5",
"mappingPath" : "/downloads/",
"downloadsFolder" : "/volume1/video/FilmDownloads"
"downloadsFolder" : "/volume1/video/FilmDownloads",
"importMode" : "Move",
"timeoutSecs" : "5"
},
"sonarr": {
"url" : "http://192.168.1.30:8989",
"apiKey" : "8727e07296064e369Xcb05d3c11b9531",
"apiKey" : "8727e07296064e369Xcb05d3c11b9532",
"mappingPath" : "/downloads/",
"downloadsFolder" : "/volume1/video/Downloads",
"importMode" : "Copy",
"timeoutSecs" : "5",
"transforms" : [
{
"search" : "Gardeners World 2019",
Expand Down
Empty file modified SonarrAutoImport.sln
100644 → 100755
Empty file.
Empty file modified SonarrAutoImport/LogHandler.cs
100644 → 100755
Empty file.
18 changes: 6 additions & 12 deletions SonarrAutoImport/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@ public class Options
{
[Option('v', "verbose", HelpText = "Run logging in Verbose Mode")]
public bool Verbose { get; set; }

[Option('c', "copy", Required = false, Default = false, HelpText = "Move or copy")]
public bool ImportMode { get; set; }


[Option('d', "dry-run", Required = false, Default = false, HelpText = "Dry run - change nothing.")]
public bool DryRun { get; set; }

[Option('t', "timeout", Required = false, Default = 1000, HelpText = "Timeout between requests")]
public int Timeout { get; set; }

[Value(0, MetaName = "Settings Path", HelpText = "Path to settings JSON file (default = app dir)", Required = false)]
public string SettingsPath { get; set; } = "Settings.json";
};
Expand All @@ -45,17 +39,17 @@ private static void RunProcess(Options o)

if (settings.sonarr != null)
{
Logging.LogHandler.Log("Processing videos for Sonarr...");
importer.ProcessService(settings.sonarr, o.DryRun, o.Verbose, "DownloadedEpisodesScan", o.ImportMode, o.Timeout);
LogHandler.Log("Processing videos for Sonarr...");
importer.ProcessService(settings.sonarr, o.DryRun, o.Verbose, "DownloadedEpisodesScan");
}
if (settings.radarr != null)
{
Logging.LogHandler.Log("Processing videos for Radarr...");
importer.ProcessService(settings.radarr, o.DryRun, o.Verbose, "DownloadedMoviesScan",o.ImportMode, o.Timeout);
LogHandler.Log("Processing videos for Radarr...");
importer.ProcessService(settings.radarr, o.DryRun, o.Verbose, "DownloadedMoviesScan");
}
}
else
Logging.LogHandler.LogError($"Settings not found: {o.SettingsPath}");
LogHandler.LogError($"Settings not found: {o.SettingsPath}");
}
}
}
6 changes: 6 additions & 0 deletions SonarrAutoImport/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ public class ServiceSettings
public string apiKey { get; set; }
[DataMember]
public List<Transform> transforms { get; set; }
[DataMember]
public string importMode { get; set; } = "Move";
[DataMember]
public int timeoutSecs { get; set; }
[DataMember]
public bool trimFolders { get; set; }
}

[DataContract]
Expand Down
8 changes: 4 additions & 4 deletions SonarrAutoImport/SonarrAutoImport.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="RestSharp" Version="106.10.1" />
<PackageReference Include="CommandLineParser" Version="2.7.82" />
<PackageReference Include="RestSharp" Version="106.11.7" />
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
<PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
</ItemGroup>
</Project>
87 changes: 66 additions & 21 deletions SonarrAutoImport/SonarrImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using RestSharp;
using System;
using System.Threading;

namespace SonarrAuto
Expand Down Expand Up @@ -90,16 +89,24 @@ private string MoveFile(string fullPathName, string newFileName)
return fullPathName;
}

public void ProcessService( ServiceSettings settings, bool dryRun, bool verbose, string apiCommand, bool import_mymode, int timeout)
public void ProcessService( ServiceSettings settings, bool dryRun, bool verbose, string apiCommand)
{
DirectoryInfo baseDir = new DirectoryInfo(settings.downloadsFolder);

if (settings.importMode != "Copy" && settings.importMode != "Move")
{
Log($"Invalid importMode '{settings.importMode}' in settings. Defaulting to 'Move'");
settings.importMode = "Move";
}

Log("Starting video processing for: {0}", baseDir);
if (verbose)
{
Log(" Base Url: {0}", settings.url);
Log(" API Key: {0}", settings.apiKey);
Log(" Mapping: {0}", settings.mappingPath);
Log(" Timeout: {0}", settings.timeoutSecs);
Log(" CopyMode: {0}", settings.importMode);
Log(" Dry Run: {0}", dryRun);
}

Expand All @@ -119,12 +126,7 @@ public void ProcessService( ServiceSettings settings, bool dryRun, bool verbose,
string videoFullPath = file.FullName;

string newFileName = TransformFileName(settings.transforms, videoFullPath, verbose);

if (!dryRun)
{
videoFullPath = MoveFile(file.FullName, newFileName);
}


if (!dryRun)
{
videoFullPath = MoveFile(file.FullName, newFileName);
Expand All @@ -134,21 +136,74 @@ public void ProcessService( ServiceSettings settings, bool dryRun, bool verbose,

if (!dryRun)
{
QuickImport(path, settings, verbose, apiCommand,import_mymode,timeout);
QuickImport(path, settings, verbose, apiCommand);
}
else
Log(" => {0}", path);

if (settings.timeoutSecs != 0)
{
Log( $"Sleeping for {settings.timeoutSecs} seconds...");
Thread.Sleep(settings.timeoutSecs * 1000);
}
}

Log("All processing complete.");
}
else
Log("No videos found. Nothing to do!");

if (settings.trimFolders)
{
Log($"Trimming empty folders in {baseDir.FullName}");
TrimEmptyFolders(baseDir);
}
}
else
Log($"Folder {baseDir} was not found. Check configuration.");
}

private void TrimEmptyFolders(DirectoryInfo baseDir)
{
if ((baseDir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
return;

if (baseDir.Name.StartsWith(".") || baseDir.Name.StartsWith("@"))
return;

var allFolders = baseDir.GetDirectories("*.*", SearchOption.AllDirectories);

foreach (var folder in allFolders)
{
try
{
TrimEmptyFolders(folder);

var nonMovieFiles = folder.GetFiles()
.Where(x => !movieExtensions.Contains(x.Extension, StringComparer.OrdinalIgnoreCase)
&& !x.Name.StartsWith( ".")
&& (x.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
.ToList();

nonMovieFiles.ForEach(x =>
{
Log($" Deleting non-video file: {x.FullName}");
x.Delete();
});

if (!folder.GetFiles().Any() && !folder.GetDirectories().Any() )
{
Log($"Removing empty folder: {folder.FullName}");
folder.Delete();
}
}
catch (Exception ex)
{
Log($"Unexpected exception during folder trim: {ex.Message}");
}
}
}

private string TranslatePath(string baseFolder, string fullName, string mapFolder)
{
string path = Path.GetFullPath(fullName);
Expand All @@ -158,22 +213,12 @@ private string TranslatePath(string baseFolder, string fullName, string mapFolde
return Path.Combine(mapFolder, localPath);
}

private void QuickImport(string remotePath, ServiceSettings service, bool verbose, string apiCommand , bool mymode , int timeout)
private void QuickImport(string remotePath, ServiceSettings service, bool verbose, string apiCommand)
{
Log($"Sleeping some seconds...");
Thread.Sleep(timeout);
try
{
RestClient client = new RestClient(service.url);
//
var payload = new PayLoad { path = remotePath, name = apiCommand};
if (mymode) {
payload = new PayLoad { path = remotePath, name = apiCommand , importMode = "Copy"};
}
// }else {
// var payload = new PayLoad { path = remotePath, name = apiCommand , importMode = "Copy"};

// }
var payload = new PayLoad { path = remotePath, name = apiCommand, importMode = service.importMode };

var request = new RestRequest(Method.POST);

Expand Down
Empty file modified publish.sh
100644 → 100755
Empty file.

0 comments on commit 5e3e7a2

Please sign in to comment.