-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
114 lines (101 loc) · 3.31 KB
/
app.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
// modules
const express = require('express');
const bodyParser = require('body-parser');
const http = require("https");
const _ = require('lodash');
let app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(__dirname + '/public'));
app.get('/',(req,res) =>{
res.redirect('/homepage')
})
// rendering homepage
app.get('/homepage', (req, res) => {
res.render('homepage')
})
app.post('/homepage',(req,res) => {
let url = req.url
console.log(url)
if(url == 'music')
res.rendirect('/music')
if(url == 'timer')
res.redirect('/timer')
if(url == 'doubts')
res.redirect('/doubts')
})
app.get('/music',(req,res) => {
let data = [];
res.render('music',{tableHeading: "No song playing",data : data})
})
app.get('/timer',(req,res) => {
res.render('timer')
})
app.post('/music', (request, response) => {
console.log(request.body)
if (request.body.search != '') {
let song = request.body.search
const http = require("https");
const options = {
"method": "GET",
"hostname": "spotify-scraper.p.rapidapi.com",
"port": null,
"path": encodeURI("/v1/search?term=" + song),
"headers": {
"X-RapidAPI-Key": "c6508df8d2msh28c51d25a65f866p1c9b1ajsn7ff5b8b72944",
"X-RapidAPI-Host": "spotify-scraper.p.rapidapi.com",
"useQueryString": true
}
};
const req = http.request(options, function (res) {
const chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
const body = Buffer.concat(chunks);
let data = JSON.parse(body)
console.log(data.albums.items)
tableHeading = ""
response.render('music', {tableHeading: tableHeading,data: data.albums.items})
});
});
req.end();
}
})
app.get('/doubts', (req, res) => {
res.render('doubts',{answer:""})
})
app.post('/doubts', (request, response) => {
const options = {
"method": "POST",
"hostname": "openai80.p.rapidapi.com",
"port": null,
"path": "/chat/completions",
"headers": {
"content-type": "application/json",
"X-RapidAPI-Key": "c6508df8d2msh28c51d25a65f866p1c9b1ajsn7ff5b8b72944",
"X-RapidAPI-Host": "openai80.p.rapidapi.com",
"useQueryString": true
}
};
const req = http.request(options, function (res) {
const chunks = [];
res.on("data", function (chunk) {
chunks.push(chunk);
});
res.on("end", function () {
const body = Buffer.concat(chunks);
console.log(JSON.parse(body).choices);
// let data = JSON.parse(body)
response.render('doubts', { answer: JSON.parse(body).choices[0].message.content})
});
});
let con = (request.body.chat).toString()
req.write(JSON.stringify({ model: 'gpt-3.5-turbo', messages: [{ role: 'user', content: con }] }));
req.end();
})
app.get('/subscription',(req,res) =>{
res.render('subscription')
})
app.listen(3000, () => { console.log("Listening at port 30000") })