repl.it
@jajoosam/

books-api-1

Express

No description

fork
loading
Files
  • index.js
  • package-lock.json
  • package.json
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
const express = require('express');
const axios = require('axios')
var pretty = require('express-prettify');
 

const app = express();
app.use(express.static('public'));
app.use(pretty({ query: 'pretty' }));


function parse(libgen, info, res){
  //console.log(links)
  var final = {
    mobi: null,
    epub: null,
    pdf: null,
    info: null
  };

  for(var i = 0; i<libgen.length; i++){
    if(libgen[i].extension=='mobi' && libgen[i].language.trim()=='English' && !final.mobi){
      final.mobi = "https://d-m.llegecoip7.com/get_item/" + libgen[i].id;
    }
    if(libgen[i].extension=='epub' && libgen[i].language.trim()=='English' && !final.mobi){
      final.epub = "https://d-m.llegecoip7.com/get_item/" + libgen[i].id;
    }
    if(libgen[i].extension=='pdf' && libgen[i].language.trim()=='English' && !final.mobi){
      final.pdf = "https://d-m.llegecoip7.com/get_item/" + libgen[i].id;
    }
  }
  
  info = info[0]

  final.info = {
    title: info.volumeInfo.title,
    authors: info.volumeInfo.authors,
    description: info.volumeInfo.description,
    isbn: info.volumeInfo.industryIdentifiers["ISBN_13"],
    pages: info.volumeInfo.pageCount,
    image: info.volumeInfo.imageLinks.thumbnail,
    categories: info.volumeInfo.categories
  }

  res.json(final)

}

app.get('/api', (req, res) => {
  var libgen = []
  var gbooks;
  var index = 0;
  var name = req.query.book;


  axios.get(`https://www.googleapis.com/books/v1/volumes?q=name:${name}`)
    .then(function (response) {
      gbooks = response.data.items;
      console.log((response.data.items[0].volumeInfo.title + " " + response.data.items[0].volumeInfo.authors[0]).trim().replace(/ *\([^)]*\) */g, "").split(" ").join("+"))
      axios.get(`https://books.libgen.me/search?search=${(response.data.items[0].volumeInfo.title + " " + response.data.items[0].volumeInfo.authors[0]).trim().replace(/ *\([^)]*\) */g, "").split(" ").join("+")}&page=1&sort=0&perPage=3&year[]=0&collection=all`)
        .then(function (response) {
            console.log(response.fata)
            parse(response.data.books, gbooks, res)   
        })
        .catch(function (error) {
          console.error(error);
        });
    })
    .catch(function (error) {
      console.error(error);
    });


});

app.get('/', (req, res) => {
	res.send('Hello Express app');
});

app.listen(3000, () => console.log('server started'));