Skip to content

Commit

Permalink
Merge branch 'main' into user_checkbox
Browse files Browse the repository at this point in the history
  • Loading branch information
tun82434 authored Apr 15, 2024
2 parents c425fc8 + f72253f commit fc46704
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Web application inspired by https://www.instagram.com/albumreceipts/. Generates

The application inspired by https://receiptify.herokuapp.com/. Generates receipts on a website for a single user's spotify data.

## Running the App Locally
## Hosting the App Locally

This app runs on Node.js. On [its website](http://www.nodejs.org/download/) you can find instructions on how to install it. You can also follow [this gist](https://gist.github.com/isaacs/579814) for a quick and easy way to install Node.js and npm.

Expand All @@ -23,23 +23,28 @@ To do so, go to [your Spotify for Developers Dashboard](https://beta.developer.s
- http://localhost:3000 (needed for the implicit grant flow)
- http://localhost:3000/callback

Once you have created your app, load the `client_id`, `redirect_uri` and `client_secret` into a `config.js` file.
Once you have created your app, change the `client_id` and `client_secret` values in the a `app.js` file to your own Spotify Developer credentials.

In order to run the app, open the folder, and run its `app.js` file:

$ cd authorization_code
$ node app.js

Then, open `http://localhost:3000` in a browser.

#### Hosting the App Locally

To allow users to access your app, you need to add them into user managment from the Spotify for Developers Dashboard.
To allow users to access your app, you need to add their names and emails under user managment from the Spotify for Developers Dashboard.

When hosting, set the Redirect URI as:
- http://[serverIP]:3000 (needed for implicit grant flow)
- http://[serverIP]:3000/callback

Setting Redirect URI as http://localhost:3000/callback when trying to host the application for local clients to connect to will fail because the redirect URI points to the client's IP address (localhost).

## Joining the App Locally

A user on the same network the app is hosted on can put the host's IP and port number into their browser (ex. 10.188.203.159:3000).

The first person will click the "Create Session" button and log into their Spotify account. A Session ID will appear on the top of their receipt.

Other users can then click "Join Session" and after entering the shared Session ID, they can log into their Spotify accounts and view the receipt.


33 changes: 27 additions & 6 deletions receiptifyv1/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ const cors = require('cors');
require('dotenv').config();


const client_id = '035844db2ccb4d0698ab8e14bb12f27a';
const client_secret = '8bfd5a9fa7a44aedbf8bf8f513236b4f';
const client_id = '792207d6524f4255a1730e478d8b66f6';
const client_secret = 'fd5c90696d984ca7a65a54853f340c70';
//const privateKey = fs.readFileSync('AuthKey_A8FKGGUQP3.p8').toString();
const teamId = process.env.teamId;
const keyId = process.env.keyId;
Expand Down Expand Up @@ -197,12 +197,27 @@ async function fetchProfile(token) {
}

app.get('/getUsers', async (req, res) =>{
console.log('/getUsers');
const sessionID = req.query.sessionID;
users = await processFile('users.csv', sessionID);
const type = req.query.type;
let col;
//console.log(`Type: ${type}`);
if (type === 'display_name')
{
console.log('Column: display_name');
col = 0;
}
else if (type === 'access_token')
{
console.log('Column: access_token');
col = 1;
}
users = await processFile('users.csv', sessionID, col);
console.log(users[0]);
res.json(users);
});

async function processFile(filePath, sessionID) {
async function processFile(filePath, sessionID, col) {
try {
const fileStream = fs.createReadStream(filePath);
const rl = readline.createInterface({
Expand All @@ -213,7 +228,7 @@ async function processFile(filePath, sessionID) {
for await (const line of rl) {
const row = line.split(',');
if (row[2] == sessionID) {
users.push(row[0]);
users.push(row[col]);
}
}
await rl.close();
Expand Down Expand Up @@ -280,8 +295,14 @@ app.get('/callback', function (req, res) {
})
);

// Gets time (year-month-day hour:min:sec)
var currentDate = new Date();
var access_time = currentDate.getFullYear() + '-' + (currentDate.getMonth()+1) + '-' + currentDate.getDate() + ' ' +
currentDate.getHours() + ':' + currentDate.getMinutes() + ':' + currentDate.getSeconds();
console.log("Token Access Time: ", access_time);

// Writing to users.csv (Database)
fs.appendFile('users.csv', ('\n'+ profile.display_name + ',' + access_token +',' + sessionID + ','), (err) => {
fs.appendFile('users.csv', ('\n'+ profile.display_name + ',' + access_token +',' + sessionID + ',' + access_time + ','), (err) => {
if (err)
{
console.error('Error: Could not write to database.');
Expand Down
12 changes: 9 additions & 3 deletions receiptifyv1/public/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,9 +612,9 @@ const removeTrack = (i) => {
}
};

async function fetchUsers(sessionID) {
async function fetchUsers(sessionID, type) {
try {
const response = await fetch (`/getUsers?sessionID=${sessionID}`);
const response = await fetch (`/getUsers?sessionID=${sessionID}&type=${type}`);
if (!response.ok) {
throw new Error(`Error fetching data: ${response.statusText}`);
}
Expand All @@ -626,6 +626,7 @@ async function fetchUsers(sessionID) {
}
;}


function checkboxUpdate(response, stats, state, users_checkbox, user, isChecked) {
if (users_checkbox.includes(null)){
console.log('true null');
Expand Down Expand Up @@ -708,7 +709,12 @@ const displayReceipt = (response, stats, state, users_checkbox = []) => {
let users = [];
(async () => {
try {
users = await fetchUsers(sessionID);
users = await fetchUsers(sessionID, 'display_name');
console.log('Fetched Users');
tokens = await fetchUsers(sessionID, 'access_token');
console.log(`Fetched Tokens: ${tokens}`);

console.log(tokens);
let total = 0;
const date = TODAY.toLocaleDateString('en-US', DATE_OPTIONS).toUpperCase();
const tracksFormatted = responseItems.map((item, i) => {
Expand Down
3 changes: 1 addition & 2 deletions receiptifyv1/users.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
display_name, access_token, sessionID

✧ jizzica ✧,BQCs--HQs0rDdKZTwC-OSwwxz_krtDrkM_wHF2u8dTy5PgY540zMnVH29uw5vaHudBhsNnVxD61rT53CoobgB8D7Kfuf3yVIlJxpkgi6D2I8tJeTC5Gumq4ML7iwOJXVB25kWA5BWJ2sUicSX6kkFnWQdEy2L3FagqmncVgVrLXqkpL_7n3H0rj2w37O-NmYZUZb6YRSmWeHh4-s4jnBjwlkPa9BTJshR4zP3Y4,071232,
Martin Duong,BQAeYschcFx7E8jK9uHTBjyNRWK36cuA2XqhJo1xAR437apd1xfrHTvOCEcPopDJhYEoliszJECHWHmU7cb6kzLO1kDdhFXrsTdpd2CaJltJUDfp4cQxp55-v5MRijaeBHLntedewUChUdgaqIuRnXSb9LR2jiRD-Yjdjz3-rSCFNzwxNo4jhD5jlS4aVQk9tnBVVcI4J8XOE53-sO0LjnF9UOWpu1_5HoZBKqM,071232,

0 comments on commit fc46704

Please sign in to comment.