-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
148 lines (133 loc) · 4.9 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
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 http = require('http');
const express = require('express');
const { Server } = require("socket.io");
const puppeteer = require('puppeteer');
require("dotenv").config()
const {scrapeLogic} = require("./scrape");
const { a } = require('./a');
const app = express();
const server = http.createServer(app);
app.get("/", (req, res) => {
res.send("hello server in production")
})
app.get("/scrape", (req, res) => {
scrapeLogic(res);
});
app.get("/aa", (req, res) => {
a(res)
})
const io = new Server(server ,{
cors: "*"
})
let browser;
io.on('connection', async (socket) => {
io.emit("a", "ali adib")
// if(!browser)
// browser = await puppeteer.launch({
// // args: [
// // "--disable-setuid-sandbox",
// // "--no-sandbox",
// // "--single-process",
// // "--no-zygote",
// // ],
// executablePath: "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
// headless: false
// });
if (!browser) {
browser = await puppeteer.launch({
args: [
"--disable-setuid-sandbox",
"--no-sandbox",
"--single-process",
"--no-zygote",
],
executablePath:
process.env.NODE_ENV === "production"
? process.env.PUPPETEER_EXECUTABLE_PATH
: puppeteer.executablePath(),
});
}
try {
const url = `https://football360.ir/results`;
const page = await browser.newPage();
await page.setViewport({width: 1080, height: 1024});
page.setDefaultNavigationTimeout( 90000 );
await page.goto(url);
await page.waitForSelector('.Sections_container__03lu8')
console.log('A user connected');
socket.emit("load", "load complete")
const matchList = await getMatchList(page)
socket.emit("message", matchList)
socket.on("click", async function(data, callback) {
const date = new Date();
const today = date.toISOString().split('T')[0];
try {
if (data === today) {
await page.click(`div > a[href="/results"]`);
} else {
await page.click(`a[href="/results?day=${data}"]`);
//await page.goto(`${url}?day=${data}`)
}
} catch (error) {
callback(false)
}
})
socket.on("message",async function(data, callback) {
console.log(`Received message from client: ${data}`);
try {
const matchList = await getMatchList(page)
callback(matchList)
} catch (error) {
callback(false)
}
})
socket.on("disconnect", async () => {
//await browser.close()
console.log("user disconnect")
})
} catch (error) {
socket.emit("message", false)
}
});
const getMatchList = async (page) => {
return await page.evaluate(async() => {
const matches = Array.from(document.querySelectorAll('.Sections_container__03lu8'))
return matches.map((product)=> {
const league = product.querySelector('h2').innerHTML;
const leagueImage = product.querySelectorAll('img')[0].src
const matchList = Array.from(product.querySelectorAll('li'))
.map((match) => {
const score = match.querySelector('.style_match__Fiqcg');
const homeTeam = match.querySelectorAll('.style_title__VxtR3')[0];
const awayTeam = match.querySelectorAll('.style_title__VxtR3')[1];
const teamImage = match.querySelectorAll('img');
const homeTeamImage = teamImage[0].src
const awayTeamImage = teamImage[1].src
const matchFinish = match.querySelector('.style_date__t6_B6')
const matchMinutes = match.querySelector('.style_live__7m2Y6 span')
const matchMinutesAfter90 = match.querySelector('.style_live__7m2Y6')
const matchCancel = match.querySelector('.style_canceled__RcO8s')
const matchAdjournment = match.querySelector('.style_date__t6_B6')
return {
// score: matchCancel ? matchCancel.innerHTML :
// matchAdjournment ? matchAdjournment.innerHTML :
// score.innerHTML.split(':').reverse().join(':'),
score: score.innerHTML.split(':').reverse().join(':') || false,
homeTeam: homeTeam.innerHTML,
awayTeam: awayTeam.innerHTML,
homeTeamImage,
awayTeamImage,
matchFinish: matchFinish?.innerHTML || false,
matchMinutes: matchMinutes?.innerHTML || false,
matchAdjournment: matchAdjournment?.innerHTML || false,
matchCancel: matchCancel?.innerHTML || false,
matchMinutesAfter90: matchMinutesAfter90?.innerHTML || false
}
})
return {league, leagueImage, matchList}
})
})
}
server.listen(3000, ()=> {
console.log('Server is running on http://localhost:3000');
});