Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved top endpoints #157

Merged
merged 5 commits into from
Apr 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 37 additions & 19 deletions lib/src/endpoints/me.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,24 @@ class Me extends _MeEndpointBase {
Future<PlaybackState> player([String? market]) async =>
_player.playbackState(market);

/// Get the current user's top tracks.
Future<Iterable<Track>> topTracks() async {
final jsonString = await _api._get('$_path/top/tracks');
final map = json.decode(jsonString);

final items = map['items'] as Iterable<dynamic>;
return items.map((item) => Track.fromJson(item));
}

/// Get the current user's top artists.
Future<Iterable<Artist>> topArtists() async {
final jsonString = await _api._get('$_path/top/artists');
final map = json.decode(jsonString);

final items = map['items'] as Iterable<dynamic>;
return items.map((item) => Artist.fromJson(item));
}
/// Get the current user's top tracks, spanning over a [timeRange].
/// The [timeRange]'s default is [TimeRange.mediumTerm].
Pages<Track> topTracks({TimeRange timeRange = TimeRange.mediumTerm}) =>
_top(_TopItemsType.tracks, (item) => Track.fromJson(item), timeRange);

/// Get the current user's top artists, spanning over a [timeRange].
/// The [timeRange]'s default is [TimeRange.mediumTerm].
Pages<Artist> topArtists({TimeRange timeRange = TimeRange.mediumTerm}) =>
_top(_TopItemsType.artists, (item) => Artist.fromJson(item), timeRange);

Pages<T> _top<T>(
_TopItemsType type, T Function(dynamic) parser, TimeRange range) =>
_getPages(
'$_path/top/${type.name}?' +
_buildQuery({
'time_range': range._key,
}),
parser);

/// Get information about a user’s available devices.
@Deprecated('Use [spotify.player.devices()]')
Expand Down Expand Up @@ -207,8 +208,8 @@ class Me extends _MeEndpointBase {
/// Returns the current user's saved episodes. Requires the `user-library-read`
/// scope.
Pages<EpisodeFull> savedEpisodes() {
return _getPages('$_path/episodes',
(json) => EpisodeFull.fromJson(json['episode']));
return _getPages(
'$_path/episodes', (json) => EpisodeFull.fromJson(json['episode']));
}

/// Saves episodes for the current user. Requires the `user-library-modify`
Expand Down Expand Up @@ -250,3 +251,20 @@ class FollowingType {
static const artist = FollowingType('artist');
static const user = FollowingType('user');
}

class TimeRange {
final String _key;

const TimeRange(this._key);

/// Consists of several years of data and including all new data as it becomes available
static const longTerm = TimeRange('long_term');

/// Consists of approximately last 6 months
static const mediumTerm = TimeRange('medium_term');

/// Consists of approximately last 4 weeks
static const shortTerm = TimeRange('short_term');
}

enum _TopItemsType { artists, tracks }
4 changes: 2 additions & 2 deletions lib/src/models/_models.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions lib/src/models/album.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,13 @@ class AlbumSimple extends Object {
: items.map((trackJson) => TrackSimple.fromJson(trackJson));
}

static AlbumType? _convertForAlbumType(String? json) {
return AlbumType.values.asNameMap()[json?.toLowerCase()];
}

/// The type of the album: one of "album", "single", or "compilation".
@JsonKey(name: 'album_type')
String? albumType;
@JsonKey(name: 'album_type', fromJson: _convertForAlbumType)
AlbumType? albumType;

/// The artists of the album. Each artist object includes a link in href to
/// more detailed information about the artist.
Expand Down Expand Up @@ -105,3 +109,5 @@ class AlbumSimple extends Object {
}

enum DatePrecision { day, month, year }

enum AlbumType { album, singe, compilation }
hayribakici marked this conversation as resolved.
Show resolved Hide resolved
88 changes: 88 additions & 0 deletions test/data/v1/me/top/artists.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"items": [
{
"external_urls": {
"spotify": "https://open.spotify.com/artist/5NXHXK6hOCotCF8lvGM1I0"
},
"followers": {
"href": null,
"total": 0
},
"genres": [
"alternative metal",
"art rock",
"progressive metal",
"progressive rock",
"symphonic rock"
],
"href": "https://api.spotify.com/v1/artists/5NXHXK6hOCotCF8lvGM1I0",
"id": "5NXHXK6hOCotCF8lvGM1I0",
"images": [
{
"height": 640,
"url": "https://i.scdn.co/image/ab6761610000e5ebc4f72407be5d96db73982400",
"width": 640
},
{
"height": 320,
"url": "https://i.scdn.co/image/ab67616100005174c4f72407be5d96db73982400",
"width": 320
},
{
"height": 160,
"url": "https://i.scdn.co/image/ab6761610000f178c4f72407be5d96db73982400",
"width": 160
}
],
"name": "Porcupine Tree",
"popularity": 56,
"type": "artist",
"uri": "spotify:artist:5NXHXK6hOCotCF8lvGM1I0"
},
{
"external_urls": {
"spotify": "https://open.spotify.com/artist/4X42BfuhWCAZ2swiVze9O0"
},
"followers": {
"href": null,
"total": 0
},
"genres": [
"art rock",
"neo-progressive",
"progressive metal",
"progressive rock",
"symphonic rock"
],
"href": "https://api.spotify.com/v1/artists/4X42BfuhWCAZ2swiVze9O0",
"id": "4X42BfuhWCAZ2swiVze9O0",
"images": [
{
"height": 640,
"url": "https://i.scdn.co/image/ab6761610000e5ebb0655526ab51d102711937d7",
"width": 640
},
{
"height": 320,
"url": "https://i.scdn.co/image/ab67616100005174b0655526ab51d102711937d7",
"width": 320
},
{
"height": 160,
"url": "https://i.scdn.co/image/ab6761610000f178b0655526ab51d102711937d7",
"width": 160
}
],
"name": "Steven Wilson",
"popularity": 49,
"type": "artist",
"uri": "spotify:artist:4X42BfuhWCAZ2swiVze9O0"
}
],
"total": 50,
"limit": 2,
"offset": 0,
"href": "https://api.spotify.com/v1/me/top/artists?limit=2&offset=0",
"next": "https://api.spotify.com/v1/me/top/artists?limit=2&offset=2",
"previous": null
}
Loading