-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (73 loc) · 2.68 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
const express = require('express')
const app = express()
const fetch = require('node-fetch')
const port = process.env.PORT || 3000
app.set('view engine', 'ejs')
const controller = new AbortController()
// 5 second timeout:
const timeoutId = setTimeout(() => controller.abort(), 4000)
app.listen(port, () => {
console.log(`Listening to ${port}`)
})
app.get('/', async (req, res) => {
res.sendFile(__dirname+"/Pixibrary.html")
})
app.get('/status', async (req, res) => {
return res.sendStatus(200)
})
app.get('/search', async (req, res) => {
if (!req.query.book) return res.redirect('./')
var fullUrl = 'https://' + req.get('host') ;
console.log(fullUrl)
response = await fetch(`${fullUrl}/e/?query=${req.query.book}`, )
response = await response.json()
if (response.error) return res.render('404')
console.log(response)
let bg = ['pink', 'rgb(167, 199, 231)', 'rgb(80, 200, 120)', 'lightsalmon' ]
let colors = ['pink', 'skyblue', 'lightgreen', 'lightsalmon' ]
let primary = ['red', 'blue', 'green', 'orangered']
let chosen = Math.floor(Math.random() * colors.length)
res.render('index', {uwu: colors[chosen],
prim: primary[chosen],
name: response.title,
bg: `${fullUrl}/img/?book=${req.query.book}`,
ing: response.categories,
auth: response.authors,
about: response.description,
publ: response.publisher,
public: response.publish_date,
pub: response.pageCount})
console.log(response.cover_image.replaceAll("amp;", ""))
})
app.get('/img', async (req, res) => {
var fullUrl = 'https://' + req.get('host')
let img = await fetch(`${fullUrl}/e/?query=${req.query.book}`)
img = await img.json()
img = await fetch(img.cover_image)
img = await img.buffer()
res.set('Content-Type', 'image/jpeg')
res.end(img)
})
app.get('/410244.png', async (req, res) => {
res.sendFile(__dirname+'/410244.png')
})
app.get('/book.png', async (req, res) => {
res.sendFile(__dirname+"/closed-book-microsoft.png")
})
app.get('/e', async (req, res) => {
console.log('Hi :)')
let content = await fetch(`https://www.googleapis.com/books/v1/volumes?q=${req.query.query}`)
content = await content.json()
if (content.totalItems === 0) return res.json({error: true})
console.log(content)
res.json({
title: content.items[0].volumeInfo.title,
authors: content.items[0].volumeInfo.authors,
publisher: content.items[0].volumeInfo.publisher,
publish_date: content.items[0].volumeInfo.publishedDate,
description: content.items[0].volumeInfo.description,
pageCount: content.items[0].volumeInfo.pageCount,
categories: content.items[0].volumeInfo.categories,
cover_image: content.items[0].volumeInfo.imageLinks.thumbnail
})
})