Skip to content

Commit

Permalink
Keep MS Edge and its web driver versions in sync
Browse files Browse the repository at this point in the history
  • Loading branch information
NoxModule committed Dec 6, 2022
1 parent c6023a6 commit 6dc68de
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 46 deletions.
1 change: 1 addition & 0 deletions PlexAutoIntroSkip.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="Microsoft.Win32.Registry" Version="5.0.0" />
<PackageReference Include="Selenium.WebDriver" Version="4.0.0-beta2" />
</ItemGroup>

Expand Down
20 changes: 7 additions & 13 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ public static void Main(string[] args)
{
var options = GetProgramOptions(args);

HandleConsoleWindow(options.ShowConsoleWindow);
// Was called using own process window?
var hWnd = Process.GetCurrentProcess().MainWindowHandle;
if (hWnd.ToInt32() != 0)
{
ShowWindow(hWnd, 0);
}

var edgeOptions = new EdgeOptions
{
Expand All @@ -58,7 +63,7 @@ public static void Main(string[] args)

try
{
WebDriverManager.AutoUpdate();
WebDriverManager.AutoInstall();
}
catch (Exception exception)
{
Expand Down Expand Up @@ -91,17 +96,6 @@ public static void Main(string[] args)
Process.GetProcessById(service.ProcessId).Kill();
}

private static void HandleConsoleWindow(bool showConsoleWindow)
{
var hWnd = Process.GetCurrentProcess().MainWindowHandle;

// Hide console window and called using own process window?
if (showConsoleWindow == false && hWnd.ToInt32() != 0)
{
ShowWindow(hWnd, 0);
}
}

/// <summary>
/// Run main program loop.
/// </summary>
Expand Down
3 changes: 0 additions & 3 deletions ProgramOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ namespace PlexAutoIntroSkip
{
public class ProgramOptions
{
[Option('d', "debug", Required = false, HelpText = "Show console window.")]
public bool ShowConsoleWindow { get; set; }

[Option('w', "wait-time", Required = false, Default = 3000,
HelpText = "Time to wait after Skip Button becomes visible before clicking.")]
public int SkipButtonWaitTime { get; set; }
Expand Down
5 changes: 1 addition & 4 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ Plex has an great feature that allows users to [Skip TV Show Intros](https://sup

## Options

* `-d`, `--debug` **:** Show console window.
* `-m`, `--manual-driver` **:** Manually handle MS Edge Web Driver.
* If manually handling web driver the executable will need to be in environment's PATH or next to the `PlexAutoIntroSkip` executable.
* `-w`, `--wait-time` **:** Time to wait, in milliseconds, after Skip Button becomes visible before clicking.
* **Default:** 2500
* **Default:** 3000

## Notes

Expand Down
68 changes: 42 additions & 26 deletions WebDriverManager.cs
Original file line number Diff line number Diff line change
@@ -1,59 +1,77 @@
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Text.RegularExpressions;
using Microsoft.Win32;

namespace PlexAutoIntroSkip
{
public static class WebDriverManager
{
/// <summary>
/// MS Edge web driver executable path.
/// </summary>
public static readonly string MsEdgeDriverFilePath = Path.GetFullPath(@"./edgedriver_win64/msedgedriver.exe");

public static readonly string MsEdgeDriverDirectoryName = "edgedriver_win64";

public static readonly string MsEdgeDriverFileName = "msedgedriver.exe";

private static readonly string _stableChannelLatestVersionAddress = "https://msedgedriver.azureedge.net/LATEST_STABLE";

private static readonly string _stableChannelAddress = "https://msedgedriver.azureedge.net/<<VERSION>>/edgedriver_win64.zip";

private static readonly Regex _msEdgeDriverVersionRegEx = new(@"(\d+\.\d+\.\d+)");
private static readonly Regex _versionRegEx = new(@"^(\d+\.\d+\.\d+)");

public static void AutoUpdate()
/// <summary>
/// Automatically download and update MS Edge web driver.
/// </summary>
public static void AutoInstall()
{
var webClient = new WebClient();
var httpStream = webClient.OpenRead(_stableChannelLatestVersionAddress);
var streamReader = new StreamReader(httpStream);
var latestVersion = streamReader.ReadLine();

streamReader.Close();
httpStream.Close();
var msEdgeVersion = GetMsEdgeVersion();
var webDriverVersion = File.Exists(MsEdgeDriverFilePath) ? GetMsEdgeDriverVersion() : string.Empty;

var currentVersion = string.Empty;
if (File.Exists(MsEdgeDriverFilePath))
if (_versionRegEx.Match(msEdgeVersion).Value != webDriverVersion)
{
currentVersion = GetMsEdgeDriverVersion(MsEdgeDriverFilePath);
}
var webClient = new WebClient();

if (currentVersion != _msEdgeDriverVersionRegEx.Match(latestVersion).Value)
{
var downloadFileName = $"./{MsEdgeDriverDirectoryName}.zip";

webClient.DownloadFile(_stableChannelAddress.Replace("<<VERSION>>", latestVersion), downloadFileName);
webClient.DownloadFile(_stableChannelAddress.Replace("<<VERSION>>", msEdgeVersion), downloadFileName);
ZipFile.ExtractToDirectory(downloadFileName, MsEdgeDriverDirectoryName, overwriteFiles: true);

webClient.Dispose();
}
}

webClient.Dispose();
/// <summary>
/// Get current version of installed MS Edge.
/// </summary>
/// <returns>Version of MS Edge.</returns>
private static string GetMsEdgeVersion()
{
if (OperatingSystem.IsWindows())
{
return Registry.GetValue(
@"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Edge\BLBeacon", "version", null).ToString();
}
else
{
throw new Exception("Unable to determine current version of MS Edge on non-Windows platforms.");
}
}

private static string GetMsEdgeDriverVersion(string msEdgeDriverPath)
/// <summary>
/// Get current version of downloaded MS Edge web driver.
/// </summary>
/// <returns>Version of MS Edge web driver.</returns>
private static string GetMsEdgeDriverVersion()
{
var msEdgeDriver = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = msEdgeDriverPath,
FileName = MsEdgeDriverFilePath,
Arguments = "--version",
UseShellExecute = false,
RedirectStandardOutput = true,
Expand All @@ -62,12 +80,10 @@ private static string GetMsEdgeDriverVersion(string msEdgeDriverPath)
};

msEdgeDriver.Start();
while (msEdgeDriver.StandardOutput.EndOfStream == false)
{
return _msEdgeDriverVersionRegEx.Match(msEdgeDriver.StandardOutput.ReadLine()).Value;
}

return string.Empty;
return msEdgeDriver.StandardOutput.EndOfStream
? string.Empty
: _versionRegEx.Match(msEdgeDriver.StandardOutput.ReadLine()).Value;
}
}
}

0 comments on commit 6dc68de

Please sign in to comment.