-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodechef.js
88 lines (79 loc) · 3.83 KB
/
codechef.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
import Puppeteer from "puppeteer";
const scrapper = async () => {
const url = "https://www.codechef.com/contests?itm_medium=home&itm_campaign=allcontests";
const browser = await Puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
let ongoingContests = await page.evaluate(() => {
let results = []
let items = document.querySelectorAll('#present-contests-data tr')
items.forEach((item) => {
results.push({
code: item.querySelector('td:nth-child(1)').innerText,
title: item.querySelector('td:nth-child(2)').innerText,
url: "https://www.codechef.com" + item.querySelector('td:nth-child(2) a').getAttribute('href'),
startDate: item.querySelector('td:nth-child(3)').innerText.substring(0, 11),
startTime: item.querySelector('td:nth-child(3)').innerText.substring(12, 21),
duration: item.querySelector('td:nth-child(4)').innerText,
endsIn: item.querySelector('td:nth-child(5)').innerText
})
})
return results
})
let upcomingContests = await page.evaluate(() => {
let results = []
let items = document.querySelectorAll('#future-contests-data tr')
items.forEach((item) => {
results.push({
code: item.querySelector('td:nth-child(1)').innerText,
title: item.querySelector('td:nth-child(2)').innerText,
url: "https://www.codechef.com" + item.querySelector('td:nth-child(2) a').getAttribute('href'),
startDate: item.querySelector('td:nth-child(3)').innerText.substring(0, 11),
startTime: item.querySelector('td:nth-child(3)').innerText.substring(12, 21),
duration: item.querySelector('td:nth-child(4)').innerText,
startsIn: item.querySelector('td:nth-child(5)').innerText
})
})
return results
})
let pastContests = await page.evaluate(() => {
let results = []
let items = document.querySelectorAll('#past-contests-data tr')
items.forEach((item) => {
results.push({
code: item.querySelector('td:nth-child(1)').innerText,
title: item.querySelector('td:nth-child(2)').innerText,
url: "https://www.codechef.com" + item.querySelector('td:nth-child(2) a').getAttribute('href'),
startDate: item.querySelector('td:nth-child(3)').innerText.substring(0, 11),
startTime: item.querySelector('td:nth-child(3)').innerText.substring(12, 21),
duration: item.querySelector('td:nth-child(4)').innerText,
participants: item.querySelector('td:nth-child(5)').innerText
})
})
return results
})
let practiseContests = await page.evaluate(() => {
let results = []
let items = document.querySelectorAll('#practice-contests-data tr')
items.forEach((item) => {
results.push({
code: item.querySelector('td:nth-child(1)').innerText,
title: item.querySelector('td:nth-child(2)').innerText,
url: "https://www.codechef.com" + item.querySelector('td:nth-child(2) a').getAttribute('href'),
startDate: item.querySelector('td:nth-child(3)').innerText.substring(0, 11),
startTime: item.querySelector('td:nth-child(3)').innerText.substring(12, 21),
duration: item.querySelector('td:nth-child(4)').innerText,
participants: item.querySelector('td:nth-child(5)').innerText
})
})
return results
})
await browser.close();
return {
ongoingContests,
upcomingContests,
practiseContests,
pastContests
}
}
export let codechef = await scrapper();