Browse Source

tweaks to use user_id not token

master
Rich Brown 8 months ago
parent
commit
fa96259bb7
5 changed files with 59 additions and 34 deletions
  1. +14
    -0
      .vscode/launch.json
  2. +4
    -7
      jsconfig.json
  3. +1
    -0
      package.json
  4. +14
    -12
      src/graphql.js
  5. +26
    -15
      src/index.js

+ 14
- 0
.vscode/launch.json View File

@@ -0,0 +1,14 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/src/index.js"
}
]
}

+ 4
- 7
jsconfig.json View File

@@ -1,10 +1,7 @@
{
"compilerOptions": {
"target": "ES5",
"module": "commonjs",
"target": "ES5",
"module": "commonjs"
},
"exclude": [
"dist",
"node_modules"
]
}
"exclude": ["dist", "node_modules"]
}

+ 1
- 0
package.json View File

@@ -4,6 +4,7 @@
"description": "",
"main": "src/index.js",
"scripts": {
"start": "node src/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],


+ 14
- 12
src/graphql.js View File

@@ -8,11 +8,12 @@ const { default: axios } = require("axios");
*/
const colorValidator = v => /^#([0-9a-f]{3}){1,2}$/i.test(v);

const url =
process.env.NODE_ENV === "production"
? process.env.GRAPHQL_URL
: "http://localhost:3000/graphql";
// const url =
// process.env.NODE_ENV === "production"
// ? process.env.GRAPHQL_URL
// : "http://localhost:3000/graphql";

const url = process.env.GRAPHQL_URL;
/**
* @param {string} query
* @param {{ timestamp?: string; color?: string; item?: string; value?: string; }} variables
@@ -24,8 +25,8 @@ const graphQuery = (query, variables, token) =>
method: "POST",
url,
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`
"Content-Type": "application/json"
// Authorization: `Bearer ${token}`
},
data: { query, variables }
})
@@ -39,8 +40,8 @@ const graphQuery = (query, variables, token) =>
*/
const updateColor = (token, color, timestamp) => {
if (!colorValidator(color)) return new Error("Not a valid #color");
const query = `mutation DoIt($timestamp: String!, $color: String!) {
updateColor(timestamp: $timestamp, color: $color) {
const query = `mutation DoIt($timestamp: String!, $color: String!, $user_id: String) {
updateColor(timestamp: $timestamp, color: $color, user_id: $user_id) {
username
current {
color
@@ -49,7 +50,8 @@ const updateColor = (token, color, timestamp) => {
}`;
const variables = {
timestamp,
color
color,
user_id: token
};
return graphQuery(query, variables, token);
};
@@ -60,13 +62,13 @@ const updateColor = (token, color, timestamp) => {
*/
const updateDisplayName = (token, string) => {
if (!string || typeof string !== "string") return new Error("Not a string");
const query = `mutation DoDisplayNameUpdate($item: String!, $value: String!) {
updateProfileItem(item: $item, value: $value) {
const query = `mutation DoDisplayNameUpdate($item: String!, $value: String!, $user_id: String!) {
updateProfileItem(item: $item, value: $value, user_id: $user_id) {
username
displayName
}
}`;
const variables = { item: "DISPLAYNAME", value: string };
const variables = { item: "DISPLAYNAME", value: string, user_id: token };
return graphQuery(query, variables, token);
};



+ 26
- 15
src/index.js View File

@@ -16,15 +16,21 @@ const watchTracks = async scrobbleName => {
const tracks = await getTracksForUser(scrobbleName, key);
// although limit=1, getTracks still sends array
const array = tracks.map(async t => {
const image = t.image.filter(i => i.size === "large")[0]["#text"];
const c = await getColors(image);
return {
artist: t.artist["#text"],
album: t.album["#text"],
track: t.name,
timestamp: t.date ? t.date.uts : Math.floor(Date.now() / 1000),
color: c[0].hex
};
try {
const image = t.image.filter(i => i.size === "large")[0]["#text"];
const c = await getColors(image);

return {
artist: t.artist["#text"],
album: t.album["#text"],
track: t.name,
timestamp: t.date ? t.date.uts : Math.floor(Date.now() / 1000),
color: c[0].hex
};
} catch (ee) {
console.log(ee);
return {};
}
});
return Promise.all(array);
};
@@ -35,17 +41,22 @@ const watchTracks = async scrobbleName => {
*/
const watchLastFM = (username, token) => {
if (!nowPlaying[username]) nowPlaying[username] = "";
setInterval(async () => {
return setInterval(async () => {
const track = await watchTracks(username);
const songName = track[0].track;
const color = track[0].color;
const timestamp = track[0].timestamp.toString();
if (nowPlaying[username] === songName) return null;
if (nowPlaying[username] === songName) {
console.log("same song");
return null;
}
nowPlaying[username] = songName;
updateDisplayName(token, songName)
.then(() => updateColor(token, color, timestamp))
.catch(e => console.log("update Failed"));

return updateColor(token, color, timestamp);
// return updateDisplayName(token, songName)
// .then(() => updateColor(token, color, timestamp))
// .catch(e => console.log("update Failed"));
}, threeMin);
};

watchLastFM("rlbgator", "test4"); // TODO: update token to match a better co10rs user.
watchLastFM("rlbgator", "5d84c8abb82d14c510e4fc4b"); // TODO: update token to match a better co10rs user.

Loading…
Cancel
Save