use-listen-along
Advanced tools
Comparing version 1.2.2 to 1.3.0
@@ -5,2 +5,6 @@ 'use strict'; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var dayjs = _interopDefault(require('dayjs')); | ||
/** | ||
@@ -12,5 +16,7 @@ * use-listen-along | ||
*/ | ||
function useListenAlong(snowflake, auth, disconnect) { | ||
var track; | ||
var currently; | ||
var position; | ||
var connected = false; | ||
@@ -20,3 +26,3 @@ var error = null; | ||
var parseResponse = function parseResponse(res) { | ||
var _res$error, _res$data, _res$data2, _res$data2$spotify; | ||
var _res$error, _res$data, _res$data2, _res$data2$spotify, _res$data3, _res$data3$spotify, _res$data3$spotify$ti, _res$data4, _res$data4$spotify, _res$data4$spotify$ti; | ||
@@ -29,6 +35,10 @@ var error = { | ||
var track = (_res$data2 = res.data) == null ? void 0 : (_res$data2$spotify = _res$data2.spotify) == null ? void 0 : _res$data2$spotify.track_id; | ||
var end = res == null ? void 0 : (_res$data3 = res.data) == null ? void 0 : (_res$data3$spotify = _res$data3.spotify) == null ? void 0 : (_res$data3$spotify$ti = _res$data3$spotify.timestamps) == null ? void 0 : _res$data3$spotify$ti.end; | ||
var start = res == null ? void 0 : (_res$data4 = res.data) == null ? void 0 : (_res$data4$spotify = _res$data4.spotify) == null ? void 0 : (_res$data4$spotify$ti = _res$data4$spotify.timestamps) == null ? void 0 : _res$data4$spotify$ti.start; | ||
return { | ||
error: error, | ||
np: np, | ||
track: track | ||
track: track, | ||
end: end, | ||
start: start | ||
}; | ||
@@ -49,2 +59,3 @@ }; | ||
currently = r == null ? void 0 : (_r$item = r.item) == null ? void 0 : _r$item.id; | ||
position = r == null ? void 0 : r.progress_ms; | ||
}); | ||
@@ -55,5 +66,5 @@ }; | ||
if (r.error.status) throw new Error(JSON.stringify(r.error)); | ||
currentlyPlaying(); | ||
currentlyPlaying(); // if the track is different OR the song is behind by 15 seconds OR the song is ahead by 15 seconds | ||
if (r.track && currently && r.track !== currently && r.track !== track && r.np && !disconnect) { | ||
if (r.track && currently && r.track !== currently && r.track !== track && r.np && !disconnect || r.track && position && !disconnect && (r.end - r.start - dayjs(r.end).diff(dayjs(), 'millisecond')) / 1000 - 15 > position / 1000 || r.track && position && !disconnect && (r.end - r.start - dayjs(r.end).diff(dayjs(), 'millisecond')) / 1000 + 15 < position / 1000) { | ||
fetch('https://api.spotify.com/v1/me/player/play', { | ||
@@ -67,3 +78,6 @@ method: 'PUT', | ||
body: JSON.stringify({ | ||
uris: ["spotify:track:" + r.track] | ||
uris: ["spotify:track:" + r.track], | ||
position_ms: // total ms | ||
r.end - r.start - // time left in the song | ||
dayjs(r.end).diff(dayjs(), 'millisecond') | ||
}) | ||
@@ -70,0 +84,0 @@ }).then(function (r) { |
@@ -1,2 +0,2 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.useListenAlong=function(t,r,e){var n,o,s=!1,a=null,i=function(t){var r,e,n,o;return{error:{status:!t.success,message:null==t||null==(r=t.error)?void 0:r.message},np:null==(e=t.data)?void 0:e.listening_to_spotify,track:null==(n=t.data)||null==(o=n.spotify)?void 0:o.track_id}},u=function(t){if(t.error.status)throw new Error(JSON.stringify(t.error));fetch("https://api.spotify.com/v1/me/player/currently-playing?market=from_token",{method:"GET",headers:{Authorization:"Bearer "+r}}).then((function(t){return t.json()})).then((function(t){var r;o=null==t||null==(r=t.item)?void 0:r.id})),t.track&&o&&t.track!==o&&t.track!==n&&t.np&&!e&&(fetch("https://api.spotify.com/v1/me/player/play",{method:"PUT",headers:{Authorization:"Bearer "+r,"Content-Type":"application/json",Accept:"application/json"},body:JSON.stringify({uris:["spotify:track:"+t.track]})}).then((function(t){s=204==t.status,a=204!==t.status?403==t.status?"User must have a premium account.":t.statusText:null})).catch((function(t){if(t.status)throw new Error(t)})),n=t.track)};return setInterval((function(){e||fetch("https://api.lanyard.rest/v1/users/"+t).then((function(t){return t.json()})).then(i).then(u)}),1500),{connected:s,error:a}}; | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e=(t=require("dayjs"))&&"object"==typeof t&&"default"in t?t.default:t;exports.useListenAlong=function(t,n,r){var s,a,i,o=!1,l=null,u=function(t){var e,n,r,s,a,i,o,l,u,c;return{error:{status:!t.success,message:null==t||null==(e=t.error)?void 0:e.message},np:null==(n=t.data)?void 0:n.listening_to_spotify,track:null==(r=t.data)||null==(s=r.spotify)?void 0:s.track_id,end:null==t||null==(a=t.data)||null==(i=a.spotify)||null==(o=i.timestamps)?void 0:o.end,start:null==t||null==(l=t.data)||null==(u=l.spotify)||null==(c=u.timestamps)?void 0:c.start}},c=function(t){if(t.error.status)throw new Error(JSON.stringify(t.error));fetch("https://api.spotify.com/v1/me/player/currently-playing?market=from_token",{method:"GET",headers:{Authorization:"Bearer "+n}}).then((function(t){return t.json()})).then((function(t){var e;a=null==t||null==(e=t.item)?void 0:e.id,i=null==t?void 0:t.progress_ms})),(t.track&&a&&t.track!==a&&t.track!==s&&t.np&&!r||t.track&&i&&!r&&(t.end-t.start-e(t.end).diff(e(),"millisecond"))/1e3-15>i/1e3||t.track&&i&&!r&&(t.end-t.start-e(t.end).diff(e(),"millisecond"))/1e3+15<i/1e3)&&(fetch("https://api.spotify.com/v1/me/player/play",{method:"PUT",headers:{Authorization:"Bearer "+n,"Content-Type":"application/json",Accept:"application/json"},body:JSON.stringify({uris:["spotify:track:"+t.track],position_ms:t.end-t.start-e(t.end).diff(e(),"millisecond")})}).then((function(t){o=204==t.status,l=204!==t.status?403==t.status?"User must have a premium account.":t.statusText:null})).catch((function(t){if(t.status)throw new Error(t)})),s=t.track)};return setInterval((function(){r||fetch("https://api.lanyard.rest/v1/users/"+t).then((function(t){return t.json()})).then(u).then(c)}),1500),{connected:o,error:l}}; | ||
//# sourceMappingURL=use-listen-along.cjs.production.min.js.map |
@@ -0,1 +1,3 @@ | ||
import dayjs from 'dayjs'; | ||
/** | ||
@@ -7,5 +9,7 @@ * use-listen-along | ||
*/ | ||
function useListenAlong(snowflake, auth, disconnect) { | ||
var track; | ||
var currently; | ||
var position; | ||
var connected = false; | ||
@@ -15,3 +19,3 @@ var error = null; | ||
var parseResponse = function parseResponse(res) { | ||
var _res$error, _res$data, _res$data2, _res$data2$spotify; | ||
var _res$error, _res$data, _res$data2, _res$data2$spotify, _res$data3, _res$data3$spotify, _res$data3$spotify$ti, _res$data4, _res$data4$spotify, _res$data4$spotify$ti; | ||
@@ -24,6 +28,10 @@ var error = { | ||
var track = (_res$data2 = res.data) == null ? void 0 : (_res$data2$spotify = _res$data2.spotify) == null ? void 0 : _res$data2$spotify.track_id; | ||
var end = res == null ? void 0 : (_res$data3 = res.data) == null ? void 0 : (_res$data3$spotify = _res$data3.spotify) == null ? void 0 : (_res$data3$spotify$ti = _res$data3$spotify.timestamps) == null ? void 0 : _res$data3$spotify$ti.end; | ||
var start = res == null ? void 0 : (_res$data4 = res.data) == null ? void 0 : (_res$data4$spotify = _res$data4.spotify) == null ? void 0 : (_res$data4$spotify$ti = _res$data4$spotify.timestamps) == null ? void 0 : _res$data4$spotify$ti.start; | ||
return { | ||
error: error, | ||
np: np, | ||
track: track | ||
track: track, | ||
end: end, | ||
start: start | ||
}; | ||
@@ -44,2 +52,3 @@ }; | ||
currently = r == null ? void 0 : (_r$item = r.item) == null ? void 0 : _r$item.id; | ||
position = r == null ? void 0 : r.progress_ms; | ||
}); | ||
@@ -50,5 +59,5 @@ }; | ||
if (r.error.status) throw new Error(JSON.stringify(r.error)); | ||
currentlyPlaying(); | ||
currentlyPlaying(); // if the track is different OR the song is behind by 15 seconds OR the song is ahead by 15 seconds | ||
if (r.track && currently && r.track !== currently && r.track !== track && r.np && !disconnect) { | ||
if (r.track && currently && r.track !== currently && r.track !== track && r.np && !disconnect || r.track && position && !disconnect && (r.end - r.start - dayjs(r.end).diff(dayjs(), 'millisecond')) / 1000 - 15 > position / 1000 || r.track && position && !disconnect && (r.end - r.start - dayjs(r.end).diff(dayjs(), 'millisecond')) / 1000 + 15 < position / 1000) { | ||
fetch('https://api.spotify.com/v1/me/player/play', { | ||
@@ -62,3 +71,6 @@ method: 'PUT', | ||
body: JSON.stringify({ | ||
uris: ["spotify:track:" + r.track] | ||
uris: ["spotify:track:" + r.track], | ||
position_ms: // total ms | ||
r.end - r.start - // time left in the song | ||
dayjs(r.end).diff(dayjs(), 'millisecond') | ||
}) | ||
@@ -65,0 +77,0 @@ }).then(function (r) { |
{ | ||
"name": "use-listen-along", | ||
"description": "A react hook to listen alongside others on spotify.", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"license": "MIT", | ||
@@ -45,3 +45,6 @@ "main": "dist/index.js", | ||
"hook" | ||
] | ||
], | ||
"dependencies": { | ||
"dayjs": "^1.10.6" | ||
} | ||
} |
import { LanyardResponse } from './types'; | ||
import dayjs from 'dayjs'; | ||
@@ -16,2 +17,3 @@ /** | ||
let currently: string | undefined; | ||
let position: number | undefined; | ||
@@ -25,2 +27,4 @@ let connected: boolean = false; | ||
const track = res.data?.spotify?.track_id; | ||
const end = res?.data?.spotify?.timestamps?.end; | ||
const start = res?.data?.spotify?.timestamps?.start; | ||
@@ -31,2 +35,4 @@ return { | ||
track, | ||
end, | ||
start, | ||
}; | ||
@@ -48,2 +54,3 @@ }; | ||
currently = r?.item?.id; | ||
position = r?.progress_ms; | ||
}); | ||
@@ -56,2 +63,4 @@ }; | ||
track: string | undefined; | ||
end: number; | ||
start: number; | ||
}) => { | ||
@@ -62,9 +71,22 @@ if (r.error.status) throw new Error(JSON.stringify(r.error)); | ||
// if the track is different OR the song is behind by 15 seconds OR the song is ahead by 15 seconds | ||
if ( | ||
r.track && | ||
currently && | ||
r.track !== currently && | ||
r.track !== track && | ||
r.np && | ||
!disconnect | ||
(r.track && | ||
currently && | ||
r.track !== currently && | ||
r.track !== track && | ||
r.np && | ||
!disconnect) || | ||
(r.track && | ||
position && | ||
!disconnect && | ||
(r.end - r.start - dayjs(r.end).diff(dayjs(), 'millisecond')) / 1000 - | ||
15 > | ||
position / 1000) || | ||
(r.track && | ||
position && | ||
!disconnect && | ||
(r.end - r.start - dayjs(r.end).diff(dayjs(), 'millisecond')) / 1000 + | ||
15 < | ||
position / 1000) | ||
) { | ||
@@ -80,2 +102,7 @@ fetch('https://api.spotify.com/v1/me/player/play', { | ||
uris: [`spotify:track:${r.track}`], | ||
position_ms: | ||
// total ms | ||
r.end - | ||
r.start - // time left in the song | ||
dayjs(r.end).diff(dayjs(), 'millisecond'), | ||
}), | ||
@@ -82,0 +109,0 @@ }) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
39756
460
1
2
+ Addeddayjs@^1.10.6
+ Addeddayjs@1.11.13(transitive)