forked from tdjsnelling/sqtracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrss.js
More file actions
90 lines (80 loc) · 2.61 KB
/
Copy pathrss.js
File metadata and controls
90 lines (80 loc) · 2.61 KB
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
import bcrypt from "bcrypt";
import User from "../schema/user";
import Torrent from "../schema/torrent";
import { embellishTorrentsWithTrackerScrape } from "./torrent";
// prettier-ignore
const getTorrentXml = (torrent, userId) => {
const announceUrl = `${process.env.SQ_BASE_URL}/sq/${userId}/announce`
return `<item>
<title>${torrent.name}</title>
<description>${torrent.description}</description>
<guid>${torrent.infoHash}</guid>
<enclosure url="${process.env.SQ_API_URL}/torrent/download/${torrent.infoHash}/${userId}" type="application/x-bittorrent" />
<torrent>
<filename>${torrent.name}</filename>
<contentlength>${torrent.size}</contentlength>
<magneturi>magnet:?xt=urn:btih:${torrent.infoHash}&dn=${encodeURIComponent(torrent.name)}&tr=${encodeURIComponent(announceUrl)}</magneturi>
<trackers>
<group order="ordered">
<tracker seeds="${torrent.seeders}" peers="${torrent.seeders + torrent.leechers}">
${announceUrl}
</tracker>
</group>
</trackers>
</torrent>
</item>`
}
export const rssFeed = (tracker) => async (req, res, next) => {
const { username, password } = req.cookies;
const { query } = req.query;
try {
const user = await User.findOne({ username });
if (!user) {
res.status(401).send("Incorrect login details");
return;
}
const matches = await bcrypt.compare(password, user.password);
if (!matches) {
res.status(401).send("Incorrect login details");
return;
}
let torrents;
if (query) {
torrents = await Torrent.find(
{
$or: [
{ name: { $regex: decodeURIComponent(query), $options: "i" } },
{
description: { $regex: decodeURIComponent(query), $options: "i" },
},
],
},
null,
{ sort: { created: -1 }, limit: 100 }
).lean();
} else {
torrents = await Torrent.find({}, null, {
sort: { created: -1 },
limit: 100,
}).lean();
}
const torrentsWithScrape = await embellishTorrentsWithTrackerScrape(
tracker,
torrents
);
const torrentsXml = torrentsWithScrape
.map((t) => getTorrentXml(t, user.uid))
.join("\n");
res.setHeader("Content-Type", "text/xml");
res.status(200).send(`<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>${process.env.SQ_SITE_NAME}: ${query ? "results" : "latest"}</title>
<link>${process.env.SQ_BASE_URL}</link>
${torrentsXml}
</channel>
</rss>`);
} catch (e) {
next(e);
}
};