login.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. timeout: 120000,
  10. });
  11. const options = {
  12. userAgent: uniqueRandomArray(userAgents)(),
  13. viewport: {
  14. width: parseInt(Math.random() * 100 + 1250),
  15. height: parseInt(Math.random() * 100 + 750),
  16. },
  17. locale: "en-CA",
  18. timezoneId: "America/Toronto",
  19. serviceWorkers: "block",
  20. };
  21. if (proxy) {
  22. console.log("Using proxy", proxy);
  23. options.proxy = proxy;
  24. }
  25. const context = await browser.newContext(options);
  26. const page = await context.newPage();
  27. await setupRoute(context);
  28. let points = null;
  29. await context.route(/points$/, async (route, request) => {
  30. const response = await route.fetch();
  31. points = await response.text();
  32. await route.continue();
  33. });
  34. await page.goto("https://www.pcoptimum.ca/login");
  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. timeout: 120000,
  77. });
  78. for (let i = 0; i < 30; i++) {
  79. await page.waitForTimeout(1000);
  80. if (points) break;
  81. }
  82. } catch (error) {
  83. console.error(error.stack);
  84. }
  85. let cookies = null;
  86. try {
  87. cookies = JSON.stringify(await context.cookies());
  88. } catch (error) {}
  89. await browser.close();
  90. return { points, cookies, options };
  91. }