-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (48 loc) · 1.52 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
'use strict';
var request = require('superagent');
var express = require('express');
var RSS = require('rss');
var util = require('util');
var cheerio = require('cheerio');
var md = require('marked');
var fs = require('fs');
var app = express();
var readme;
app
.get('/', function(req, res, next) {
if (readme) return res.send(readme);
fs.readFile(__dirname + '/README.md', function(err, file) {
if (err) return next(err);
readme = '<!doctype html><html><head><link rel="stylesheet" ' +
'href="//rawgit.com/sindresorhus/github-markdown-css/gh-pages/' +
'github-markdown.css"><title>PirateBay Search to RSS</title>' +
'</head><body class="markdown-body">' + md(file.toString()) +
'</body></html>';
res.send(readme);
});
})
.get('/rss', generateRSS)
.listen(process.env.PORT || 8080);
function generateRSS(req, res, next) {
var query = req.param('q');
if (!query) return res.status(400).end();
request
.get(util.format('https://thepiratebay.se/search/%s/0/7/0', query))
.end(function(err, result) {
var $, rss;
if (err) return next(err);
$ = cheerio.load(result.text);
rss = new RSS({
title: query,
link: ''
});
$('#searchResult td:nth-child(2)').each(function() {
var item = $(this);
rss.item({
title: item.find('.detName a').text(),
url: item.children('a').attr('href')
});
});
res.type('text/xml').send(rss.xml(' '));
});
}