From df43c9323441a63fa738f4054e2c926997b9af49 Mon Sep 17 00:00:00 2001 From: Yair Peretz Date: Sun, 4 Sep 2022 14:21:55 +0300 Subject: [PATCH] Fixed driver's user agent --- SdarotAPI/Resources/Constants.cs | 1 + SdarotAPI/SdarotAPI.csproj | 1 + SdarotAPI/SdarotDriver.cs | 2 ++ SdarotAPI/SdarotHelper.cs | 4 ++-- 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/SdarotAPI/Resources/Constants.cs b/SdarotAPI/Resources/Constants.cs index 3f832b7..57e5839 100644 --- a/SdarotAPI/Resources/Constants.cs +++ b/SdarotAPI/Resources/Constants.cs @@ -42,4 +42,5 @@ public static class IdSelectors public const int WaitTime = 30; public const string Error2Message = "שגיאה 2!"; public const string LoginMessage = "התחברות לאתר"; + public const string UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"; } diff --git a/SdarotAPI/SdarotAPI.csproj b/SdarotAPI/SdarotAPI.csproj index 98d7828..789b1bc 100644 --- a/SdarotAPI/SdarotAPI.csproj +++ b/SdarotAPI/SdarotAPI.csproj @@ -8,6 +8,7 @@ + diff --git a/SdarotAPI/SdarotDriver.cs b/SdarotAPI/SdarotDriver.cs index 964712f..048e39c 100644 --- a/SdarotAPI/SdarotDriver.cs +++ b/SdarotAPI/SdarotDriver.cs @@ -25,6 +25,7 @@ public async Task Initialize(bool headless = true) if (headless) { options.AddArgument("headless"); + options.AddArgument("user-agent=" + Constants.UserAgent); options.AddArgument("--remote-debugging-port=9222"); } @@ -280,6 +281,7 @@ public async Task GetEpisodeMediaDetailsAsync(EpisodeInform { throw new Error2Exception(); } + throw new WebsiteErrorException(); } diff --git a/SdarotAPI/SdarotHelper.cs b/SdarotAPI/SdarotHelper.cs index 307eb0e..f425aab 100644 --- a/SdarotAPI/SdarotHelper.cs +++ b/SdarotAPI/SdarotHelper.cs @@ -17,7 +17,7 @@ public static async Task DownloadEpisode(EpisodeMediaDetails episode, string dow { using var handler = new HttpClientHandler() { CookieContainer = episode.Cookies }; using var client = new HttpClient(handler); - client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36"); + client.DefaultRequestHeaders.Add("User-Agent", Constants.UserAgent); using var file = new FileStream(downloadLocation, FileMode.Create, FileAccess.Write, FileShare.None); await client.DownloadAsync(episode.MediaUrl, file, progress, ct); } @@ -26,7 +26,7 @@ public static async Task DownloadEpisode(EpisodeMediaDetails episode, string dow { using var handler = new HttpClientHandler() { CookieContainer = episode.Cookies }; using var client = new HttpClient(handler); - client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36"); + client.DefaultRequestHeaders.Add("User-Agent", Constants.UserAgent); // Get the http headers first to examine the content length using var response = await client.GetAsync(episode.MediaUrl, HttpCompletionOption.ResponseHeadersRead); return response.Content.Headers.ContentLength;