Skip to content

Commit

Permalink
Merge pull request #15 from cis3296s24/testingFileWrite
Browse files Browse the repository at this point in the history
added database with user id, display name, session id
  • Loading branch information
tun82434 authored Apr 5, 2024
2 parents bbdea28 + e945801 commit a301ae0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
9 changes: 9 additions & 0 deletions dfj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
addingSessionBranchAri
addingSessionButton
ari-proof-of-concept
findingItems
main
session_file
session_redirect
session_redirect_ari
* testingFileWrite
53 changes: 42 additions & 11 deletions receiptifyv1/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const os = require('os');


/**
* This is an example of a basic node.js script that performs
* the Authorization Code oAuth2 flow to authenticate against
Expand All @@ -12,6 +11,7 @@ const os = require('os');
const express = require('express'); // Express web server framework
const request = require('request');

const axios = require('axios');
// const axios = require("axios"); // "Request" library
// const bodyParser = require("body-parser");
// const cors = require("cors");
Expand Down Expand Up @@ -46,10 +46,15 @@ for (const name of Object.keys(networkInterfaces)) {
}
}

// // Open file for writing (creates a new file if it doesn't exist)
// fs.writeFile('filename.txt', 'New content to be written', (err) => {
// if (err) throw err;
// console.log('File has been written!');
// });

/**
* redirect_uri must equal what is in the developer dashboard. If we move to server and have a static IP, then we can change to a set IP address.
*/

var redirect_uri = process.env.redirect_uri || `http://${serverIP}:3000/callback`;
/**
* Generates a random string containing numbers and letters
Expand Down Expand Up @@ -113,6 +118,7 @@ app.get('/login', function (req, res) {
state: state,
})
);

});


Expand Down Expand Up @@ -156,7 +162,6 @@ app.get('/submit', function (req, res){
state: state,
})
);
});


/*app.get('/applemusic', function (req, res) {
Expand Down Expand Up @@ -196,15 +201,24 @@ app.get('/submit', function (req, res){
res.sendFile(__dirname + '/public/lastfm.html');
});*/

async function fetchProfile(token) {
const result = await fetch("https://api.spotify.com/v1/me", {
method: "GET", headers: { Authorization: `Bearer ${token}` }
});

return await result.json();
}

app.get('/callback', function (req, res) {
// your application requests refresh and access tokens
// after checking the state parameter
sessionID = generateSessionID();
sessionIDString = 'sessionID'
if (req.cookies[sessionIDString] != null){
sessionID = req.cookies[sessionIDString];
}
console.log(`/callback sessionID: ` + sessionID);
sessionID = generateSessionID();
sessionIDString = 'sessionID'
if (req.cookies[sessionIDString] != null){
sessionID = req.cookies[sessionIDString];
}
console.log(`/callback sessionID: ` + sessionID);

var code = req.query.code || null;
var state = req.query.state || null;
var storedState = req.cookies ? req.cookies[stateKey] : null;
Expand Down Expand Up @@ -235,12 +249,11 @@ console.log(`/callback sessionID: ` + sessionID);
json: true,
};

request.post(authOptions, function (error, response, body) {
request.post(authOptions, async function (error, response, body) {
if (!error && response.statusCode === 200) {
access_token = body.access_token;
var access_token = body.access_token,
refresh_token = body.refresh_token;

res.redirect(
'/#' +
querystring.stringify({
Expand All @@ -250,6 +263,19 @@ console.log(`/callback sessionID: ` + sessionID);
sessionID: sessionID // add the session ID
})
);

const profile = await fetchProfile(access_token);
console.log(profile.display_name);

// Writing to user data file
fs.appendFile('users.csv', ('\n'+ profile.display_name + ',' + access_token +',' + sessionID), (err) => {
if (err)
{
console.error('error appending to file');
return;
}
console.log('added session id');
});
// res.redirect("/spotify");
// console.log(retrieveTracksSpotify(access_token, "short_term", 1, "LAST MONTH"));
// res.render("spotify", {
Expand All @@ -263,6 +289,9 @@ console.log(`/callback sessionID: ` + sessionID);

});
}



});

app.get('/refresh_token', function (req, res) {
Expand Down Expand Up @@ -304,3 +333,5 @@ app.listen(process.env.PORT || 3000, function () {
console.log(`Server is running on ${serverIP}:3000`);

});


Empty file added receiptifyv1/session_info
Empty file.
5 changes: 5 additions & 0 deletions receiptifyv1/users.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
display_name, access_token, sessionID

Ari,BQDFyKBaWPeZBAc5CTym0rrVAyqnhCkyN1CYf3q1CU4zO_di7cIPSDI6l0UrPt6m1_Cz7SdJqEWFRU89yloa0RURhAyFUyU7M1z1K2-hJDtO9cgU8kp1ZJZv-I3284y5ZdGtr_5rpts_A8hGVyyJJDQwvUiK1oJH-NjUoVGGRhrG6NPKFh18WtL0NDZteFJp0_P9KUVNbdBU-rYn5GAjXtZczfwwI15XfwVNS-xft_6GWc8O2zMz8YmV,6969
Ari,BQAKCFZetKD1yP1N6f7u7D_untncrbIiCvbhPlW9rfr3tVpkSvu_hiqh3RcGjfOh2nmZMGP_z1Z9FD70QGhoLxJ5OP43-PLg2ICnhSRwRR5-jijvUhJwgwxKIqq132bTyl-wwBK8CGpU9iqqbNZ9MlZLl_YB42Ti1zcy7fROeCHCO0XqmO5yBjQF1QdNyb_UOuqlej1joIxK6w3pRd1wyNBgR6eVsVFqXZzp7dpn-ilqUBKxaQEw2tdr,6868
Nina Perone,BQDNviWM7kQtBqBEcBNxNyLJWFMslZjnwEySeaLis1OgwK3Fe9xhjDelihY9jS_6dKXVbwQzY9kjEUZq4ZVXE_8l1lF-qUaIfkFs-48qkbG11kpbyASWCAB7UmnygABcNKdHt6ow89mWxScTj8OfzsKtcacowv8t6GQ-CaCpNGDPhE1vie2FHX6u8U-83Qc5v1941jxXrfapaak3caUZqqXNULCP00CHdGJCtTkutqOs6-WbDPd8SaCE,3141

0 comments on commit a301ae0

Please sign in to comment.