Skip to content

Commit

Permalink
Improve checkfornewtools logging and reuse HttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
ptr727 committed May 29, 2023
1 parent 246acce commit 03371c9
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 25 deletions.
8 changes: 3 additions & 5 deletions PlexCleaner/FfMpegTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Net.Http;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
Expand Down Expand Up @@ -100,10 +99,10 @@ protected override bool GetLatestVersionWindows(out MediaToolInfo mediaToolInfo)

// Load the release version page
// https://www.gyan.dev/ffmpeg/builds/release-version
using HttpClient httpClient = new();
mediaToolInfo.Version = httpClient.GetStringAsync("https://www.gyan.dev/ffmpeg/builds/release-version").Result;
mediaToolInfo.Version = Download.GetHttpClient().GetStringAsync("https://www.gyan.dev/ffmpeg/builds/release-version").Result;

// Create download URL and the output filename using the version number
// https://www.gyan.dev/ffmpeg/builds/packages/ffmpeg-6.0-full_build.7z
mediaToolInfo.FileName = $"ffmpeg-{mediaToolInfo.Version}-full_build.7z";
mediaToolInfo.Url = $"https://www.gyan.dev/ffmpeg/builds/packages/{mediaToolInfo.FileName}";
}
Expand All @@ -126,8 +125,7 @@ protected override bool GetLatestVersionLinux(out MediaToolInfo mediaToolInfo)

// Load the release version page
// https://johnvansickle.com/ffmpeg/release-readme.txt
using HttpClient httpClient = new();
var readmePage = httpClient.GetStringAsync("https://johnvansickle.com/ffmpeg/release-readme.txt").Result;
var readmePage = Download.GetHttpClient().GetStringAsync("https://johnvansickle.com/ffmpeg/release-readme.txt").Result;

// Read each line until we find the build and version lines
// build: ffmpeg-5.0-amd64-static.tar.xz
Expand Down
5 changes: 2 additions & 3 deletions PlexCleaner/MediaInfoTool.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Reflection;
using System.Text.RegularExpressions;
using InsaneGenius.Utilities;
using Serilog;

// http://manpages.ubuntu.com/manpages/zesty/man1/mediainfo.1.html
Expand Down Expand Up @@ -78,8 +78,7 @@ protected override bool GetLatestVersionWindows(out MediaToolInfo mediaToolInfo)
{
// Load the release history page
// https://raw.githubusercontent.com/MediaArea/MediaInfo/master/History_CLI.txt
using HttpClient httpClient = new();
var historyPage = httpClient.GetStringAsync("https://raw.githubusercontent.com/MediaArea/MediaInfo/master/History_CLI.txt").Result;
var historyPage = Download.GetHttpClient().GetStringAsync("https://raw.githubusercontent.com/MediaArea/MediaInfo/master/History_CLI.txt").Result;

// Read each line until we find the first version line
// E.g. Version 17.10, 2017-11-02
Expand Down
4 changes: 1 addition & 3 deletions PlexCleaner/MkvMergeTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -84,8 +83,7 @@ protected override bool GetLatestVersionWindows(out MediaToolInfo mediaToolInfo)
{
// Download latest release file
// https://mkvtoolnix.download/latest-release.xml.gz
using HttpClient httpClient = new();
var releaseStream = httpClient.GetStreamAsync("https://mkvtoolnix.download/latest-release.xml.gz").Result;
var releaseStream = Download.GetHttpClient().GetStreamAsync("https://mkvtoolnix.download/latest-release.xml.gz").Result;

// Get XML from Gzip
using GZipStream gzstream = new(releaseStream, CompressionMode.Decompress);
Expand Down
11 changes: 8 additions & 3 deletions PlexCleaner/Monitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,15 @@ void ErrorHandler(object s, ErrorEventArgs e)
// Add monitor folders to the processing list
if (Program.Options.PreProcess)
{
Log.Logger.Information("Pre-processing all monitored folders");
foreach (string folder in folders)
// Lock
lock (WatchLock)
{
OnChanged(folder);
Log.Logger.Information("Pre-processing all monitored folders");
foreach (string folder in folders)
{
Log.Logger.Information("Adding folder to processing queue : {Folder}", folder);
WatchFolders.Add(folder, DateTime.UtcNow.AddSeconds(-Program.Config.MonitorOptions.MonitorWaitTime));
}
}
}

Expand Down
15 changes: 10 additions & 5 deletions PlexCleaner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -593,17 +593,22 @@ public static CancellationToken CancelToken()
return CancelSource.Token;
}

// Commandline options
public static CommandLineOptions Options { get; internal set; }
public static ConfigFileJsonSchema Config { get; internal set; }

private static readonly CancellationTokenSource CancelSource = new();

private readonly List<string> DirectoryList = new();
private readonly List<string> FileList = new();
// Config file options
public static ConfigFileJsonSchema Config { get; internal set; }

// Snippet runtime in seconds
public static readonly TimeSpan SnippetTimeSpan = TimeSpan.FromSeconds(30);

// Interlaced detection threshold as percentage
public const double InterlacedThreshold = 5.0 / 100.0;

// Cancellation token
private static readonly CancellationTokenSource CancelSource = new();

// File and directory lists
private readonly List<string> DirectoryList = new();
private readonly List<string> FileList = new();
}
4 changes: 1 addition & 3 deletions PlexCleaner/SevenZipTool.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -79,8 +78,7 @@ protected override bool GetLatestVersionWindows(out MediaToolInfo mediaToolInfo)
// Load the download page
// TODO: Find a more reliable way of getting the latest release
// https://www.7-zip.org/download.html
using HttpClient httpClient = new();
var downloadPage = httpClient.GetStringAsync("https://www.7-zip.org/download.html").Result;
var downloadPage = Download.GetHttpClient().GetStringAsync("https://www.7-zip.org/download.html").Result;

// Extract the version number from the page source
// E.g. "Download 7-Zip 22.01 (2022-07-15):"
Expand Down
11 changes: 8 additions & 3 deletions PlexCleaner/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,13 +262,18 @@ public static bool CheckForNewTools()
{
// Get the latest version of each tool
Log.Logger.Information("Getting latest version of {Tool} ...", mediaTool.GetToolFamily());
if (!mediaTool.GetLatestVersion(out MediaToolInfo latestToolInfo) ||
// Get the URL details
!GetUrlDetails(latestToolInfo))
if (!mediaTool.GetLatestVersion(out MediaToolInfo latestToolInfo))
{
Log.Logger.Error("{Tool} : Failed to get latest version", mediaTool.GetToolFamily());
return false;
}

// Get the URL details
if (!GetUrlDetails(latestToolInfo))
{
Log.Logger.Error("{Tool} : Failed to get download URI details : {Uri}", mediaTool.GetToolFamily(), latestToolInfo.Url);
return false;
}

// Lookup in JSON file using the tool family
MediaToolInfo jsonToolInfo = toolInfoJson.GetToolInfo(mediaTool);
Expand Down

0 comments on commit 03371c9

Please sign in to comment.