-
-
Save sapics/4653becae57f8441c110b87d51d5c33f to your computer and use it in GitHub Desktop.
ChromeDriverUpdator .NET(C#)アプリ版
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"CHROMEDRIVER_DIRECTORY": "C:\\hogehoge", | |
"CHROMEDRIVER_FILE_NAME": "chromedriver.exe", | |
"ARCHIVE_FILE_NAME": "chromedriver_win32.zip" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Codeplex.Data; | |
using System; | |
using System.Diagnostics; | |
using System.IO; | |
using System.IO.Compression; | |
using System.Net; | |
using System.Net.Http; | |
using System.Text.RegularExpressions; | |
namespace ChromeDriverUpdator | |
{ | |
public class Program | |
{/// <summary> | |
/// 現在パスが通っているchromedriverのバージョンと、chromedriverで公開されている | |
/// 最新のchromedriverのバージョンを比較し、新しくなっている場合は更新する。 | |
/// </summary> | |
/// <param name="args"></param> | |
public static void Main(string[] args) | |
{ | |
// ChromeDriverの保存先 | |
var config = DynamicJson.Parse(File.ReadAllText("config.json")); | |
string EXE_FILE_NAME = config.CHROMEDRIVER_FILE_NAME; | |
var CURRENT_CHROMEDRIVER = $"{config.CHROMEDRIVER_DIRECTORY}\\{config.CHROMEDRIVER_FILE_NAME}"; | |
var CHROMEDRIVER_ARCHIVE_DIR = $"{config.CHROMEDRIVER_DIRECTORY}\\"; | |
// 取得するzipファイル名 | |
string ARCHIVE_FILE_NAME = config.ARCHIVE_FILE_NAME; | |
Console.WriteLine($"現行ChromeDriverパス: {CURRENT_CHROMEDRIVER}"); | |
// 最新のバージョンを取得する | |
// | |
// ↓のようなaタグからバージョン文字列を取得する。 | |
// <a name="TOC-Latest-Release:-ChromeDriver-2.28"></a> | |
// | |
var URL_DOWNLOAD_PAGE = "https://sites.google.com/a/chromium.org/chromedriver/downloads"; | |
var LATEST_VERSION = GetRequest(URL_DOWNLOAD_PAGE).Match(@"(?<=TOC-Latest-Release:-ChromeDriver-)([^ ""]+)").Value; | |
Console.WriteLine($"最新バージョン: {LATEST_VERSION}"); | |
// 今動作しているバージョン | |
var CURRENT_VERSION_FULL = GetChromeDriverVersion(CURRENT_CHROMEDRIVER); | |
//マイナーバージョンまでで切った文字列 | |
var CURRENT_VERSION = $"{CURRENT_VERSION_FULL.Major}.{CURRENT_VERSION_FULL.Minor}"; | |
Console.WriteLine($"現行バージョン: {CURRENT_VERSION} ({CURRENT_VERSION_FULL})"); | |
Console.WriteLine(""); | |
//更新がある場合 | |
if (new Version(LATEST_VERSION) > new Version(CURRENT_VERSION)) | |
{ | |
Console.WriteLine("ChromeDriverが更新されています。"); | |
//Windows向けのzipファイルを取得 | |
var ZIP_URL = "https://" + $"chromedriver.storage.googleapis.com/{LATEST_VERSION}/{ARCHIVE_FILE_NAME}"; | |
var ZIP_DEST_DIR = $"{CHROMEDRIVER_ARCHIVE_DIR}"; | |
if (!Directory.Exists(ZIP_DEST_DIR)) | |
{ | |
Console.WriteLine($"ディレクトリ{ZIP_DEST_DIR}を作成します。"); | |
Directory.CreateDirectory(ZIP_DEST_DIR); | |
} | |
Directory.SetCurrentDirectory(ZIP_DEST_DIR); | |
Console.WriteLine($"アーカイブ取得...{ZIP_URL}"); | |
new WebClient().DownloadFile(ZIP_URL, ARCHIVE_FILE_NAME); | |
Console.WriteLine("アーカイブ解凍"); | |
//すでにある場合は上書き | |
Unzip(ARCHIVE_FILE_NAME, EXE_FILE_NAME); | |
Console.WriteLine($"更新後バージョン: {GetChromeDriverVersion(CURRENT_CHROMEDRIVER)}"); | |
} | |
else | |
{ | |
Console.WriteLine("ChromeDriverは最新です。"); | |
} | |
} | |
private static void Unzip(string ARCHIVE_FILE_NAME, string EXE_FILE_NAME) | |
{ | |
using (var a = ZipFile.OpenRead(ARCHIVE_FILE_NAME)) | |
{ | |
ZipArchiveEntry e = a.GetEntry(EXE_FILE_NAME); | |
if (e == null) | |
{ | |
Console.WriteLine($"{ARCHIVE_FILE_NAME}内に{EXE_FILE_NAME} が見つかりませんでした。"); | |
} | |
else | |
{ | |
e.ExtractToFile(EXE_FILE_NAME, true); | |
} | |
} | |
} | |
private static Version GetChromeDriverVersion(string CURRENT_CHROMEDRIVER) | |
{ | |
if (!File.Exists(CURRENT_CHROMEDRIVER)) { return new Version("0.0"); } | |
var pi = new ProcessStartInfo(CURRENT_CHROMEDRIVER, "--version"); | |
pi.UseShellExecute = false; | |
pi.RedirectStandardOutput = true; | |
var p = Process.Start(pi); | |
var r = p.StandardOutput.ReadToEnd(); | |
return new Version(r.Split(new[] { ' ' })[1]); | |
} | |
private static string GetRequest(string url) | |
{ | |
var client = new HttpClient(); | |
var response = client.GetAsync(url).Result; | |
return response.Content.ReadAsStringAsync().Result; | |
} | |
} | |
public static class Extenstion | |
{ | |
public static Match Match(this string str, string regex) | |
{ | |
var re = new Regex(regex); | |
return re.Match(str); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment