login.mjs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { chromium } from "playwright";
  2. import uniqueRandomArray from "unique-random-array";
  3. import userAgents from "top-user-agents";
  4. export async function login(email, password, proxy) {
  5. const browser = await chromium.launch({
  6. headless: true,
  7. devtools: false,
  8. });
  9. const options = {
  10. userAgent: uniqueRandomArray(userAgents)(),
  11. viewport: {
  12. width: parseInt(Math.random() * 100 + 1250),
  13. height: parseInt(Math.random() * 100 + 750),
  14. },
  15. locale: "en-CA",
  16. timezoneId: "America/Toronto",
  17. serviceWorkers: "block",
  18. proxy,
  19. };
  20. const context = await browser.newContext(options);
  21. const page = await context.newPage();
  22. let points = null;
  23. await page.route(/points$/, async (route, request) => {
  24. const response = await route.fetch();
  25. points = await response.text();
  26. await route.continue();
  27. });
  28. await page.route(
  29. /(\.png)|(\.jpg)|(\.svg)|(\.otf)|(\.woff2)|(\.ttf)|(\.mp3)/gi,
  30. (route) => route.abort()
  31. );
  32. await page.goto("https://www.pcoptimum.ca/");
  33. const link = await page.$('a > span:has-text("sign in")');
  34. link.click();
  35. await page.waitForURL("https://accounts.pcid.ca/login");
  36. await page.fill('input[id="email"]', email);
  37. await page.fill('input[id="password"]', password);
  38. try {
  39. await new Promise(async (resolve, reject) => {
  40. await page.route(/\/login$/, async (route, request) => {
  41. console.log(
  42. `${request.method()} ${request.resourceType()} ${request.url()}`
  43. );
  44. if (request.method() === "POST") {
  45. const response = await route.fetch();
  46. const status = response.status();
  47. console.log(status);
  48. if (status === 200) {
  49. await route.continue();
  50. resolve();
  51. } else if (status === 401) {
  52. reject(new Error("Invalid email or password"));
  53. } else if (status === 429) {
  54. reject(
  55. new Error(
  56. "Oops, something went wrong. Please try again later."
  57. )
  58. );
  59. } else {
  60. reject(
  61. new Error("Unknown error. Please try again later.")
  62. );
  63. }
  64. } else {
  65. await route.continue();
  66. }
  67. });
  68. await page.click('button:has-text("Sign in")');
  69. });
  70. } catch (error) {
  71. await browser.close();
  72. throw error;
  73. }
  74. try {
  75. await page.waitForURL("https://www.pcoptimum.ca/dashboard");
  76. for (let i = 0; i < 30; i++) {
  77. await page.waitForTimeout(1000);
  78. if (points) break;
  79. }
  80. } catch (error) {
  81. console.error(error.stack);
  82. }
  83. let cookies = null;
  84. try {
  85. cookies = JSON.stringify(await context.cookies());
  86. } catch (error) {}
  87. await browser.close();
  88. return { points, cookies, options };
  89. }