Skip to content

Commit

Permalink
Merge pull request #19 from Nebulino/track_extension
Browse files Browse the repository at this point in the history
Add capability of applying TrackMethods in Track.
  • Loading branch information
Nebulino authored Apr 16, 2020
2 parents 5685028 + fdf8e2e commit d3a1903
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 11 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## 1.0.5
#### Add capability of applying TrackMethods in Track.

- Created new track example.
- Changed *lang* in Methods into *language*
- Updated **CHANGELOG.md**.
- TrackMethods.scrobbleOnce() into TrackMethods.scrobble()
- TrackMethods.scrobbleOnceFromObject() into TrackMethods.scrobbleFromObject()
- TrackMethods.scrobble() into TrackMethods.multiScrobble()

## 1.0.4
#### Add capability of applying TagMethods in Tag.

Expand Down
124 changes: 124 additions & 0 deletions example/applied_methods_example/track_methods_example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* Scrobblenaut - A deadly simple Last.FM API Wrapper for Dart.
* Copyright (c) 2020 Nebulino
*/

import 'package:scrobblenaut/lastfm.dart';
import 'package:scrobblenaut/scrobblenaut.dart';
import 'package:scrobblenaut/scrobblenaut_helpers.dart';

import '../api_values.dart';

// Just an example of use.
void main() async {
print('####################################################################');

final lastFMAuth = await LastFM.authenticate(
apiKey: APIValues.API,
apiSecret: APIValues.secret,
username: APIValues.username,
password: APIValues.password,
sessionKey: APIValues.sessionKey,
);

final scrobblenaut = Scrobblenaut(lastFM: lastFMAuth);

final trackInstance = (await scrobblenaut.track.getInfo(
track: 'Tomoyo',
artist: 'Zekk',
autoCorrect: true,
));

print('##########################track.addTags#############################');

// track.addTags
print('Result of addTag request: ' +
(await trackInstance.addTags(tags: ['anime'])).toString());

print('######################track.getCorrection###########################');

// track.getCorrection
(await trackInstance.getCorrection())?.forEach((Track track) {
print('Track Correction Name: ${track.name} |'
' Track Correction URL: ${track.url}');
});

print('#########################track.getInfo##############################');

// track.getInfo
final trackGetInfo = (await trackInstance.getInfo(
username: 'nebulino',
autoCorrect: true,
));

print('Track Info Name: ${trackGetInfo.name} '
'| Track URL: ${trackGetInfo.url} '
'| Track Duration: ${trackGetInfo.duration}');

print('#########################track.getSimilar###########################');

// track.getSimilar
(await trackInstance.getSimilar())?.forEach((Track track) {
print('Similar Track Name: ${track.name}');
});

print('###########################track.getTags############################');

// track.getTags
(await trackInstance.getTags(user: 'nebulino'))?.forEach((Tag tag) {
print('Tag name: ${tag.name} | Tag url: ${tag.url}');
});

print('#########################track.getTopTags###########################');

// track.getTopTags
(await trackInstance.getTopTags())?.forEach((Tag tag) {
print('Tag name: ${tag.name} | Tag url: ${tag.url}');
});

print('#############################track.love#############################');

// track.love
print('Result of love request: ' + (await trackInstance.love()).toString());

print('########################track.removeTag#############################');

// track.removeTag
print('Result of removeTag request: ' +
(await trackInstance.removeTag(tag: 'anime')).toString());

print('#########################track.scrobble#############################');

// track.scrobble
final scrobbleResponse =
await trackInstance.scrobble(timestamp: DateTime.now());
// YAY. IT WORKS!

scrobbleResponse.scrobbleResponses?.forEach((ScrobbledTrack scrobbledTrack) {
print('Scrobbled Title: ${scrobbledTrack.track}');
});

print('###########################track.search#############################');

// track.search
(await trackInstance.search()).tracks?.forEach((Track track) {
print('Track Name from search: ${track.name}');
});

print('##########################track.unlove##############################');

// track.unlove
print(
'Result of unlove request: ' + (await trackInstance.unLove()).toString());

print('#####################track.updateNowPlaying#########################');

// track.updateNowPlaying
final nowPlayingResponse = await trackInstance.updateNowPlaying();
// YAY. IT WORKS!

print('Result of updateNowPlaying request: ${nowPlayingResponse.status} | '
'${nowPlayingResponse.track}');

print('####################################################################');
}
6 changes: 3 additions & 3 deletions example/methods_example/scrobble_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void main() async {

print('Single scrobble.');

final response = await scrobblenaut.track.scrobbleOnce(
final response = await scrobblenaut.track.scrobble(
track: 'Beautiful Moonlight',
artist: 'QU4RTZ',
timestamp: DateTime.now());
Expand All @@ -41,7 +41,7 @@ void main() async {
final scrobble = Scrobble(track: 'Sunlight', artist: 'PLEEG');

final anotherResponse =
await scrobblenaut.track.scrobbleOnceFromObject(scrobble: scrobble);
await scrobblenaut.track.scrobbleFromObject(scrobble: scrobble);

anotherResponse.scrobbleResponses?.forEach((ScrobbledTrack scrobbledTrack) {
print('Scrobbled Title: ${scrobbledTrack.track}');
Expand All @@ -51,7 +51,7 @@ void main() async {
final scrobble2 = Scrobble(track: 'Missing', artist: 'HoneyComeBear');

final lastResponse =
await scrobblenaut.track.scrobble(scrobbleList: [scrobble, scrobble2]);
await scrobblenaut.track.multiScrobble(scrobbleList: [scrobble, scrobble2]);

lastResponse.scrobbleResponses?.forEach((ScrobbledTrack scrobbledTrack) {
print('Scrobbled Title: ${scrobbledTrack.track}');
Expand Down
2 changes: 1 addition & 1 deletion example/methods_example/track_methods_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void main() async {
print('#########################track.scrobble#############################');

// track.scrobble
final scrobbleResponse = await scrobblenaut.track.scrobbleOnce(
final scrobbleResponse = await scrobblenaut.track.scrobble(
track: 'Beautiful Moonlight',
artist: 'QU4RTZ',
timestamp: DateTime.now());
Expand Down
1 change: 1 addition & 0 deletions lib/scrobblenaut.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export 'package:scrobblenaut/src/scrobblenaut.dart';
export 'package:scrobblenaut/src/extensions/album_extension.dart';
export 'package:scrobblenaut/src/extensions/artist_extension.dart';
export 'package:scrobblenaut/src/extensions/tag_extension.dart';
export 'package:scrobblenaut/src/extensions/track_extension.dart';
166 changes: 166 additions & 0 deletions lib/src/extensions/track_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,174 @@ import 'package:meta/meta.dart';
import 'package:scrobblenaut/lastfm.dart';
import 'package:scrobblenaut/lastfm_methods.dart';
import 'package:scrobblenaut/scrobblenaut.dart';
import 'package:scrobblenaut/scrobblenaut_helpers.dart';
import 'package:scrobblenaut/src/helpers/now_played_track.dart';

/// Give the ability of applying [TrackMethods] on [Track].
extension TrackExtension on Track {
TrackMethods get _trackMethods => Scrobblenaut.instance.track;

/// [TrackMethods.addTags]
Future<bool> addTags({@required List<String> tags}) async {
return await _trackMethods.addTags(
track: name,
artist: artist.name,
tags: tags,
);
}

/// [TrackMethods.getCorrection]
Future<List<Track>> getCorrection() async {
return await _trackMethods.getCorrection(
track: name,
artist: artist.name,
);
}

/// [TrackMethods.getInfo]
Future<Track> getInfo({
String username,
bool autoCorrect = false,
}) async {
return await _trackMethods.getInfo(
track: name,
artist: artist.name,
mbid: mbid,
username: username,
autoCorrect: autoCorrect,
);
}

/// [TrackMethods.getSimilar]
Future<List<Track>> getSimilar({
int limit,
bool autoCorrect = false,
}) async {
return await _trackMethods.getSimilar(
track: name,
artist: artist.name,
mbid: mbid,
limit: limit,
autoCorrect: autoCorrect,
);
}

/// [TrackMethods.getTags]
Future<List<Tag>> getTags({
String user,
bool autoCorrect = false,
}) async {
return await _trackMethods.getTags(
track: name,
artist: artist.name,
mbid: mbid,
user: user,
autoCorrect: autoCorrect,
);
}

/// [TrackMethods.getTopTags]
Future<List<Tag>> getTopTags({
bool autoCorrect = false,
}) async {
return await _trackMethods.getTopTags(
track: name,
artist: artist.name,
mbid: mbid,
autoCorrect: autoCorrect,
);
}

/// [TrackMethods.love]
Future<bool> love() async {
return await _trackMethods.love(
track: name,
artist: artist.name,
);
}

/// [TrackMethods.removeTag]
Future<bool> removeTag({
@required String tag,
}) async {
return await _trackMethods.removeTag(
track: name,
artist: artist.name,
tag: tag,
);
}

/// [TrackMethods.scrobble]
Future<ScrobbleResponse> scrobble({
String album,
int trackNumber,
Duration duration,
DateTime timestamp,
String context,
String streamId,
bool chosenByUser = false,
String mbid,
}) async {
timestamp ??= DateTime.now();

return await _trackMethods.scrobble(
track: name,
album: album ?? this.album.name,
artist: artist.name,
trackNumber: trackNumber,
duration: duration ?? this.duration,
timestamp: timestamp,
context: context,
streamId: streamId,
chosenByUser: chosenByUser,
mbid: mbid ?? this.mbid,
);
}

/// [TrackMethods.search]
Future<TrackSearchResults> search({
String artist,
int page = 1,
int limit = 30,
}) async {
return await _trackMethods.search(
track: name,
artist: artist ?? this.artist.name,
page: page,
limit: limit,
);
}

/// [TrackMethods.unLove]
Future<bool> unLove() async {
return await _trackMethods.unLove(
track: name,
artist: artist.name,
);
}

/// [TrackMethods.updateNowPlaying]
Future<NowPlayedTrack> updateNowPlaying({
String album,
int trackNumber,
Duration duration,
DateTime timestamp,
String context,
String streamId,
bool chosenByUser = false,
String mbid,
}) async {
return await _trackMethods.updateNowPlaying(
track: name,
album: album ?? this.album.name,
artist: artist.name,
trackNumber: trackNumber,
duration: duration ?? this.duration,
timestamp: timestamp,
context: context,
streamId: streamId,
chosenByUser: chosenByUser,
mbid: mbid ?? this.mbid,
);
}
}
13 changes: 7 additions & 6 deletions lib/src/methods/track_methods.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ class TrackMethods {

return tags['tag'] == null
? null
: List.generate((tags as List).length, (i) => Tag.fromJson(tags[i]));
: List.generate(
(tags['tag'] as List).length, (i) => Tag.fromJson(tags['tag'][i]));
}

/// Get the top tags for this track on Last.fm, ordered by tag count.
Expand Down Expand Up @@ -323,7 +324,7 @@ class TrackMethods {
/// the now playing service as input for the scrobble request,
/// unless they have been explicitly approved by the user.
/// Parameter names are case sensitive.
Future<ScrobbleResponse> scrobbleOnce({
Future<ScrobbleResponse> scrobble({
@required String track,
String album,
@required String artist,
Expand Down Expand Up @@ -365,8 +366,8 @@ class TrackMethods {
return ScrobbleResponse.parse(response);
}

/// See [TrackMethods.scrobbleOnce].
Future<ScrobbleResponse> scrobbleOnceFromObject(
/// See [TrackMethods.scrobble].
Future<ScrobbleResponse> scrobbleFromObject(
{@required Scrobble scrobble}) async {
if (!_api.isAuth) {
return Future.error(ScrobblenautException(
Expand Down Expand Up @@ -400,8 +401,8 @@ class TrackMethods {
return ScrobbleResponse.parse(response);
}

/// See [TrackMethods.scrobbleOnce] and [Scrobble] for more information.
Future<ScrobbleResponse> scrobble(
/// See [TrackMethods.scrobble] and [Scrobble] for more information.
Future<ScrobbleResponse> multiScrobble(
{@required List<Scrobble> scrobbleList}) async {
// TODO: make a queue for scrobbleList longer than 50?

Expand Down
Loading

0 comments on commit d3a1903

Please sign in to comment.