Skip to content

Commit

Permalink
Merge pull request #34 from calluswhatyouwant/v0.2.x
Browse files Browse the repository at this point in the history
Upgrade to v0.2.0
  • Loading branch information
JoseRenan committed Nov 22, 2018
2 parents a392a10 + 29f5563 commit a89976b
Show file tree
Hide file tree
Showing 38 changed files with 2,372 additions and 46 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"Robson Junior <jrobsonjr16@gmail.com> (https://github.com/JRobsonJr)"
],
"license": "MIT",
"version": "0.1.3",
"version": "0.2.0",
"dependencies": {
"axios": "^0.18.0"
},
Expand Down
9 changes: 6 additions & 3 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export * from './albums';
export * from './artists';
export * from './driver';
export * from './tracks';
export * from './follow';
export * from './artists';
export * from './albums';
export * from './personalization';
export * from './playlists';
export * from './player';
export * from './search';
export * from './tracks';
52 changes: 52 additions & 0 deletions src/lib/models/paging/cursor-based-page.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { getAxiosSpotifyInstance } from '../../driver';

class CursorBasedPage<T> {
private t: new (json: any) => T;
href: string;
items: T[];
limit: number;
next: string;
cursors: any;
total: number;

constructor(json: any, t: new (json: any) => T) {
this.t = t;
this.href = json.href;
this.items = json.items.map((json: any) => new t(json));
this.limit = json.limit;
this.next = json.next ? json.next.split('?')[1] : null;
this.cursors = json.cursors;
this.total = json.total;
}

get queryParams(): any {
const queryString = this.href.split('?')[1];
const paramsString = queryString.split('&');
const queryParams: any = {};

for (const param of paramsString) {
const [name, value] = param.split('=');
queryParams[name] = value;
}

return queryParams;
}

private getAxiosPageInstance() {
const instance = getAxiosSpotifyInstance();
instance.defaults.baseURL = this.href.split('?')[0];
return instance;
}

hasNext() {
return Boolean(this.next);
}

async getNextPage() {
if (!this.hasNext()) throw new Error('There are no more pages');
const response = await this.getAxiosPageInstance().get(`?${this.next}`);
return new CursorBasedPage<T>(response.data, this.t);
}
}

export default CursorBasedPage;
59 changes: 42 additions & 17 deletions src/lib/models/paging/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,42 @@ class Page<T> {

total: number;

constructor(json: any, t: new (json: any) => T) {
wrapper?: string;

constructor(json: any, t: new (json: any) => T, wrapper?: string) {
this.wrapper = wrapper;
let unwrappedJson = json;
if (wrapper) unwrappedJson = unwrappedJson[wrapper];
this.t = t;
this.href = json.href.split('?')[0];
this.items = json.items.map((json: any) => new t(json));
this.limit = json.limit;
this.next = json.next ? json.next.split('?')[1] : null;
this.offset = json.offset;
this.previous = json.previous ? json.previous.split('?')[1] : null;
this.total = json.total;
this.href = unwrappedJson.href;
this.items = unwrappedJson.items.map((json: any) => new t(json));
this.limit = unwrappedJson.limit;
this.next = unwrappedJson.next
? unwrappedJson.next.split('?')[1]
: null;
this.offset = unwrappedJson.offset;
this.previous = unwrappedJson.previous
? unwrappedJson.previous.split('?')[1]
: null;
this.total = unwrappedJson.total;
}

get queryParams(): any {
const queryString = this.href.split('?')[1];
const paramsString = queryString.split('&');
const queryParams: any = {};

for (const param of paramsString) {
const [name, value] = param.split('=');
queryParams[name] = value;
}

return queryParams;
}

private getAxiosPageInstance() {
const instance = getAxiosSpotifyInstance();
instance.defaults.baseURL = this.href;
instance.defaults.baseURL = this.href.split('?')[0];
return instance;
}

Expand All @@ -44,22 +66,25 @@ class Page<T> {

async getNextPage() {
if (!this.hasNext()) throw new Error('There are no more pages');
const params = { limit: this.limit, offset: this.offset + this.limit };
const params = {
...this.queryParams,
limit: this.limit,
offset: this.offset + this.limit,
};
const response = await this.getAxiosPageInstance().get('/', { params });
return new Page<T>(response.data, this.t);
return new Page<T>(response.data, this.t, this.wrapper);
}

async getPreviousPage(forceLimit = false) {
async getPreviousPage(includeRepeated = false) {
if (!this.hasPrevious()) throw new Error('There are no more pages');
let limit = this.limit;
if (this.offset < this.limit && !forceLimit) {
limit = this.offset;
}
const params = { limit, offset: 0 };
if (this.offset < this.limit && !includeRepeated) limit = this.offset;
const offset = Math.max(this.offset - this.limit, 0);
const params = { ...this.queryParams, limit, offset };
const response = await this.getAxiosPageInstance().get('/', {
params,
});
return new Page<T>(response.data, this.t);
return new Page<T>(response.data, this.t, this.wrapper);
}
}

Expand Down
22 changes: 22 additions & 0 deletions src/lib/models/player/currently-playing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Context from './context';
import Track from '../track/track';

class CurrentlyPlaying {
context: Context | null;
currentlyPlayingType: string;
isPlaying: boolean;
item: Track | null;
progressMs: number;
timestamp: number;

constructor(json: any) {
this.context = json.context ? new Context(json.context) : null;
this.currentlyPlayingType = json.currently_playing_type;
this.isPlaying = json.is_playing;
this.item = json.item ? new Track(json.item) : null;
this.progressMs = json.progress_ms;
this.timestamp = json.timestamp;
}
}

export default CurrentlyPlaying;
4 changes: 2 additions & 2 deletions src/lib/models/player/play-history.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Context from './context';
import Track from '../track/track';

class PlaylistTrack {
class PlayHistory {
track: Track;

playedAt: string; // Timestamp
Expand All @@ -15,4 +15,4 @@ class PlaylistTrack {
}
}

export default PlaylistTrack;
export default PlayHistory;
145 changes: 145 additions & 0 deletions src/lib/models/track/audio-analysis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
class AudioAnalysis {
bars: TimeInterval[];
beats: TimeInterval[];
sections: Section[];
segments: Segment[];
tatums: TimeInterval[];
track: TrackAnalysisData;

constructor(json: any) {
this.bars = json.bars.map((bar: any) => new TimeInterval(bar));
this.beats = json.beats.map((beat: any) => new TimeInterval(beat));
this.sections = json.sections.map(
(section: any) => new Section(section)
);
this.segments = json.segments.map(
(segment: any) => new Segment(segment)
);
this.tatums = json.tatums.map((tatum: any) => new TimeInterval(tatum));
this.track = new TrackAnalysisData(json.track);
}
}

class TimeInterval {
start: number;
duration: number;
confidence: number;

constructor(json: any) {
this.start = json.start;
this.duration = json.duration;
this.confidence = json.confidence;
}
}

class Section {
start: number;
duration: number;
confidence: number;
loudness: number;
tempo: number;
tempoConfidence: number;
key: number;
keyConfidence: number;
mode: number;
modeConfidence: number;
timeSignature: number;
timeSignatureConfidence: number;

constructor(json: any) {
this.start = json.start;
this.duration = json.duration;
this.confidence = json.confidence;
this.loudness = json.loudness;
this.tempo = json.tempo;
this.tempoConfidence = json.tempo_confidence;
this.key = json.key;
this.keyConfidence = json.key_confidence;
this.mode = json.mode;
this.modeConfidence = json.mode_confidence;
this.timeSignature = json.time_signature;
this.timeSignatureConfidence = json.time_signature_confidence;
}
}

class Segment {
start: number;
duration: number;
confidence: number;
loudnessStart: number;
loudnessMaxTime: number;
loudnessMax: number;
loudnessEnd: number;
pitches: number[];
timbre: number[];

constructor(json: any) {
this.start = json.start;
this.duration = json.duration;
this.confidence = json.confidence;
this.loudnessStart = json.loudness_start;
this.loudnessMaxTime = json.loudness_max_time;
this.loudnessMax = json.loudness_max;
this.loudnessEnd = json.loudness_end;
this.pitches = json.pitches;
this.timbre = json.timbre;
}
}

class TrackAnalysisData {
duration: number;
sampleMd5: string;
offsetSeconds: number;
windowSeconds: number;
analysisSampleRate: number;
analysisChannels: number;
endOfFadeIn: number;
startOfFadeOut: number;
loudness: number;
tempo: number;
tempoConfidence: number;
timeSignature: number;
timeSignatureConfidence: number;
key: number;
keyConfidence: number;
mode: number;
modeConfidence: number;
codeString: string;
codeVersion: number;
echoprintString: string;
echoprintVersion: number;
synchString: string;
synchVersion: number;
rhythmString: string;
rhythmVersion: number;

constructor(json: any) {
this.duration = json.duration;
this.sampleMd5 = json.sample_md5;
this.offsetSeconds = json.offset_seconds;
this.windowSeconds = json.window_seconds;
this.analysisSampleRate = json.analysis_sample_rate;
this.analysisChannels = json.analysis_channels;
this.endOfFadeIn = json.end_of_fade_in;
this.startOfFadeOut = json.start_of_fade_out;
this.loudness = json.loudness;
this.tempo = json.tempo;
this.tempoConfidence = json.tempo_confidence;
this.timeSignature = json.time_signature;
this.timeSignatureConfidence = json.time_signature_confidence;
this.key = json.key;
this.keyConfidence = json.key_confidence;
this.mode = json.mode;
this.modeConfidence = json.mode_confidence;
this.codeString = json.codestring;
this.codeVersion = json.code_version;
this.echoprintString = json.echoprintstring;
this.echoprintVersion = json.echoprint_version;
this.synchString = json.synchstring;
this.synchVersion = json.synch_version;
this.rhythmString = json.rhythmstring;
this.rhythmVersion = json.rhythm_version;
}
}

export default AudioAnalysis;
12 changes: 8 additions & 4 deletions src/lib/models/user/user-public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ class PublicUser {

externalUrls: any;

followers: Followers;
followers?: Followers;

href: string;

id: string;

images: Image[];
images?: Image[];

type: 'user';

Expand All @@ -21,10 +21,14 @@ class PublicUser {
constructor(json: any) {
this.displayName = json.display_name;
this.externalUrls = json.external_urls;
this.followers = new Followers(json.followers);
if (json.followers) this.followers = new Followers(json.followers);
this.href = json.href;
this.id = json.id;
this.images = json.images.map((imageJson: any) => new Image(imageJson));
if (json.images) {
this.images = json.images.map(
(imageJson: any) => new Image(imageJson)
);
}
this.type = json.type;
this.uri = json.uri;
}
Expand Down

0 comments on commit a89976b

Please sign in to comment.