Compare commits
6 Commits
main
...
3aa8b85c59
| Author | SHA1 | Date | |
|---|---|---|---|
| 3aa8b85c59 | |||
| 869b0b466e | |||
| 430c5fd2b8 | |||
| 28bad6c3c1 | |||
| 7684e8869b | |||
| 99eb077d14 |
@@ -1,3 +1,7 @@
|
||||
[Pp]assword.txt
|
||||
CVV.txt
|
||||
CardDetails.txt
|
||||
|
||||
# ---> VisualStudioCode
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Playwright" Version="1.59.0" />
|
||||
<PackageReference Include="Soenneker.Playwrights.Extensions.Stealth" Version="4.0.62" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="Password.txt" CopyToOutputDirectory="PreserveNewest" />
|
||||
<None Include="CardDetails.txt" CopyToOutputDirectory="PreserveNewest" />
|
||||
<None Include="CVV.txt" CopyToOutputDirectory="PreserveNewest" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,24 @@
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.5.2.0
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BadmintonBooker", "BadmintonBooker.csproj", "{64D6B026-79C7-8E9B-78B3-7E47FF75FA6D}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{64D6B026-79C7-8E9B-78B3-7E47FF75FA6D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{64D6B026-79C7-8E9B-78B3-7E47FF75FA6D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{64D6B026-79C7-8E9B-78B3-7E47FF75FA6D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{64D6B026-79C7-8E9B-78B3-7E47FF75FA6D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {30C191B7-3059-49DA-8326-9D39D1120073}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
@@ -0,0 +1,59 @@
|
||||
using System.Diagnostics;
|
||||
using System.Net.Sockets;
|
||||
using Microsoft.Playwright;
|
||||
|
||||
namespace BadmintonBooker;
|
||||
|
||||
internal static class BrowserManager
|
||||
{
|
||||
private const int DebugPort = 9222;
|
||||
private const string ChromeExe = @"C:\Program Files\Google\Chrome\Application\chrome.exe";
|
||||
private const string UserDataDir = @"C:\Users\Shaamil\PlaywrightData";
|
||||
|
||||
public static async Task<IBrowserContext> GetContextAsync(IPlaywright playwright)
|
||||
{
|
||||
await EnsureChromeRunningAsync();
|
||||
var browser = await playwright.Chromium.ConnectOverCDPAsync($"http://127.0.0.1:{DebugPort}");
|
||||
return browser.Contexts[0];
|
||||
}
|
||||
|
||||
private static async Task EnsureChromeRunningAsync()
|
||||
{
|
||||
if (IsPortListening(DebugPort))
|
||||
return;
|
||||
|
||||
Process.Start(new ProcessStartInfo
|
||||
{
|
||||
FileName = ChromeExe,
|
||||
Arguments = $"--remote-debugging-port={DebugPort} --user-data-dir=\"{UserDataDir}\"",
|
||||
UseShellExecute = true
|
||||
});
|
||||
|
||||
// Wait for Chrome to bind the debug port
|
||||
const int maxWaitMs = 10000;
|
||||
const int intervalMs = 500;
|
||||
var elapsed = 0;
|
||||
|
||||
while (!IsPortListening(DebugPort) && elapsed < maxWaitMs)
|
||||
{
|
||||
await Task.Delay(intervalMs);
|
||||
elapsed += intervalMs;
|
||||
}
|
||||
|
||||
if (!IsPortListening(DebugPort))
|
||||
throw new TimeoutException("Chrome did not open the remote debugging port in time.");
|
||||
}
|
||||
|
||||
private static bool IsPortListening(int port)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var tcp = new TcpClient();
|
||||
tcp.Connect("127.0.0.1", port);
|
||||
return true;
|
||||
}
|
||||
catch { return false; }
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using Microsoft.Playwright;
|
||||
|
||||
namespace BadmintonBooker;
|
||||
|
||||
public static class GoogleMessages
|
||||
{
|
||||
public static async Task<string?> GetEgovOTP(IBrowserContext context, IPage originalPage)
|
||||
{
|
||||
var msgPage = await context.NewPageAsync();
|
||||
await msgPage.GotoAsync("https://messages.google.com/web/u/0/conversations/CgjwUsdGGwxxGhIBNw", new PageGotoOptions { WaitUntil = WaitUntilState.Load });
|
||||
|
||||
var messageElement = msgPage.Locator("mws-text-message-part").Last;
|
||||
string messageText = await messageElement.InnerTextAsync();
|
||||
|
||||
await originalPage.BringToFrontAsync();
|
||||
|
||||
var match = Regex.Match(messageText, @"\b\d{5,}\b");
|
||||
if (match.Success)
|
||||
{
|
||||
return match.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("No OTP found in the message.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<string?> GetBMLOTP(IBrowserContext context, IPage originalPage)
|
||||
{
|
||||
var msgPage = await context.NewPageAsync();
|
||||
await msgPage.GotoAsync("https://messages.google.com/web/u/0/conversations/Cgj4KNDeDzFopxIBMQ", new PageGotoOptions { WaitUntil = WaitUntilState.Load });
|
||||
|
||||
var messageElement = msgPage.Locator("mws-text-message-part").Last;
|
||||
string messageText = await messageElement.InnerTextAsync();
|
||||
|
||||
await originalPage.BringToFrontAsync();
|
||||
|
||||
var match = Regex.Match(messageText, @"\b\d{6,}\b");
|
||||
if (match.Success)
|
||||
{
|
||||
return match.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("No OTP found in the message.");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<string> GetMessageFromHDC(IBrowserContext context, IPage originalPage)
|
||||
{
|
||||
var msgPage = await context.NewPageAsync();
|
||||
await msgPage.GotoAsync("https://messages.google.com/web/u/0/conversations/Cghm7Ql96iXyyhIBMg", new PageGotoOptions { WaitUntil = WaitUntilState.Load });
|
||||
|
||||
var messageElement = msgPage.Locator("mws-text-message-part").Last;
|
||||
string messageText = await messageElement.InnerTextAsync();
|
||||
|
||||
await originalPage.BringToFrontAsync();
|
||||
return messageText;
|
||||
}
|
||||
}
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using BadmintonBooker;
|
||||
using Microsoft.Playwright;
|
||||
|
||||
var argsLength = args.Length;
|
||||
if(argsLength < 1)
|
||||
{
|
||||
Console.WriteLine("Useage: BadmintonBooker <endtime (HH:mm)> <debug? (true/false)>");
|
||||
return;
|
||||
}
|
||||
var endTime = args[0];
|
||||
if (!TimeSpan.TryParse(endTime, out var endTimeSpan))
|
||||
{
|
||||
Console.WriteLine("Invalid end time format. Use HH:mm");
|
||||
return;
|
||||
}
|
||||
|
||||
var debug = false;
|
||||
if (argsLength == 2)
|
||||
{
|
||||
if (!bool.TryParse(args[1], out debug))
|
||||
{
|
||||
Console.WriteLine("Invalid debug value. Use true or false");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
using var playwright = await Playwright.CreateAsync();
|
||||
var context = await BrowserManager.GetContextAsync(playwright);
|
||||
var page = context.Pages.Count > 0 ? context.Pages[0] : await context.NewPageAsync();
|
||||
await page.GotoAsync("https://my.hdc.mv/");
|
||||
await page.GetByRole(AriaRole.Link, new() { Name = "Login" }).ClickAsync();
|
||||
await page.GetByRole(AriaRole.Button, new() { Name = "eFaas" }).ClickAsync();
|
||||
await page.GetByRole(AriaRole.Tab, new() { Name = "Password Login" }).ClickAsync(new () { Delay = 1000 });
|
||||
await page.GetByPlaceholder("Username").FillAsync("A384347");
|
||||
var password = File.ReadAllText("password.txt");
|
||||
await page.GetByPlaceholder("Password").FillAsync(password);
|
||||
await page.GetByRole(AriaRole.Button, new() { Name = "LOGIN" }).ClickAsync();
|
||||
await page.GetByRole(AriaRole.Button, new() { Name = "CONTINUE" }).ClickAsync(new () { Delay = 1000 });
|
||||
await Task.Delay(5000);
|
||||
|
||||
var egovOtp = await GoogleMessages.GetEgovOTP(context, page);
|
||||
if (egovOtp is not null)
|
||||
Console.WriteLine($"Extracted eGov OTP: {egovOtp}");
|
||||
else
|
||||
return;
|
||||
|
||||
await page.GetByPlaceholder("OTP").FillAsync(egovOtp);
|
||||
await page.GetByRole(AriaRole.Button, new() { Name = "CONTINUE" }).ClickAsync();
|
||||
await page.GetByText("Make a booking").ClickAsync();
|
||||
await page.GetByText("Fahiveni").ClickAsync();
|
||||
await page.GetByText("Sports and Leisure").ClickAsync();
|
||||
await page.GetByText(" Fahiveni Community Center ").First.ClickAsync(new () { Delay = 1500 });
|
||||
await page.GetByText("Badminton Court 1").ClickAsync(new () { Delay = 1500 });
|
||||
|
||||
var currentDateTime = DateTime.Now;
|
||||
var upperBound = new DateTime(currentDateTime.Year, currentDateTime.Month, currentDateTime.Day,
|
||||
hour: 8, minute: 15, second: 0);
|
||||
|
||||
var lowerBound = new DateTime(currentDateTime.Year, currentDateTime.Month, currentDateTime.Day,
|
||||
hour: 7, minute: 59, second: 59, millisecond: 800);
|
||||
|
||||
var lastReloadTime = DateTime.Now;
|
||||
while (!debug && (currentDateTime < lowerBound || currentDateTime >= upperBound))
|
||||
{
|
||||
if(lowerBound - currentDateTime > TimeSpan.FromSeconds(10) &&
|
||||
lastReloadTime - currentDateTime > TimeSpan.FromMinutes(2))
|
||||
{
|
||||
await page.ReloadAsync();
|
||||
lastReloadTime = DateTime.Now;
|
||||
}
|
||||
|
||||
await Task.Delay(5);
|
||||
currentDateTime = DateTime.Now;
|
||||
}
|
||||
|
||||
await page.Locator("div.flex.items-center.justify-end > div > div:nth-child(2) > svg").ClickAsync();
|
||||
await page.Locator("div.grid.bg-gray-100.rounded-lg.anim").Filter(new () { HasText = endTime }).ClickAsync();
|
||||
await page.GetByRole(AriaRole.Button, new() { Name = "Submit" }).ClickAsync();
|
||||
await page.GetByRole(AriaRole.Link, new() { Name = "Proceed to payment" }).ClickAsync();
|
||||
await page.GetByRole(AriaRole.Button, new() { Name = "Pay Now" }).ClickAsync(new () { Delay = 3500 });
|
||||
await page.GetByRole(AriaRole.Checkbox, new()).ClickAsync();
|
||||
await page.GetByRole(AriaRole.Button, new() { Name = "Pay now" }).ClickAsync();
|
||||
var cardDetails = File.ReadAllLines("CardDetails.txt");
|
||||
await page.GetByPlaceholder("Name on card").FillAsync(cardDetails[0]);
|
||||
await page.GetByPlaceholder("1234 1234 1234 1234").FillAsync(cardDetails[1]);
|
||||
await page.GetByPlaceholder("MM/YY").FillAsync(cardDetails[2]);
|
||||
var cvv = File.ReadAllText("CVV.txt");
|
||||
await page.GetByPlaceholder("CVV").FillAsync(cvv);
|
||||
await page.GetByText(new Regex("^Pay")).ClickAsync(new () { Delay = 1000 });
|
||||
await Task.Delay(15000);
|
||||
var bmlOtp = await GoogleMessages.GetBMLOTP(context, page);
|
||||
if (bmlOtp is not null)
|
||||
Console.WriteLine($"Extracted BML OTP: {bmlOtp}");
|
||||
else
|
||||
return;
|
||||
await page.Locator("input[name='otpValue']").FillAsync(bmlOtp);
|
||||
await page.GetByRole(AriaRole.Button, new() { Name = "CONFIRM" }).ClickAsync();
|
||||
await Task.Delay(15000);
|
||||
|
||||
var hdcMessage = await GoogleMessages.GetMessageFromHDC(context, page);
|
||||
Console.WriteLine($"Message from HDC: {hdcMessage}");
|
||||
@@ -1,2 +1,11 @@
|
||||
# BadmintonBooker
|
||||
|
||||
Helps me book the badminton court from HDC automatically.
|
||||
Right now now configuration is added but password's been removed from the repo and I will need to create a file called "Password.txt" to work on the project.
|
||||
In the future perhaps it would be much better if I can deploy as a webapp that can be used by friends to schedule an auto purchase of a slot at a perticular time.
|
||||
|
||||
## Example Useage
|
||||
**`dotnet run -- 22:00`**
|
||||
Where `22:00` is the end time of the prefered time. `22:00` is the slot from `21:00 - 22:00`.
|
||||
|
||||
Preferably the application will be run close to 8AM around 7:45AM so that there's need to perpetually keep refreshing the site for a long time.
|
||||
Reference in New Issue
Block a user