Advertisement
Guest User

node.js

a guest
Nov 20th, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fetch = require("node-fetch");
  2. const express = require('express')
  3. const app = express()
  4.  
  5. String.prototype.replaceAll = function (search, replacement) {
  6.     return this.replace(new RegExp(search, 'g'), replacement);
  7. };
  8.  
  9. const parseFile = url =>
  10.     url.split('=')[1].split('&')[0].replaceAll('%2F', '/').replaceAll('%3A', ':');
  11.  
  12. const url = x => `https://gs.3g.cn/D/${x.toString(16)}/w`;
  13.  
  14. const format = (url, url2) => {
  15.     const style = `style="height:400px;"`;
  16.     if (url.endsWith('.zip')) {
  17.         return `<iframe src='${url2}' ${style}></iframe>`;
  18.     }
  19.     const format = url.endsWith('.mp3') ? 'audio' : (url.endsWith('.mp4') ? 'video' : 'img');
  20.     return `<${format} src='${url}' ${style}></${format}>`;
  21. }
  22.  
  23. app.get('/:start/:end', async (req, res) => {
  24.     const start = parseInt(req.params.start),
  25.         end = parseInt(req.params.end);
  26.     console.log(start, end);
  27.  
  28.     const urls = Array(end).fill(start).map((i, j) => i + j).map(x => url(x));
  29.  
  30.     const results = await Promise.all(urls.map(async u =>
  31.         fetch(u)
  32.             .then(r => {
  33.                 return [parseFile(r.url), u];
  34.             })
  35.             .catch(() => console.log('error', u))
  36.     ));
  37.     // console.log(results);
  38.  
  39.     res.send(results.map(r => `<a href='${r[1]}' >${format(r[0], r[1])}</a>`).join('\n'));
  40. })
  41.  
  42. app.listen(8000, () => console.log(`http://localhost:8000/14000000/10`));
  43.  
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement