-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
374 lines (316 loc) · 10.8 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
const express = require('express');
const fetch = require('node-fetch');
const cors = require('cors');
const axios = require('axios');
// Import your scraper function
const { pirateBay } = require('./scraper/pirateBay');
const { torrent1337x } = require('./scraper/1337x');
const { nyaaSI } = require('./scraper/nyaaSI');
const { yts } = require('./scraper/yts');
const peakpx = require('./scraper/peakpx');
const scrapePixiv = require('./scraper/pixiv');
const getRingtones = require('./scraper/ringtone');
const getGifs = require('./scraper/giphy');
const getStations = require('./scraper/radio');
const app = express();
const port = 3000;
// Enable All CORS Requests
app.use(cors());
app.get('/', (req, res) => {
res.send(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>The News Api's</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="https://i.imgur.com/38RT99Z.jpg" rel="icon" />
<style>
@import url('https://fonts.googleapis.com/css2?family=Mukta&display=swap');
body {
font-family: 'Mukta', sans-serif;
padding: 2rem;
text-align: center;
background-color: #111;
color: #fff;
}
/* Dark mode styles */
main {
background-color: #222;
padding: 2rem;
border-radius: 8px;
}
h1, h2, h3, h6 {
word-wrap: break-word;
color: #fff;
}
p {
color: #ccc;
}
a {
color: #999;
}
a:hover {
color: #fff;
}
/* Separator */
.separator {
margin: 2rem 0;
height: 1px;
background-color: #666;
}
/* Allow text selection */
body {
-webkit-user-select: text;
-moz-user-select: text;
-ms-user-select: text;
user-select: text;
}
</style>
</head>
<body>
<main>
<h1>Welcome</h1>
<p>Your one-stop solution to access the latest news, search for torrents, get insights from Genius, fetch data from Newscatcher, and more.</p>
<h3>Base URL</h3>
<p>https://news-api-six-navy.vercel.app</p>
<p>https://news-api-czsp.onrender.com</p>
<div class="separator"></div>
<h6>API Endpoints</h6>
<h2>News Category</h2>
<h3>GET /api/news/ann</h3>
<p>Fetches news from AnimeNewsNetwork.</p>
<h3>GET /api/news/inshorts</h3>
<p>Fetches news from Inshorts. Use ?query= to search for news.</p>
<h3>GET /api/news/us-tech</h3>
<p>Fetches top headlines in the technology category from the US.</p>
<h3>GET /api/news/in-tech</h3>
<p>Fetches top headlines in the technology category from India.</p>
<div class="separator"></div>
<h2>Torrents Category</h2>
<h3>GET /api/torrent/piratebay/:query/:page?</h3>
<p>Fetches torrents data from pirateBay. Replace :query with your search query. :page is optional and defaults to 1.</p>
<h3>GET /api/torrent/1337x/:query/:page?</h3>
<p>Fetches torrents data from 1337x. Replace :query with your search query. :page is optional and defaults to 1.</p>
<h3>GET /api/torrent/nyaasi/:query/:page?</h3>
<p>Fetches torrents data from Nyaa.si. Replace :query with your search query. :page is optional and defaults to 1.</p>
<h3>GET /api/torrent/yts/:query/:page?</h3>
<p>Fetches torrents data from YTS. Replace :query with your search query. :page is optional and defaults to 1.</p>
<div class="separator"></div>
<h2>Additional APIs Category</h2>
<h3>GET /api/peakpx/:query/:page?</h3>
<p>Fetches img from peakpx. :page is optional and defaults to 1.</p>
<h3>GET /api/slok/:ch?/:sl?</h3>
<p>Fetches slok from Gita. :ch & :sl is optional and defaults to 1.</p>
<h3>GET /api/genius/:query</h3>
<p>Fetches lyrics from Genius.</p>
<h3>GET /api/ringtone/:query</h3>
<p>Fetches ringtones from MusikRingtone.</p>
<h3>GET /api/memes</h3>
<p>Fetches memes from API.</p>
<h3>GET /api/pixiv/:query/:page?</h3>
<p>Fetches image from PIXIV.</p>
<h3>GET /api/person/:num?</h3>
<p>Fetches random details from API.</p>
<h3>GET /api/giphy/:query/:page?</h3>
<p>Fetches gifs from Giphy.</p>
<h3>GET /api/radio/:page?</h3>
<p>Fetches radio from API.</p>
<h3>GET /api/ifsc/:ifsc</h3>
<p>Fetches bank details from IFSC CODE.</p>
<h3>GET /api/jokes/:query</h3>
<p>Fetches jokes from Chucknorris.</p>
</main>
</body>
</html>
`);
});
app.get('/api/torrent/piratebay/:query/:page?', createScrapeRoute(pirateBay));
app.get('/api/torrent/1337x/:query/:page?', createScrapeRoute(torrent1337x));
app.get('/api/torrent/nyaasi/:query/:page?', createScrapeRoute(nyaaSI));
app.get('/api/torrent/yts/:query/:page?', createScrapeRoute(yts));
// Generic function to handle scraping requests
function createScrapeRoute(scraperFunction) {
return async (req, res) => {
const { query, page = 1 } = req.params;
try {
const data = await scraperFunction(query, page);
res.json(data);
} catch (err) {
console.error(err);
res.status(500).json({ message: 'An error occurred while scraping torrents data.' });
}
};
}
app.get('/api/radio/:page?', async (req, res) => {
const page = req.params.page || 1;
try {
const stations = await getStations(page);
let data = JSON.stringify(stations, null, 2);
res.setHeader('Content-Type', 'application/json');
res.send(data);
} catch (error) {
console.error('Error:', error);
res.status(500).send('Server error');
}
});
app.get('/api/giphy/:query/:page?', async (req, res) => {
const { query, page } = req.params;
try {
const gifs = await getGifs(query, page);
let data = JSON.stringify(gifs, null, 2);
res.setHeader('Content-Type', 'application/json');
res.send(data);
} catch (error) {
res.status(500).json({ error: error.toString() });
}
});
app.get('/api/ringtone/:query', async (req, res) => {
const { query } = req.params;
try {
const ringtones = await getRingtones(query);
let data = JSON.stringify(ringtones, null, 2);
res.setHeader('Content-Type', 'application/json');
res.send(data);
} catch (err) {
console.error(err);
res.status(500).json({ message: "Internal server error" });
}
});
app.get('/api/person/:num?', async (req, res) => {
try {
const num = req.params.num || 1;
const url = `https://peoplegeneratorapi.live/api/person/${num}`;
const response = await axios.get(url);
const prettyJson = JSON.stringify(response.data, null, 2); // This will format the JSON with 2 spaces of indentation
res.setHeader('Content-Type', 'application/json');
res.send(prettyJson);
} catch (error) {
console.error('Error:', error);
res.status(500).send('Server error');
}
});
app.get('/api/memes', async (req, res) => {
try {
const response = await axios.get('https://api.imgflip.com/get_memes');
let data = response.data;
data = JSON.stringify(data, null, 2); // Prettify JSON
data = data.replace(/\\\//g, '/'); // Replace \/ with /
res.setHeader('Content-Type', 'application/json');
res.send(data);
} catch (error) {
console.error('Error:', error);
res.status(500).send('Server error');
}
});
app.get('/api/peakpx/:query/:page?', async (req, res) => {
const { query, page = 1 } = req.params;
peakpx.search(query, page)
.then(images => {
res.json(images);
})
.catch(error => {
console.error('Error:', error);
res.status(500).json({ message: 'An error occurred while fetching peakpx images.' });
});
});
app.get('/api/pixiv/:query/:page?', async (req, res) => {
const { query, page } = req.params;
try {
const artworks = await scrapePixiv(query, page);
let data = JSON.stringify(artworks, null, 2);
res.setHeader('Content-Type', 'application/json');
res.send(data);
} catch (error) {
console.error(error);
res.status(500).json({ error: 'An error occurred while fetching Pixiv artworks.' });
}
});
app.get('/api/slok/:ch?/:sl?', async (req, res) => {
const chapter = req.params.ch || '1';
const sloka = req.params.sl || '1';
try {
const response = await axios.get(`https://bhagavadgitaapi.in/slok/${chapter}/${sloka}`);
const prettyJson = JSON.stringify(response.data, null, 2); // This will format the JSON with 2 spaces of indentation
res.setHeader('Content-Type', 'application/json');
res.send(prettyJson);
} catch (error) {
console.error('Error:', error);
res.status(500).send('Server error');
}
});
app.get('/api/jokes/:query', async (req, res) => {
try {
const response = await axios.get(`https://api.chucknorris.io/jokes/search?query=${req.params.query}`);
const prettyJson = JSON.stringify(response.data, null, 2); // This will format the JSON with 2 spaces of indentation
res.setHeader('Content-Type', 'application/json');
res.send(prettyJson);
} catch (error) {
console.error('Error:', error);
res.status(500).send('Server error');
}
});
app.get('/api/ifsc/:ifsc', async (req, res) => {
try {
const ifsc = req.params.ifsc;
const url = `https://bank-apis.justinclicks.com/API/V1/IFSC/${ifsc}`;
const response = await axios.get(url);
const prettyJson = JSON.stringify(response.data, null, 2); // This will format the JSON with 2 spaces of indentation
res.setHeader('Content-Type', 'application/json');
res.send(prettyJson);
} catch (error) {
console.error('Error:', error);
res.status(500).send('Server error');
}
});
app.get('/api/genius/:query', async (req, res) => {
const { query } = req.params;
try {
const response = await fetch(`https://genius.com/api/search/multi?per_page=1&q=${query}`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
res.json(data);
} catch (err) {
console.error(err);
res.status(500).json({ message: 'An error occurred while fetching data from Genius.' });
}
});
const fetchNews = (category, country, res) => {
fetch(`https://saurav.tech/NewsAPI/top-headlines/category/${category}/${country}.json`)
.then(response => response.json())
.then(data => res.json(data))
.catch(err => res.status(500).json({ error: err.toString() }));
};
app.get('/api/news/us-tech', (req, res) => {
fetchNews('technology', 'us', res);
});
app.get('/api/news/in-tech', (req, res) => {
fetchNews('technology', 'in', res);
});
app.get('/api/news/:source', async (req, res) => {
const { source } = req.params;
const { query } = req.query;
const sourceToUrlMap = {
ann: 'https://api.fl-anime.com/news/ann/recent-feeds',
inshorts: query
? `https://inshorts.vercel.app/news/search?query=${query}&offset=0&limit=10`
: 'https://inshorts.vercel.app/news/all?offset=0&limit=10'
};
if (!sourceToUrlMap.hasOwnProperty(source)) {
return res.status(400).send('Invalid source');
}
try {
const response = await fetch(sourceToUrlMap[source]);
const data = await response.json();
res.json(data);
} catch (error) {
console.error('Error:', error);
res.status(500).send('Server error');
}
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}/`);
});
module.exports = app;