Reaperxxxx commited on
Commit
ed00dcb
·
verified ·
1 Parent(s): f564b1b

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +12 -24
server.js CHANGED
@@ -1,14 +1,12 @@
1
  const express = require("express");
 
2
  const { chromium } = require("playwright-core");
3
  const cheerio = require("cheerio");
4
- const prettify = require("express-prettify");
5
- const prettyjson = require("prettyjson");
6
 
7
  const app = express();
8
  const PORT = 7860;
9
 
10
- // Use prettify middleware for JSON responses
11
- app.use(prettify({ query: "pretty" }));
12
 
13
  async function scrapeMatches() {
14
  const browser = await chromium.launch({
@@ -21,15 +19,15 @@ async function scrapeMatches() {
21
 
22
  // Set headers to bypass bot detection
23
  await page.setExtraHTTPHeaders({
24
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
25
- "Accept-Language": "en-US,en;q=0.9",
26
- "Referer": "https://www.google.com/"
27
  });
28
 
29
  // Block unnecessary requests (images, CSS, etc.)
30
- await page.route("**/*", (route) => {
31
- return ["image", "stylesheet", "font", "media"].includes(route.request().resourceType())
32
- ? route.abort()
33
  : route.continue();
34
  });
35
 
@@ -48,9 +46,7 @@ async function scrapeMatches() {
48
 
49
  for (let link of leagueLinks) {
50
  await page.goto(link, { waitUntil: "domcontentloaded", timeout: 60000 });
51
-
52
- // Use evaluate to get full HTML for parsing
53
- const html = await page.evaluate(() => document.documentElement.outerHTML);
54
  const $ = cheerio.load(html);
55
 
56
  $(".wttr").each((_, element) => {
@@ -80,21 +76,13 @@ async function scrapeMatches() {
80
  app.get("/draw", async (req, res) => {
81
  try {
82
  const matches = await scrapeMatches();
83
-
84
- // Beautify response (Console Output)
85
- console.log("\nFetched Matches:");
86
- console.log(prettyjson.render(matches));
87
-
88
- res.json({
89
- success: true,
90
- data: matches
91
- });
92
  } catch (error) {
93
- console.error("Error fetching matches:", error);
94
  res.status(500).json({ success: false, message: "Failed to fetch data" });
95
  }
96
  });
97
 
98
  app.listen(PORT, () => {
99
- console.log(`\nServer running at http://localhost:${PORT}/draw\n`);
100
  });
 
1
  const express = require("express");
2
+ const pretty = require("express-pretty"); // Import express-pretty
3
  const { chromium } = require("playwright-core");
4
  const cheerio = require("cheerio");
 
 
5
 
6
  const app = express();
7
  const PORT = 7860;
8
 
9
+ app.use(pretty()); // Enable express-pretty
 
10
 
11
  async function scrapeMatches() {
12
  const browser = await chromium.launch({
 
19
 
20
  // Set headers to bypass bot detection
21
  await page.setExtraHTTPHeaders({
22
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
23
+ 'Accept-Language': 'en-US,en;q=0.9',
24
+ 'Referer': 'https://www.google.com/'
25
  });
26
 
27
  // Block unnecessary requests (images, CSS, etc.)
28
+ await page.route('**/*', (route) => {
29
+ return ['image', 'stylesheet', 'font', 'media'].includes(route.request().resourceType())
30
+ ? route.abort()
31
  : route.continue();
32
  });
33
 
 
46
 
47
  for (let link of leagueLinks) {
48
  await page.goto(link, { waitUntil: "domcontentloaded", timeout: 60000 });
49
+ const html = await page.content();
 
 
50
  const $ = cheerio.load(html);
51
 
52
  $(".wttr").each((_, element) => {
 
76
  app.get("/draw", async (req, res) => {
77
  try {
78
  const matches = await scrapeMatches();
79
+ res.json({ success: true, data: matches });
 
 
 
 
 
 
 
 
80
  } catch (error) {
81
+ console.error("Error:", error);
82
  res.status(500).json({ success: false, message: "Failed to fetch data" });
83
  }
84
  });
85
 
86
  app.listen(PORT, () => {
87
+ console.log(`Server running at http://localhost:${PORT}/draw`);
88
  });