-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
119 lines (98 loc) · 3.35 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
const express = require("express");
const axios = require("axios");
const session = require("express-session");
const cors = require("cors");
const { getBoards, getPins } = require("./pinterest_queries");
const getLabels = require("./clarifai-api");
const getAllSongs = require("./spotify-queries");
const app = express();
const port = process.env.PORT || 3000;
app.use(
cors({
origin: [`https://songboard-front-end.vercel.app`],
credentials: true,
})
);
require("dotenv").config();
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
proxy: true,
cookie: { secure: true, sameSite: "None", maxAge: 1 * 60 * 60 * 1000 }, // Use 'true' if using HTTPS
})
);
app.use(express.json()); // Use express.json() to parse JSON bodies
//PINTEREST PORTION
app.get("/auth/pinterest/callback", async (req, res) => {
const authorizationCode = req.query.code;
const clientId = process.env.PINTEREST_CLIENT_ID;
const clientSecret = process.env.PINTEREST_CLIENT_SECRET;
const redirectUri =
"https://songboard-back-end.vercel.app/auth/pinterest/callback"; // This should match the one used in the frontend
const authString = `${clientId}:${clientSecret}`;
const authHeader = "Basic " + Buffer.from(authString).toString("base64");
// Prepare the request body
const data = new URLSearchParams({
grant_type: "authorization_code",
code: authorizationCode,
redirect_uri: redirectUri,
});
try {
const response = await axios.post(
"https://api.pinterest.com/v5/oauth/token",
data,
{
headers: {
Authorization: authHeader,
"Content-Type": "application/x-www-form-urlencoded",
},
}
);
req.session.accessToken = response.data.access_token; // Store the access token in the session
req.session.save(); // Save the session
// console.log(response.data);
res.redirect("https://songboard-front-end.vercel.app/pinterest-select"); //adjust redirect url when deploying
} catch (error) {
res.status(500).send("Error during Pinterest OAuth");
}
});
app.get("/pinterest/queries/boards", async (req, res) => {
const accessToken = req.session.accessToken; // Retrieve the access token from the session
if (!accessToken) {
return res.status(401).send("Access token not found");
}
try {
const all_boards = await getBoards(accessToken);
res.status(200).send(all_boards);
} catch (error) {
res.status(404).send("Error in getting boards");
}
});
app.get("/pinterest/queries/pins", async (req, res) => {
const accessToken = req.session.accessToken; // Retrieve the access token from the session
const board_id = req.query.board_id;
if (!accessToken) {
return res.status(401).send("Access token not found");
}
try {
const pins = await getPins(accessToken, board_id);
res.status(200).send(pins);
} catch (error) {
res.status(404).send("Error in getting pins");
}
});
// REST OF LOGIC
app.post("/analyze-image", async (req, res) => {
const { imageList } = req.body;
// get labels from google vision
const all_labels = await getLabels(imageList);
// return all songs
const final_result = await getAllSongs(all_labels);
res.status(200).send(final_result);
// console.log(`Final list: ${final_result}`);
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});