diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b130c6..68b9fe1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/example/applied_methods_example/track_methods_example.dart b/example/applied_methods_example/track_methods_example.dart new file mode 100644 index 0000000..0cca5cc --- /dev/null +++ b/example/applied_methods_example/track_methods_example.dart @@ -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('####################################################################'); +} diff --git a/example/methods_example/scrobble_example.dart b/example/methods_example/scrobble_example.dart index b6bd022..6aac09a 100644 --- a/example/methods_example/scrobble_example.dart +++ b/example/methods_example/scrobble_example.dart @@ -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()); @@ -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}'); @@ -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}'); diff --git a/example/methods_example/track_methods_example.dart b/example/methods_example/track_methods_example.dart index 33215e4..95bbbd3 100644 --- a/example/methods_example/track_methods_example.dart +++ b/example/methods_example/track_methods_example.dart @@ -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()); diff --git a/lib/scrobblenaut.dart b/lib/scrobblenaut.dart index 46dfa64..913f7e9 100644 --- a/lib/scrobblenaut.dart +++ b/lib/scrobblenaut.dart @@ -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'; diff --git a/lib/src/extensions/track_extension.dart b/lib/src/extensions/track_extension.dart index 9f87f3c..f9f3654 100644 --- a/lib/src/extensions/track_extension.dart +++ b/lib/src/extensions/track_extension.dart @@ -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 addTags({@required List tags}) async { + return await _trackMethods.addTags( + track: name, + artist: artist.name, + tags: tags, + ); + } + + /// [TrackMethods.getCorrection] + Future> getCorrection() async { + return await _trackMethods.getCorrection( + track: name, + artist: artist.name, + ); + } + + /// [TrackMethods.getInfo] + Future 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> 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> 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> getTopTags({ + bool autoCorrect = false, + }) async { + return await _trackMethods.getTopTags( + track: name, + artist: artist.name, + mbid: mbid, + autoCorrect: autoCorrect, + ); + } + + /// [TrackMethods.love] + Future love() async { + return await _trackMethods.love( + track: name, + artist: artist.name, + ); + } + + /// [TrackMethods.removeTag] + Future removeTag({ + @required String tag, + }) async { + return await _trackMethods.removeTag( + track: name, + artist: artist.name, + tag: tag, + ); + } + + /// [TrackMethods.scrobble] + Future 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 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 unLove() async { + return await _trackMethods.unLove( + track: name, + artist: artist.name, + ); + } + + /// [TrackMethods.updateNowPlaying] + Future 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, + ); + } } diff --git a/lib/src/methods/track_methods.dart b/lib/src/methods/track_methods.dart index a69262d..d95bc49 100644 --- a/lib/src/methods/track_methods.dart +++ b/lib/src/methods/track_methods.dart @@ -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. @@ -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 scrobbleOnce({ + Future scrobble({ @required String track, String album, @required String artist, @@ -365,8 +366,8 @@ class TrackMethods { return ScrobbleResponse.parse(response); } - /// See [TrackMethods.scrobbleOnce]. - Future scrobbleOnceFromObject( + /// See [TrackMethods.scrobble]. + Future scrobbleFromObject( {@required Scrobble scrobble}) async { if (!_api.isAuth) { return Future.error(ScrobblenautException( @@ -400,8 +401,8 @@ class TrackMethods { return ScrobbleResponse.parse(response); } - /// See [TrackMethods.scrobbleOnce] and [Scrobble] for more information. - Future scrobble( + /// See [TrackMethods.scrobble] and [Scrobble] for more information. + Future multiScrobble( {@required List scrobbleList}) async { // TODO: make a queue for scrobbleList longer than 50? diff --git a/pubspec.yaml b/pubspec.yaml index 701e832..58dd1d7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: scrobblenaut description: A deadly simple LastFM API Wrapper for Dart. So deadly simple that it's gonna hit the mark. -version: 1.0.4 +version: 1.0.5 homepage: https://github.com/Nebulino/Scrobblenaut issue_tracker: https://github.com/Nebulino/Scrobblenaut/issues