Skip to content

Commit

Permalink
Current progress
Browse files Browse the repository at this point in the history
  • Loading branch information
samip5 committed Jan 4, 2024
1 parent 8be218a commit f7538f9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ const configDir = process.env.CONFIG_DIR || path.resolve(projectDir, `./config`)
// do something with the authorized apple source api client
//const apl = scrobbleSources.getByName('aap') as AppleSource;
//const recentPlays = await apl.apiClient.getRecentlyPlayed(20, 0, "songs");
//console.log(`Recent: ${JSON.stringify(recentPlays, null, 2)}`);


scheduler.addSimpleIntervalJob(new SimpleIntervalJob({
minutes: 20,
Expand Down
59 changes: 57 additions & 2 deletions src/backend/sources/AppleSource.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import MemorySource from "./MemorySource";
import {AppleMusicSourceConfig} from "../common/infrastructure/config/source/apple";
import {InternalConfig} from "../common/infrastructure/Atomic";
import {FormatPlayObjectOptions, InternalConfig} from "../common/infrastructure/Atomic";
import EventEmitter from "events";
import {readJson, readText, writeFile} from "../utils";
import MusicKit from 'node-musickit-api/personalized/index.js';
import {createJWT} from 'node-musickit-api/modules/createJWT.js';
import {RecentlyPlayedOptions} from "./AbstractSource";
import {PlayObject} from "../../core/Atomic";


export class AppleSource extends MemorySource {
Expand All @@ -24,7 +26,8 @@ export class AppleSource extends MemorySource {
constructor(name: any, config: AppleMusicSourceConfig, internal: InternalConfig, emitter: EventEmitter) {
super('apple', name, config, internal, emitter);

this.workingCredsPath = `${internal.configDir}/currentCreds-apple-${name}.json`;
this.workingCredsPath = `${internal.configDir}/currentCreds-apple-${name}.json`
this.logger.debug(`APPLE workingCredsPath: ${internal.configDir}/currentCreds-apple-${name}.json`);
}

initialize = async () => {
Expand Down Expand Up @@ -81,6 +84,58 @@ export class AppleSource extends MemorySource {
return true;
}


static formatPlayObj(obj: any, options: FormatPlayObjectOptions = {}): PlayObject {
const {newFromSource = false} = options;
const {
id,
attributes: {
name: tittle,
albumName: album,
artistName: artist,
durationInMillis: duration
}
} = obj;
return {
data: {
artists: [artist],
album,
duration: duration / 1000
},
meta: {
source: 'Apple',
trackId: id,
newFromSource
}
}
}

// @ts-ignore
getRecentlyPlayed = async(options: RecentlyPlayedOptions = {})=> {
const {formatted = false} = options;
const resp = await this.apiClient.getRecentlyPlayed(20, 0, "songs");
const {
body: {
data = []
} = {}
} = resp;
const currentData = data.map(AppleSource.formatPlayObj);
this.logger.debug(`RecentlyPlayedData: ${currentData}`);
return this.processRecentPlays(data);
}

doAuthentication = async () => {
try {
const credFile = await readJson(this.workingCredsPath, {throwOnNotFound: false});
if (credFile !== undefined) {
return true;
}
} catch (e) {
this.logger.warn('Error occurred with reading APPLE credentials', {path: this.workingCredsPath});
return false;
}
}

generateDeveloperToken = () => {
return createJWT({
key: this.keyContents,
Expand Down

0 comments on commit f7538f9

Please sign in to comment.