Lost a lot of functionnality but at least it works

This commit is contained in:
2023-05-02 09:36:09 +02:00
parent 66e8699afe
commit 044a00e167
2 changed files with 60 additions and 187 deletions
+56 -183
View File
@@ -1,22 +1,18 @@
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;
namespace Test_Merge
{
public class F1TVEmulator
internal class F1TVEmulator
{
public const string COOKIE_HOST = ".formula1.com";
public const string PYTHON_COOKIE_RETRIEVAL_FILENAME = "recoverCookiesCSV.py";
@@ -30,7 +26,6 @@ namespace Test_Merge
private string _grandPrixUrl;
public string GrandPrixUrl { get => _grandPrixUrl; private set => _grandPrixUrl = value; }
public bool Ready { get => _ready; set => _ready = value; }
public F1TVEmulator(string grandPrixUrl)
{
GrandPrixUrl = grandPrixUrl;
@@ -38,7 +33,7 @@ namespace Test_Merge
}
private void StartCookieRecovering()
{
string scriptPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), PYTHON_COOKIE_RETRIEVAL_FILENAME);
string scriptPath = PYTHON_COOKIE_RETRIEVAL_FILENAME;
Process process = new Process();
process.StartInfo.FileName = "python.exe";
process.StartInfo.Arguments = scriptPath;
@@ -48,179 +43,6 @@ namespace Test_Merge
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
}
public async Task<int> Start()
{
//SETUP
//Those values can be changed to alter the way that the selenium driver looks and behaves in general
Ready = false;
string loginCookieName = "login";
string loginSessionCookieName = "login-session";
string loginCookieValue = GetCookie(COOKIE_HOST, loginCookieName);
string loginSessionValue = GetCookie(COOKIE_HOST, loginSessionCookieName);
int windowWidth = 1920;
int windowHeight = 1080;
var service = FirefoxDriverService.CreateDefaultService(AppDomain.CurrentDomain.BaseDirectory + GECKODRIVER_FILENAME);
service.Host = "127.0.0.1";
service.Port = 5555;
FirefoxOptions options = new FirefoxOptions();
options.SetPreference("media.peerconnection.enabled", false);
//FirefoxProfile profile = new FirefoxProfile();
//profile.SetPreference("layout.css.devPixelsPerPx", "2.0");
//options.Profile = profile;
FirefoxProfile profile = new FirefoxProfile();
profile.SetPreference("media.navigator.video.default_width", 1920);
profile.SetPreference("media.navigator.video.default_height", 1080);
options.Profile = profile;
options.AddArgument("--window-size=" + windowWidth + "," + windowHeight + "");
options.AddArgument("--disable-web-security");
options.AddArgument("--same-site-none-secure");
options.AcceptInsecureCertificates = true;
//Headless part
options.AddArgument("--headless");
options.AddArgument("--silent-launch");
options.AddArgument("--no-startup-window");
//ACTUAL STARTUP
try
{
Driver = new FirefoxDriver(service, options);
}
catch
{
//Could not start the driver, could be because an other instance is running. make sure its closed before re attempting
return 101;
}
var loginCookie = new Cookie(loginCookieName, loginCookieValue, COOKIE_HOST, "/", DateTime.Now.AddDays(5));
var loginSessionCookie = new Cookie(loginSessionCookieName, loginSessionValue, COOKIE_HOST, "/", DateTime.Now.AddDays(5));
Actions actions = new Actions(Driver);
Driver.Navigate().GoToUrl("https://f1tv.formula1.com/");
//TEST
//Driver.Manage().Window.Position = new Point(0, 0);
//Driver.Manage().Window.Size = new Size(1920, 1080);
Screenshot screenshot = ((ITakesScreenshot)Driver).GetScreenshot();
screenshot.SaveAsFile(@"C:\Users\Moi\Desktop\imgDump\Decode\FirstTest.png", ScreenshotImageFormat.Png);
//TEST
Driver.Manage().Cookies.AddCookie(loginCookie);
Driver.Manage().Cookies.AddCookie(loginSessionCookie);
try
{
Driver.Navigate().GoToUrl(GrandPrixUrl);
}
catch
{
//The url is not a valid url
Driver.Dispose();
return 103;
}
//Waits for the page to fully load
Driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(30);
//Removes the cookie prompt
try
{
IWebElement conscentButton = Driver.FindElement(By.Id("truste-consent-button"));
conscentButton.Click();
}
catch
{
Driver.Dispose();
return 104;
}
//Again waits for the page to fully load (when you accept cookies it takes a little time for the page to load)
//Cannot use The timeout because the feed loading is not really loading so there is not event or anything
Thread.Sleep(5000);
//Switches to the Data channel
try
{
IWebElement dataChannelButton = Driver.FindElement(By.ClassName("data-button"));
dataChannelButton.Click();
}
catch
{
//If the data button does not exists its because the user is not connected
Driver.Dispose();
return 102;
}
//Open settings
// Press the space key, this should make the setting button visible
// It does not matter if the feed is paused because when changing channel it autoplays
actions.SendKeys(OpenQA.Selenium.Keys.Space).Perform();
Thread.Sleep(1000);
//Clicks on the settings Icon
try
{
IWebElement settingsButton = Driver.FindElement(By.ClassName("bmpui-ui-settingstogglebutton"));
settingsButton.Click();
IWebElement selectElement = Driver.FindElement(By.ClassName("bmpui-ui-videoqualityselectbox"));
SelectElement select = new SelectElement(selectElement);
IWebElement selectOption = selectElement.FindElement(By.CssSelector("option[value^='1080_']"));
selectOption.Click();
}
catch
{
//Sometimes it can crash because it could not get the options to show up in time. When it happens just retry
Driver.Dispose();
return 105;
}
//Makes the feed fullscreen
WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
IWebElement fullScreenButton = Driver.FindElement(By.ClassName("bmpui-ui-fullscreentogglebutton"));
fullScreenButton.Click();
//STARTUP FINISHED READY TO SCREENSHOT
Ready = true;
return 0;
}
public void Stop()
{
Ready = false;
Driver.Dispose();
}
public Bitmap Screenshot()
{
//bitmovinplayer-video-slave-embeddedPlayer
Bitmap result = new Bitmap(100, 100);
if (Ready)
{
/*
Screenshot scrsht = ((ITakesScreenshot)Driver).GetScreenshot();
// Get the raw bytes of the screenshot
*/
//Driver.Manage().Window.Position = new Point(0, 0);
//Driver.Manage().Window.Size = new Size(1920, 1080);
IWebElement element = Driver.FindElement(By.Id("bitmovinplayer-video-slave-embeddedPlayer"));
Screenshot scrsht = ((ITakesScreenshot)element).GetScreenshot();
byte[] screenshotBytes = Convert.FromBase64String(scrsht.AsBase64EncodedString);
// Create a new MemoryStream from the bytes
MemoryStream stream = new MemoryStream(screenshotBytes);
// Create a new Bitmap object from the MemoryStream
result = new Bitmap(stream);
}
result.Save(@"C:\Users\Moi\Desktop\imgDump\Decode\Test.png");
return result;
}
public string GetCookie(string host, string name)
{
StartCookieRecovering();
@@ -258,5 +80,56 @@ namespace Test_Merge
return value;
}
public async Task<int> Start()
{
var service = FirefoxDriverService.CreateDefaultService(AppDomain.CurrentDomain.BaseDirectory + @"geckodriver-v0.27.0-win64\geckodriver.exe");
service.Host = "127.0.0.1";
service.Port = 5555;
FirefoxOptions options = new FirefoxOptions();
options.AcceptInsecureCertificates = true;
options.AddArgument("--headless");
options.AddArgument("--width=1920");
options.AddArgument("--height=1200");
try
{
Driver = new FirefoxDriver(service, options);
Ready = true;
}
catch
{
Ready = false;
return 101;
}
Driver.Navigate().GoToUrl("https://f1tv.formula1.com/");
Thread.Sleep(2000);
Screenshot("AAAAAAAAAAA");
return 0;
}
public Bitmap Screenshot(string name = "TEST")
{
Bitmap result = new Bitmap(100, 100);
if (Ready)
{
//IWebElement element = Driver.FindElement(By.Id("bitmovinplayer-video-slave-embeddedPlayer"));
//Screenshot scrsht = ((ITakesScreenshot)element).GetScreenshot();
Screenshot scrsht = ((ITakesScreenshot)Driver).GetScreenshot();
byte[] screenshotBytes = Convert.FromBase64String(scrsht.AsBase64EncodedString);
MemoryStream stream = new MemoryStream(screenshotBytes);
result = new Bitmap(stream);
}
result.Save(name+".png");
return result;
}
public void Stop()
{
Ready = false;
Driver.Dispose();
}
}
}
+4 -4
View File
@@ -144,15 +144,15 @@
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\Selenium.Firefox.WebDriver.0.27.0\build\Selenium.Firefox.WebDriver.targets" Condition="Exists('..\packages\Selenium.Firefox.WebDriver.0.27.0\build\Selenium.Firefox.WebDriver.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Selenium.Firefox.WebDriver.0.27.0\build\Selenium.Firefox.WebDriver.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Selenium.Firefox.WebDriver.0.27.0\build\Selenium.Firefox.WebDriver.targets'))" />
<Error Condition="!Exists('..\packages\Selenium.WebDriver.4.8.2\build\Selenium.WebDriver.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Selenium.WebDriver.4.8.2\build\Selenium.WebDriver.targets'))" />
<Error Condition="!Exists('..\packages\Tesseract.5.2.0\build\Tesseract.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Tesseract.5.2.0\build\Tesseract.targets'))" />
<Error Condition="!Exists('..\packages\Selenium.WebDriver.4.8.2\build\Selenium.WebDriver.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Selenium.WebDriver.4.8.2\build\Selenium.WebDriver.targets'))" />
<Error Condition="!Exists('..\packages\Selenium.Firefox.WebDriver.0.27.0\build\Selenium.Firefox.WebDriver.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Selenium.Firefox.WebDriver.0.27.0\build\Selenium.Firefox.WebDriver.targets'))" />
</Target>
<Import Project="..\packages\Selenium.WebDriver.4.8.2\build\Selenium.WebDriver.targets" Condition="Exists('..\packages\Selenium.WebDriver.4.8.2\build\Selenium.WebDriver.targets')" />
<Import Project="..\packages\Tesseract.5.2.0\build\Tesseract.targets" Condition="Exists('..\packages\Tesseract.5.2.0\build\Tesseract.targets')" />
<Import Project="..\packages\Selenium.WebDriver.4.8.2\build\Selenium.WebDriver.targets" Condition="Exists('..\packages\Selenium.WebDriver.4.8.2\build\Selenium.WebDriver.targets')" />
<Import Project="..\packages\Selenium.Firefox.WebDriver.0.27.0\build\Selenium.Firefox.WebDriver.targets" Condition="Exists('..\packages\Selenium.Firefox.WebDriver.0.27.0\build\Selenium.Firefox.WebDriver.targets')" />
</Project>