Selector/Selector.Web/scripts/now.ts

158 lines
5.4 KiB
TypeScript
Raw Normal View History

2021-10-31 19:47:07 +00:00
import * as signalR from "@microsoft/signalr";
2021-11-09 18:17:22 +00:00
import * as Vue from "vue";
import { TrackAudioFeatures, PlayCount, CurrentlyPlayingDTO, CountSample } from "./HubInterfaces";
import NowPlayingCard from "./Now/NowPlayingCard";
2021-11-11 19:54:28 +00:00
import { AudioFeatureCard, AudioFeatureChartCard, PopularityCard, SpotifyLogoLink } from "./Now/Spotify";
import { PlayCountChartCard, CombinedPlayCountChartCard } from "./Now/PlayCountGraph";
import { ArtistBreakdownChartCard } from "./Now/ArtistBreakdownGraph";
import { PlayCountCard, LastFmLogoLink } from "./Now/LastFm";
import BaseInfoCard from "./Now/BaseInfoCard";
2021-10-31 19:47:07 +00:00
const connection = new signalR.HubConnectionBuilder()
2022-10-10 11:44:47 +01:00
.withUrl("/nowhub")
2022-11-12 11:05:42 +00:00
.withAutomaticReconnect()
2021-10-31 19:47:07 +00:00
.build();
connection.start()
.then(val => {
connection.invoke("OnConnected");
})
.catch(err => console.error(err));
2021-10-31 19:47:07 +00:00
interface InfoCard {
Content: string
}
interface NowPlaying {
currentlyPlaying?: CurrentlyPlayingDTO,
trackFeatures?: TrackAudioFeatures,
playCount?: PlayCount,
cards: InfoCard[]
}
export interface ScrobbleDataSeries {
label: string,
colour: string,
data: CountSample[]
}
2021-11-09 18:17:22 +00:00
const app = Vue.createApp({
data() {
return {
currentlyPlaying: undefined,
trackFeatures: undefined,
playCount: undefined,
cards: []
} as NowPlaying
2021-11-09 18:17:22 +00:00
},
computed: {
lastfmTrack() {
return {
name: this.currentlyPlaying.track.name,
artist: this.currentlyPlaying.track.artists[0].name,
album: this.currentlyPlaying.track.album.name,
album_artist: this.currentlyPlaying.track.album.artists[0].name,
};
},
showArtistChart(){
return this.playCount !== null && this.playCount !== undefined && this.playCount.artistCountData.length > 3;
},
showAlbumChart() {
return this.playCount !== null && this.playCount !== undefined && this.playCount.albumCountData.length > 3;
},
showTrackChart(){
return this.playCount !== null && this.playCount !== undefined && this.playCount.trackCountData.length > 3;
},
2022-06-28 07:25:24 +01:00
showArtistBreakdown(){
return this.playCount !== null && this.playCount !== undefined && this.playCount.artist > 0;
},
earliestDate(){
return this.playCount.artistCountData[0].timeStamp;
},
latestDate(){
return this.playCount.artistCountData.at(-1).timeStamp;
},
trackGraphTitle() { return `${this.currentlyPlaying.track.name} 🎵`},
albumGraphTitle() { return `${this.currentlyPlaying.track.album.name} 💿`},
artistGraphTitle() { return `${this.currentlyPlaying.track.artists[0].name} 🎤`},
combinedData(){
return [
{
label: "artist",
colour: "#598556",
data: this.playCount.artistCountData
},
{
label: "album",
colour: "#a34c77",
data: this.playCount.albumCountData
},
{
label: "track",
colour: "#7a99c2",
data: this.playCount.trackCountData
}];
}
},
2021-11-09 18:17:22 +00:00
created() {
connection.on("OnNewPlaying", (context: CurrentlyPlayingDTO) =>
{
console.log(context);
this.currentlyPlaying = context;
2021-11-11 19:54:28 +00:00
this.trackFeatures = null;
this.playCount = null;
this.cards = [];
if(context.track !== null && context.track !== undefined)
{
if(context.track.id !== null)
{
connection.invoke("SendAudioFeatures", context.track.id);
}
connection.invoke("SendPlayCount",
context.track.name,
context.track.artists[0].name,
context.track.album.name,
context.track.album.artists[0].name
);
connection.invoke("SendFacts",
context.track.name,
context.track.artists[0].name,
context.track.album.name,
context.track.album.artists[0].name
);
}
});
connection.on("OnNewAudioFeature", (feature: TrackAudioFeatures) =>
{
console.log(feature);
this.trackFeatures = feature;
});
connection.on("OnNewPlayCount", (count: PlayCount) => {
console.log(count);
this.playCount = count;
});
connection.on("OnNewCard", (card: InfoCard) => {
console.log(card);
this.cards.push(card);
});
2021-11-09 18:17:22 +00:00
}
});
app.component("now-playing-card", NowPlayingCard);
2021-11-11 19:54:28 +00:00
app.component("audio-feature-card", AudioFeatureCard);
app.component("audio-feature-chart-card", AudioFeatureChartCard);
app.component("info-card", BaseInfoCard);
app.component("popularity", PopularityCard);
app.component("spotify-logo", SpotifyLogoLink);
app.component("lastfm-logo", LastFmLogoLink);
app.component("play-count-card", PlayCountCard);
app.component("play-count-chart-card", PlayCountChartCard);
app.component("play-count-chart-card-comb", CombinedPlayCountChartCard);
app.component("artist-breakdown", ArtistBreakdownChartCard);
2021-11-09 18:17:22 +00:00
const vm = app.mount('#app');