File size: 3,661 Bytes
7385ed3
 
b932588
 
7385ed3
 
 
b932588
 
1e5ca80
26bacb1
b932588
0edb330
6474027
 
b932588
 
26bacb1
 
b932588
 
 
 
 
 
 
 
1e5ca80
b932588
 
 
 
 
 
 
7763a3b
b932588
 
 
 
 
7763a3b
b932588
 
7763a3b
b932588
7763a3b
 
1e5ca80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b932588
 
 
 
 
 
7385ed3
b932588
 
 
 
 
7763a3b
b932588
7763a3b
b932588
 
 
7385ed3
b932588
 
 
7385ed3
b932588
 
7385ed3
b932588
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
const express = require('express');
const axios = require('axios');
const { parseStudentData } = require('./contentModel.js');

const app = express();
const PORT = 7860;

// Middleware
app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
app.use((req, res, next) => {
  res.header({
    "Access-Control-Allow-Origin": "106.77.189.65",
    "Access-Control-Allow-Methods": "GET, OPTIONS",
    "Access-Control-Allow-Headers": "Content-Type",
  });
  next();
});

// API Endpoint
app.get('/fetch-regno-:regno', async (req, res) => {
  try {
    const { regno } = req.params;
    
    // Validate registration number
    if (!/^\d{11}$/.test(regno)) {
      return res.status(400).json({
        error: "Invalid registration number format",
        example: "22104134026"
      });
    }

    // Fetch data with retries
    const html = await fetchWithRetries(regno);
    if (!html) return res.status(404).json({ error: "Result not found" });

    // Parse and return
    const result = parseStudentData(html, regno);
    res.json(result);

  } catch (error) {
    res.status(500).json({
      error: "Server error",
      details: error.message
    });
  }
});

// Search Endpoint (Web Interface)
app.get('/search', async (req, res) => {
  try {
    const regno = req.query.regno;
    if (!regno) return res.redirect('/');

    const apiResponse = await axios.get(`http://localhost:${PORT}/fetch-regno-${regno}`);
    const result = apiResponse.data;

    res.send(`
      <!DOCTYPE html>
      <html>
      <head>
        <title>Result for ${regno}</title>
        <link rel="stylesheet" href="/style.css">
      </head>
      <body>
        <div class="container">
          <h1>Student Result</h1>
          <div class="result-box">
            <p><strong>Name:</strong> ${result.student_name}</p>
            <p><strong>Registration No:</strong> ${regno}</p>
            <p><strong>SGPA:</strong> ${result.sgpa}</p>
            <p><strong>College:</strong> ${result.college_name}</p>
            <p><strong>Course:</strong> ${result.course_name}</p>
          </div>
          <a href="/" class="button">Check Another</a>
        </div>
      </body>
      </html>
    `);

  } catch (error) {
    res.status(error.response?.status || 500).send(`
      <!DOCTYPE html>
      <html>
      <head><title>Error</title><link rel="stylesheet" href="/style.css"></head>
      <body>
        <div class="container">
          <h1>Error ${error.response?.status || ''}</h1>
          <div class="error-box">
            <p>${error.response?.data?.error || 'Something went wrong'}</p>
            <a href="/" class="button">Try Again</a>
          </div>
        </div>
      </body>
      </html>
    `);
  }
});

// Helper function with retries
async function fetchWithRetries(regno) {
  const url = `https://results.beup.ac.in/ResultsBTech1stSem2023_B2023Pub.aspx?Sem=I&RegNo=${regno}`;
  let retries = 3;
  let delay = 1000;

  for (let attempt = 0; attempt < retries; attempt++) {
    try {
      const response = await axios.get(url, {
        timeout: 10000,
        headers: {
          'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
          'Referer': 'https://results.beup.ac.in/'
        }
      });

      if (response.data.includes("No Record Found !!!")) return null;
      return response.data;
      
    } catch (error) {
      if (attempt === retries - 1) throw error;
      await new Promise(resolve => setTimeout(resolve, delay));
      delay *= 2;
    }
  }
}

app.listen(PORT, () => console.log(`Server running on port ${PORT}`));