Files
Selenium_Clean/Selenium_Clean/Form1.cs
2023-04-19 11:47:02 +02:00

160 lines
6.7 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System.IO;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Interactions;
namespace Selenium_Clean
{
public partial class Form1 : Form
{
public const string COOKIE_HOST = ".formula1.com";
public Form1()
{
InitializeComponent();
}
private void StartCookieRecovering()
{
string scriptPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "recoverCookiesCSV.py");
Process process = new Process();
process.StartInfo.FileName = "python.exe";
process.StartInfo.Arguments = scriptPath;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
}
public string GetCookie(string host, string name)
{
StartCookieRecovering();
string value = "";
List<Cookie> cookies = new List<Cookie>();
using (var reader = new StreamReader("cookies.csv"))
{
// Read the header row and validate column order
string header = reader.ReadLine();
string[] expectedColumns = { "host_key", "name", "value", "path", "expires_utc", "is_secure", "is_httponly" };
string[] actualColumns = header.Split(',');
for (int i = 0; i < expectedColumns.Length; i++)
{
if (expectedColumns[i] != actualColumns[i])
{
throw new InvalidOperationException($"Expected column '{expectedColumns[i]}' at index {i} but found '{actualColumns[i]}'");
}
}
// Read each data row and parse values into a Cookie object
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
string[] fields = line.Split(',');
string hostname = fields[0];
string cookieName = fields[1];
if (hostname == host && cookieName == name)
{
value = fields[2];
}
}
}
return value;
}
private void btnStart_Click(object sender, EventArgs e)
{
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-v0.27.0-win32\geckodriver.exe");
service.Host = "127.0.0.1";
service.Port = 5555;
FirefoxOptions options = new FirefoxOptions();
options.AddArgument("--window-size="+windowWidth+","+windowHeight+"");
options.AddArgument("--disable-headless");
options.AddArgument("--disable-web-security");
options.AddArgument("--same-site-none-secure");
options.AcceptInsecureCertificates = true;
using (var driver = new FirefoxDriver(service,options))
{
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/");
driver.Manage().Cookies.AddCookie(loginCookie);
driver.Manage().Cookies.AddCookie(loginSessionCookie);
driver.Navigate().GoToUrl("https://f1tv.formula1.com/detail/1000005104/2022-bahrain-grand-prix?action=play");
//Waits for the page to fully load
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(30);
//Removes the cookie prompt
IWebElement conscentButton = driver.FindElement(By.Id("truste-consent-button"));
conscentButton.Click();
//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 (OpenQA.Selenium.NoSuchElementException error)
{
//If the data button does not exists its because the user is not connected
MessageBox.Show("Could not connect to the F1TV. Please connect to it using your account on chrome and if you already did please wait the process can take a few minuts");
driver.Dispose();
Application.Exit();
}
//Open settings
// Press the space key twice, this should make the setting button visible
actions.SendKeys(OpenQA.Selenium.Keys.Space).Perform();
Thread.Sleep(1000);
//Clicks on the settings Icon
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();
//Makes the feed fullscreen
IWebElement fullScreenButton = driver.FindElement(By.ClassName("bmpui-ui-fullscreentogglebutton"));
fullScreenButton.Click();
Thread.Sleep(5000);
//Now you can start taking screenshots
}
}
}
}