-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
179 lines (130 loc) · 5.4 KB
/
index.ts
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import * as puppeteer from "puppeteer";
import { Page } from "puppeteer";
import { wait } from "./utils";
const targetedCategories = ["15", "16"];
// const maximumPrice = "65";
async function startScraping() {
console.log("Start scrapping " + new Date());
const browser = await puppeteer.launch({
headless: "new",
// headless: false,
slowMo: 50,
});
const result: Record<string, Record<string, number>> = {};
const page: Page = await browser.newPage();
await page.goto("https://billetterie.psg.fr/fr/ticketplace");
await wait(5000);
await page.click(".didomi-continue-without-agreeing");
await wait(3000);
const ticketPlaceButtons = await page.$$(
".psgMatchCardLinks > a:nth-child(2)"
);
const visitingTeams = await page.$$(
".psgMatchCardTitle > span > span > span:nth-child(5)"
);
await wait(1000);
for (let index = 0; index < ticketPlaceButtons.length; index++) {
const button = ticketPlaceButtons[index];
const visitingTeam = visitingTeams[index];
const visitingTeamName = (await visitingTeam.getProperty("textContent"))
.toString()
.replace("JSHandle:", "")
.trim();
console.log(
`Scrapping begins for PSG - ${visitingTeamName} match ----------------------`
);
const link = await button.getProperty("href");
const matchPage = await browser.newPage();
await matchPage.goto(link.toString().replace("JSHandle:", ""));
await wait(1300);
await matchPage.click(".psgTicketplaceFastest button");
await wait(1300);
const categoriesToggle = await matchPage.$$(
"button.bookingCategoryToggle"
);
const filteredCategories = await Promise.all(
categoriesToggle.map(async (category) => {
const categoryName = await category.getProperty("textContent");
const categoryNumber = categoryName
.toString()
.replace("JSHandle:", "")
.trim()
.split(" ")[1]
.trim();
if (targetedCategories.includes(categoryNumber)) {
return { category, categoryNumber };
}
return undefined;
})
);
for (let index = 0; index < filteredCategories.length; index++) {
const category = filteredCategories[index];
let categoryMinimumPrice: number | undefined = undefined;
if (!category) continue;
console.log(
`Price check for category ${category?.categoryNumber}...`
);
await category.category.click();
await wait(2000);
const accessButtons = await matchPage.$$(
`.bookingCategory:nth-child(${index + 1}) .bookingBlockBtn`
);
for (let index = 0; index < accessButtons.length; index++) {
const element = accessButtons[index];
await element.click();
await wait(2000);
await matchPage.click(".bookingBackButton");
await wait(2000);
const firstPriceSpan = await matchPage.$(
".bookingResaleTogglePrice span:nth-child(2)"
);
const firstPrice = (
await firstPriceSpan?.getProperty("textContent")
)
?.toString()
.replace("JSHandle:", "")
.split(",")[0]
.replace(/\s/g, "");
const firstPriceNumber = firstPrice ? +firstPrice : undefined;
if (!firstPriceNumber) {
console.log(`Invalid price ${firstPriceNumber}`);
} else if (firstPriceNumber && !categoryMinimumPrice) {
console.log(
`First prize found for category ${category.categoryNumber} : ${firstPriceNumber}`
);
categoryMinimumPrice = firstPriceNumber;
} else if (
categoryMinimumPrice &&
firstPriceNumber &&
categoryMinimumPrice <= firstPriceNumber
) {
console.log(`The price is no lower : ${firstPriceNumber}`);
} else if (firstPriceNumber) {
console.log(`The price is lower : ${firstPriceNumber}`);
categoryMinimumPrice = firstPriceNumber;
}
await category.category.click();
await wait(1000);
}
if (categoryMinimumPrice) {
console.log(
`The minimum price for category ${category.categoryNumber} for the PSG - ${visitingTeamName} match is : ${categoryMinimumPrice}€`
);
if (!result[visitingTeamName]) result[visitingTeamName] = {};
result[visitingTeamName][category.categoryNumber] =
categoryMinimumPrice;
console.log(result);
} else
console.log(
`No prizes for category ${category.categoryNumber}`
);
}
}
console.log("Stop scrapping " + new Date());
return result;
}
try {
startScraping();
} catch (error) {
console.log(error);
}