login.js 3.1 KB

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