Skip to content

Instantly share code, notes, and snippets.

@seresk
seresk / AppleMusicPlayBehavior.ts
Created April 19, 2021 21:02
AppleMusicPlayBehavior partial implementation
export class AppleMusicPlayBehavior implements IPlayBehaviour {
public TAG: string;
public playState: PlayStateEnum;
public playedMs: number;
public playlist: NowPlayingPlaylistModel = new NowPlayingPlaylistModel();
public progress: number;
public trackPlayedLabel: string;
public trackRemainingLabel: string;
public volume: number;
musicKit: MusicKitInstance;
@seresk
seresk / AppleMusicEngine.ts
Last active April 19, 2021 20:52
AppleMusicEngine partial inplementation
import MusicKitInstance = MusicKit.MusicKitInstance;
import AppleMusicKit = MusicKit;
export class AppleMusicEngine extends EngineBase implements IStreamEngine {
TAG = 'AppleMusicEngine';
musicKit: MusicKitInstance;
constructor(
instanciatedByTAG: string
@seresk
seresk / MusicKitInit.js
Created April 18, 2021 20:50
MusicKit initialization script
document.addEventListener('musickitconfigured', function () {
console.log('musickitconfigured event triggered');
})
document.addEventListener('musickitloaded', function () {
// MusicKit global is now defined
MusicKit.configure({
developerToken: 'YOUR DEVELOPER TOKEN HERE',
app: {
@seresk
seresk / MusicKitTokenGenerator.js
Created April 18, 2021 15:14
MusicKit JWT Token Generator
"use strict";
const fs = require("fs");
const jwt = require("jsonwebtoken");
const request = require("request");
const privateKey = fs.readFileSync("AuthKey.p8").toString(); //your MusicKit private key
const jwtToken = jwt.sign({}, privateKey, {
algorithm: "ES256",
expiresIn: "180d",
@seresk
seresk / TransferDevicePartial.ts
Created April 12, 2021 20:20
Transferring the playback to another device
private async _transferDevice(): Promise<void> {
const devicesResponse = await this.dataService.put<void>(
'https://api.spotify.com/v1/me/player',
JSON.stringify({device_ids: [this.deviceService.activeDevice.id]}),
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.authorizationService.spotifyAccessToken}`
}
@seresk
seresk / SpotifySetVolume.ts
Created April 12, 2021 20:04
Setting the volume
private async _setVolume(volume: number): Promise<void> {
const devicesResponse = await this.dataService.put<void>(
'https://api.spotify.com/v1/me/player/volume?volume_percent=' + volume + '&device_id=' + this.deviceService.activeDevice.id,
null,
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.authorizationService.spotifyAccessToken}`
}
private async _seek(positionMs: number): Promise<void> {
await this.dataService.put<any>(
`https://api.spotify.com/v1/me/player/seek?position_ms=${positionMs}`,
null,
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.authorizationService.spotifyAccessToken}`
}
}
@seresk
seresk / SpotifyEnginePlayUpdatePartial.ts
Created April 6, 2021 22:18
SpotifyEngine with play and update methods
export class SpotifyEngine extends EngineBase
implements IStreamEngine{
///...
private async playTrack(trackId: string): Promise<void> {
await this.dataService.put<any>(
SpotifyURLResources.PlayTrack + `?device_id=${this.deviceService.activeDevice.id}`,
JSON.stringify({uris: [trackId]}),
{
@seresk
seresk / SpotifyEnginePartial.ts
Last active April 6, 2021 22:07
SpotifyEngine
export class SpotifyEngine extends EngineBase
implements IStreamEngine{
///...
private async getAlbums(): Promise<Array<AlbumModel>> {
const limitQueryString = '?limit=50';
const result = new Array<AlbumModel>();
let url = SpotifyURLResources.UsersSavedAlbums + limitQueryString;
do {
@seresk
seresk / spotify-server.js
Created April 5, 2021 20:48
Spotify Authorization server
var express = require('express'); // Express web server framework
var request = require('request'); // "Request" library
var cors = require('cors');
var querystring = require('querystring');
var cookieParser = require('cookie-parser');
var client_id = '*****'; // Your client id
var client_secret = '****'; // Your secret
var redirect_uri = 'http://localhost:8888/callback'; // Your redirect uri