136 lines
4.9 KiB
C#
136 lines
4.9 KiB
C#
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.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Test_Merge
|
|
{
|
|
internal class F1TVEmulator
|
|
{
|
|
public const string COOKIE_HOST = ".formula1.com";
|
|
public const string PYTHON_COOKIE_RETRIEVAL_FILENAME = "recoverCookiesCSV.py";
|
|
public const string GECKODRIVER_FILENAME = @"geckodriver-v0.27.0-win32\geckodriver.exe";
|
|
//BE CAREFULL IF YOU CHANGE IT HERE YOU NEED TO CHANGE IT IN THE PYTHON SCRIPT TOO
|
|
public const string COOKIES_CSV_FILENAME = "cookies.csv";
|
|
|
|
private FirefoxDriver Driver;
|
|
|
|
private bool _ready;
|
|
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;
|
|
Ready = false;
|
|
}
|
|
private void StartCookieRecovering()
|
|
{
|
|
string scriptPath = PYTHON_COOKIE_RETRIEVAL_FILENAME;
|
|
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_FILENAME))
|
|
{
|
|
// 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;
|
|
}
|
|
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();
|
|
}
|
|
}
|
|
}
|