-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (54 loc) · 1.68 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
import express from 'express'
import bodyParser from 'body-parser'
import { scrape } from './torrent.js'
const app = express()
const PORT = 3020
app.use(bodyParser.json())
app.use(express.static('.')) // serve index.html and other static assets
app.post('/scrape', async (req, res) => {
// Expecting { magnet: 'magnet:?...' }
try {
const { magnet } = req.body
if (!magnet || !magnet.startsWith('magnet:?')) {
return res.status(400).json({ error: 'Invalid magnet link.' })
}
// Remove 'magnet:?' prefix and parse parameters
const params = new URLSearchParams(magnet.slice(8))
const xt = params.get('xt')
if (!xt || !xt.includes('btih:')) {
return res.status(400).json({ error: 'Missing infohash (xt parameter).' })
}
const infohash = xt.split('btih:')[1].toUpperCase()
if (infohash.length !== 40) {
return res
.status(400)
.json({ error: 'Infohash must be 40 hexadecimal characters.' })
}
// Get all trackers and filter for UDP trackers
const trackers = params
.getAll('tr')
.filter((tracker) => tracker.toLowerCase().startsWith('udp://'))
if (trackers.length === 0) {
return res
.status(400)
.json({ error: 'No UDP trackers found in magnet link.' })
}
// For each tracker, call scrape() concurrently
const results = await Promise.all(
trackers.map(async (tracker) => {
try {
const data = await scrape(tracker, [infohash])
return { tracker, data }
} catch (error) {
return { tracker, error: error.message }
}
})
)
res.json({ infohash, results })
} catch (error) {
res.status(500).json({ error: error.message })
}
})
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`)
})