Skip to content
Open

Uri #60

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 90 additions & 89 deletions server/api/prompt.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,97 @@
const express = require('express');
const app = express.Router();
const { isLoggedIn } = require('./middleware');
const { Op } = require('sequelize');

const { Prompt, User } = require('../db');

module.exports = app;


app.get('/', async(req, res, next) => {
try{
res.send(await Prompt.findAll());
}
catch(err){
next(err);
}
})

app.post('/', async( req, res, next) => {
try{
console.log(req.headers);
const user = await User.findBySpotifyId(req.headers.spotifyid);

const prompt = await Prompt.create({userPrompt: req.body.prompt, userId: user.id});


await prompt.askChatGPT()
res.send(prompt);
}
catch(ex){
next(ex);
}
});

app.post('/json', async (req, res, next) => {
try {
console.log(req.headers);
const user = await User.findBySpotifyId(req.headers.spotifyid);
const userPrompt = `You are an assistant that only responds in JSON.
Create a list of ${req.body.length} unique songs similar to the following
playlist: "${req.body.spotifyData}". Include "id", "title", "artist", "album"
in your response.`;

const prompt = await Prompt.create({
userPrompt: userPrompt,
userId: user.id,
});

await prompt.askChatGPT();
prompt.response = req.body.spotifyData;
await prompt.save();

res.send(prompt);
} catch (ex) {
next(ex);
}
});


app.get('/', async(req, res, next)=> {
try {
res.send(Prompt.findAllBySpotifyId(req.headers.spotifyid));
}
catch(ex){
next(ex);
}
});
// const express = require('express');
// const app = express.Router();
// const { isLoggedIn } = require('./middleware');
// const { Op } = require('sequelize');

// const { Prompt, User } = require('../db');

// module.exports = app;


// app.get('/', async(req, res, next) => {
// try{
// res.send(await Prompt.findAll());
// }
// catch(err){
// next(err);
// }
// })

// app.post('/', async( req, res, next) => {
// try{
// console.log(req.headers);
// const user = await User.findBySpotifyId(req.headers.spotifyid);

// const prompt = await Prompt.create({userPrompt: req.body.prompt, userId: user.id});


// await prompt.askChatGPT()
// res.send(prompt);
// }
// catch(ex){
// next(ex);
// }
// });

// app.post('/json', async (req, res, next) => {
// try {
// console.log(req.headers);
// const user = await User.findBySpotifyId(req.headers.spotifyid);
// const userPrompt = `You are an assistant that only responds in JSON.
// Create a list of ${req.body.length} unique songs similar to the following
// playlist: "${req.body.spotifyData}". Remember you only respond in JSON do not respond in the playlist format. Include "id", "title", "artist", "album"
// in your response. An example response is: "
// [
// {
// "id": 1,
// "title": "Hey Jude",
// "artist": "The Beatles",
// "album": "The Beatles (White Album)",
// "duration": "4:56"
// }
// ]". You must respond only in JSON.`;

// const prompt = await Prompt.create({
// userPrompt: userPrompt,
// userId: user.id,
// });

// await prompt.askChatGPT();
// prompt.response = req.body.spotifyData;
// await prompt.save();

// res.send(prompt);
// } catch (ex) {
// next(ex);
// }
// });


// app.get('/', async(req, res, next)=> {
// try {
// res.send(Prompt.findAllBySpotifyId(req.headers.spotifyid));
// }
// catch(ex){
// next(ex);
// }
// });


app.delete('/', isLoggedIn, async(req, res, next)=> {
try {
const prompt = await Prompt.findById(req.prompt.id);
await prompt.destroy();
// app.delete('/', isLoggedIn, async(req, res, next)=> {
// try {
// const prompt = await Prompt.findById(req.prompt.id);
// await prompt.destroy();

res.send(200);
}
catch(ex){
next(ex);
}
});
// res.send(200);
// }
// catch(ex){
// next(ex);
// }
// });

module.exports = app;
// module.exports = app;


/* pre 6/2 MT Updates

const express = require('express');
const app = express.Router();
Expand Down Expand Up @@ -124,20 +132,15 @@ app.post('/json', async (req, res, next) => {
try {
console.log(req.headers);
const user = await User.findBySpotifyId(req.headers.spotifyid);
const userPrompt = `You are an assistant that only responds in JSON.
Create a list of ${req.body.length} unique songs similar to the following
playlist: "${req.body.spotifyData}". Include "id", "title", "artist", "album"
in your response.`;
const userPrompt = ` You are an assistant that can only responds in JSON.
Include "id", "title", "artist", "album" in your response. Remember only respond in JSON. An example response is: "[{"id": 1,"title": "Hey Jude","artist": "The Beatles","album": "The Beatles (White Album)","duration": "4:56"}]". You must respond only in JSON. Create a list of ${req.body.length} unique songs similar to the following playlist: "${req.body.spotifyData}". `;

const prompt = await Prompt.create({
userPrompt: userPrompt,
userId: user.id,
});

await prompt.askChatGPT();

// Attach the req.body.spotifyData to the response
prompt.response = req.body.spotifyData;
await prompt.save();

res.send(prompt);
Expand Down Expand Up @@ -172,5 +175,3 @@ app.post('/json', async (req, res, next) => {

module.exports = app;


*/
11 changes: 7 additions & 4 deletions server/api/spotify.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,17 @@ export const searchFunctionality = async (searchKey) => {
try {
const response = await spotifyAxios.get('/search', {
params: {
q: `${encodeURIComponent(searchKey.title)}album:${encodeURIComponent(searchKey.album)}artist:${encodeURIComponent(searchKey.artist)}`,
q: `name:${encodeURIComponent(searchKey.title)}album:${encodeURIComponent(searchKey.album)}artist:${encodeURIComponent(searchKey.artist)}`,
type: 'track',
},
});
// https://api.spotify.com/v1/search?q=name:${encodeURIComponent(song.title)}album:${encodeURIComponent(song.album)}artist:${encodeURIComponent(song.artist)}&type=track`,
const track_uri = await response.data.tracks.items[0].uri;
track_uris.push(track_uri);
console.log('track uris check hereeeeeeeeeeeeeeeeeeeeeee',track_uris);
if (await response.data.tracks.items[0]){
return await response.data.tracks.items[0].uri
}
else{
console.log(response.data, searchKey);
}
} catch (error) {
console.error(error);
throw error;
Expand Down
Loading