-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
123 lines (106 loc) · 4.44 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const puppeteer = require("puppeteer");
const express = require("express");
const app = express();
app.use(express.json());
let browser, page, frame, photosFrame;
const delay = (time) =>
new Promise((resolve, reject) => {
setTimeout(() => {
resolve("");
}, time);
});
app.post("/login", async (req, res) => {
const { ph, pwd } = req.body;
appleLogin(ph, pwd);
res.status(200).json({ msg: "plsease enter otp" });
});
app.post("/otp", async (req, res) => {
const { otp } = req.body;
console.log({ otp });
appleOtp(otp);
res.status(200).json({ msg: "success" });
});
const appleLogin = async (ph, pwd) => {
browser = await puppeteer.launch({ headless: false });
page = await browser.newPage();
try {
// Go to iCloud login page
await page.goto("https://www.icloud.com/", { waitUntil: "networkidle2" });
await page.setViewport({ width: 1920, height: 1080 });
// Wait for and type the Apple ID
await page.waitForSelector(".sign-in-button", { timeout: 60000 });
await page.click(".sign-in-button");
// Wait for the iframe and switch to it
await page.waitForSelector("iframe", { timeout: 60000 });
const loginFrame = await page.$("iframe");
frame = await loginFrame.contentFrame();
// Wait for and type the Apple ID
await frame.waitForSelector("input#account_name_text_field", { timeout: 60000 });
await frame.type("input#account_name_text_field", ph, { delay: 50 });
await frame.click("button#sign-in");
await frame.waitForSelector("#continue-password", { timeout: 60000 });
await frame.click("#continue-password");
// Wait for and type the password
await frame.waitForSelector("input#password_text_field", { timeout: 60000 });
await frame.type("input#password_text_field", pwd, { delay: 50 });
await frame.click("button#sign-in");
} catch (error) {
console.error("Login failed:", error);
} finally {
// await browser.close();
}
};
const appleOtp = async (otp) => {
try {
// 6 inputs
const inputs = otp.split("");
await frame.waitForSelector("input[aria-label='Please enter the verification code Digit 1']", { timeout: 60000 });
await frame.type("input[aria-label='Please enter the verification code Digit 1']", inputs[0], { delay: 50 });
await delay(500);
await frame.waitForSelector("input[aria-label='Digit 2']", { timeout: 60000 });
await frame.type("input[aria-label='Digit 2']", inputs[1], { delay: 50 });
await delay(500);
await frame.waitForSelector("input[aria-label='Digit 3']", { timeout: 60000 });
await frame.type("input[aria-label='Digit 3']", inputs[2], { delay: 50 });
await delay(500);
await frame.waitForSelector("input[aria-label='Digit 4']", { timeout: 60000 });
await frame.type("input[aria-label='Digit 4']", inputs[3], { delay: 50 });
await delay(500);
await frame.waitForSelector("input[aria-label='Digit 5']", { timeout: 60000 });
await frame.type("input[aria-label='Digit 5']", inputs[4], { delay: 50 });
await delay(500);
await frame.waitForSelector("input[aria-label='Digit 6']", { timeout: 60000 });
await frame.type("input[aria-label='Digit 6']", inputs[5], { delay: 50 });
// Wait for and type the Apple ID
// Wait for and type the password
await frame.waitForSelector("button.button-rounded-rectangle", { timeout: 60000 });
await frame.click("button.button-rounded-rectangle", { delay: 50 });
await page.waitForSelector("a[href='https://www.icloud.com/photos']", { timeout: 60000 });
await page.click('a[href="https://www.icloud.com/photos"]', { delay: 50 });
await page.waitForNavigation({ waitUntil: "networkidle2" });
await page.waitForSelector("iframe", { timeout: 60000 });
const photosiframe = await page.$$("iframe");
console.log({ photosiframe, length: photosiframe.length });
const photosFrame = await photosiframe[1].contentFrame();
const links = await photosFrame.$$(".grid-items .grid-item img");
for (let link of links) {
await link.click();
await delay(500);
await photosFrame.waitForSelector(".DownloadButton", { timeout: 60000 });
await photosFrame.click(".DownloadButton", { delay: 50 });
}
console.log("Login successful");
} catch (error) {
console.error("Login failed:", error);
} finally {
// await browser.close();
}
};
const start = async () => {
try {
app.listen(3200, () => {
console.log("APIs are running on port 3200");
});
} catch (error) {}
};
start();