-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_service.js
148 lines (114 loc) · 5.36 KB
/
api_service.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const puppeteer = require('puppeteer');
const express = require('express');
const app = express();
const port = 3000;
// http://localhost:3000/scrape?username=TUO-USERNAME&password=TUA-PASSWORD richiesta http per login e scrape
// Variabili globali
let iliad_login_link = 'https://www.iliad.it/account/';
let xpathCredito = '//*[@id="container"]/div/div/div[2]/div[2]/div/div/div/h2/b';
let xpathIdUser = '//*[@id="account-login"]/div[1]/div[2]/div/div[4]/div[1]/form/div[1]/label[1]/input';
let xpathPassword = '//*[@id="account-login"]/div[1]/div[2]/div/div[4]/div[1]/form/div[1]/label[2]/input';
let xpathAccediButton = '//*[@id="account-login"]/div[1]/div[2]/div/div[4]/div[1]/form/div[2]/button';
let xpathTipoOfferta = '//*[@id="container"]/div/div/div[2]/div[2]/div/div/div/h2/span';
let xpathRecuperaPassword = '//*[@id="account-login"]/div[1]/div[2]/div/div[4]/div[1]/form/div[3]/a';
let xpathMinutiChiamata = '//*[@id="container"]/div/div/div[2]/div[2]/div/div/div/div[3]/div[1]/div[1]/div/div[1]/span[1]';
let xpathGbConsumati = '//*[@id="container"]/div/div/div[2]/div[2]/div/div/div/div[3]/div[2]/div[1]/div/div[1]/span[1]';
let xpathMessaggiInviati = '//*[@id="container"]/div/div/div[2]/div[2]/div/div/div/div[3]/div[1]/div[2]/div/div[1]/span[1]';
let xpathMMSInviati = '//*[@id="container"]/div/div/div[2]/div[2]/div/div/div/div[3]/div[2]/div[2]/div/div[1]/span[1]';
let xpathRinnovo = '//*[@id="container"]/div/div/div[2]/div[2]/div/div/div/div[2]';
// Funzione per effettuare il login
async function login(page, username, password) {
try {
await page.goto(iliad_login_link);
await page.waitForXPath(xpathIdUser);
const idInput = await page.$x(xpathIdUser);
await idInput[0].type(username);
await page.waitForXPath(xpathPassword);
const passwordInput = await page.$x(xpathPassword);
await passwordInput[0].type(password);
await page.waitForXPath(xpathAccediButton);
const loginButton = await page.$x(xpathAccediButton);
await loginButton[0].click();
await page.waitForNavigation();
console.log('Login successful');
} catch (error) {
console.error('An error occurred during login:', error);
throw error; // Rilancia l'errore per gestirlo nel chiamante
}
}
// Funzione per ottenere i dati di scraping
async function scrapeData(username, password) {
const browser = await puppeteer.launch({
headless: true
});
const page = await browser.newPage();
try {
await login(page, username, password)
// Wait for the element identified by xpathCredito
await page.waitForXPath(xpathCredito);
//offerta
const tipoOffertaElement = await page.$x(xpathTipoOfferta);
const offertaText = await page.evaluate(element => element.textContent, tipoOffertaElement[0]);
//console.log('Offerta:', offertaText);
//rinnovo
const rinnovoElement = await page.$x(xpathRinnovo);
const rinnovoText = await page.evaluate(element => element.textContent, rinnovoElement[0])
const rinnovoTextTrimmed = rinnovoText.trim();
//console.log('Rinnovo:', rinnovoTextTrimmed);
//credito
const creditoElement = await page.$x(xpathCredito);
const creditoText = await page.evaluate(element => element.textContent, creditoElement[0]);
//console.log('Credito:', creditoText);
//minuti
const minutiElement = await page.$x(xpathMinutiChiamata);
const minutiText = await page.evaluate(element => element.textContent, minutiElement[0]);
//console.log('Minuti:', minutiText);
//gb
const gbElement = await page.$x(xpathGbConsumati);
const gbText = await page.evaluate(element => element.textContent, gbElement[0]);
//console.log('Gb:', gbText);
//messaggi
const messaggiElement = await page.$x(xpathMessaggiInviati);
const messaggiText = await page.evaluate(element => element.textContent, messaggiElement[0]);
//console.log('Messaggi:', messaggiText);
//mms
const mmsElement = await page.$x(xpathMMSInviati);
const mmsText = await page.evaluate(element => element.textContent, mmsElement[0]);
//console.log('MMS:', mmsText);
// Creazione dell'oggetto con i dati
const scrapedData = {
offerta: offertaText,
rinnovo: rinnovoTextTrimmed,
credito: creditoText,
minuti: minutiText,
gigabyte_usati: gbText,
messaggi: messaggiText,
mms: mmsText,
// ... altre proprietà ...
};
// Converti l'oggetto in formato JSON
const jsonData = JSON.stringify(scrapedData, null, 2);
// Stampa il JSON sulla console
console.log(jsonData);
return scrapedData;
} catch (error) {
throw error;
} finally {
await browser.close();
}
}
app.get('/scrape', async (req, res) => {
const { username, password } = req.query;
if (!username || !password) {
return res.status(400).json({ error: 'Username and password are required' });
}
try {
const data = await scrapeData(username, password);
res.json(data);
} catch (error) {
res.status(500).json({ error: 'An error occurred' });
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});