Skip to content

Commit

Permalink
Merge pull request #6 from Nebulino/track_methods
Browse files Browse the repository at this point in the history
Huge update. Seventh Pre-Release. One more to go...
  • Loading branch information
Nebulino authored Apr 14, 2020
2 parents 1e43b59 + 9190a33 commit 84f1e95
Show file tree
Hide file tree
Showing 25 changed files with 1,467 additions and 51 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
## 0.1.6
### Huge update
#### Seventh Pre-Release. One more to go...

- Implemented Track Methods.
- Fixed Artist toJson() missing method.
- Updated the changelog.
- Created NowPlayed Track helper.
- Created NowPlaying Object.
- Created Scrobble Response (renamed from Scrobble Response Helper).
- Created Scrobbled Track helper.
- Created helper library.
- Created Track Result Object.
- Added track example.
- Added scrobble example.
- Updated the README.

## 0.1.5
#### Sixth Pre-Release. Still a long way...

Expand Down
3 changes: 2 additions & 1 deletion example/artist_methods_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ void main() async {
));

print(
'Artist Info Name: ${artistGetInfo.name} | Artist URL: ${artistGetInfo.url}');
'Artist Info Name: ${artistGetInfo.name} | Artist URL: ${artistGetInfo
.url}');

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

Expand Down
61 changes: 61 additions & 0 deletions example/scrobble_example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import 'package:scrobblenaut/lastfm.dart';
/**
* Scrobblenaut - A deadly simple Last.FM API Wrapper for Dart.
* Copyright (c) 2020 Nebulino
*/

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

import 'api_values.dart';

// Just an example of use.
void main() async {
final lastFMAuth = await LastFM.authenticate(
apiKey: APIValues.API,
apiSecret: APIValues.secret,
username: APIValues.username,
password: APIValues.password,
sessionKey: APIValues.sessionKey,
);

// final lastFMnoAuth = await LastFM.noAuth(apiKey: APIValues.API);
// When used a LastFM in noAuth mode, it raises an exception.

final scrobblenaut = Scrobblenaut(lastFM: lastFMAuth);

print('Single scrobble.');

final response = await scrobblenaut.track.scrobbleOnce(
track: 'Beautiful Moonlight',
artist: 'QU4RTZ',
timestamp: DateTime.now());
// YAY. IT WORKS!

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

print('Another scrobble method.');

final scrobble = Scrobble(track: 'Sunlight', artist: 'PLEEG');

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

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

print('Multiple scrobble.');
final scrobble2 = Scrobble(track: 'Missing', artist: 'HoneyComeBear');

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

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

// final response = await scrobblenaut.track.scrobble(scrobbleList);
}
5 changes: 5 additions & 0 deletions example/tag_methods_example.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* Scrobblenaut - A deadly simple Last.FM API Wrapper for Dart.
* Copyright (c) 2020 Nebulino
*/

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

Expand Down
137 changes: 137 additions & 0 deletions example/track_methods_example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/**
* 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);

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

// track.addTags
print('Result of addTag request: ' +
(await scrobblenaut.track
.addTags(track: 'Flay', artist: 'PLEEG', tags: ['anime']))
.toString());

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

// track.getCorrection
(await scrobblenaut.track.getCorrection(track: 'TOMOYO', artist: 'Zekk'))
?.forEach((Track track) {
print('Track Correction Name: ${track.name} |'
' Track Correction URL: ${track.url}');
});

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

// track.getInfo
final trackGetInfo = (await scrobblenaut.track.getInfo(
track: 'Tomoyo',
artist: 'Zekk',
username: 'nebulino',
autoCorrect: true,
));

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

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

// track.getSimilar
(await scrobblenaut.track.getSimilar(track: 'Tomoyo', artist: 'Zekk'))
?.forEach((Track track) {
print('Similar Track Name: ${track.name}');
});

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

// track.getTags
(await scrobblenaut.track
.getTags(track: 'Hells Bells', artist: 'AC/DC', user: 'nebulino'))
?.forEach((Tag tag) {
print('Tag name: ${tag.name} | Tag url: ${tag.url}');
});

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

// track.getTopTags
(await scrobblenaut.track.getTopTags(track: 'Hells Bells', artist: 'AC/DC'))
?.forEach((Tag tag) {
print('Tag name: ${tag.name} | Tag url: ${tag.url}');
});

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

// track.love
print('Result of love request: ' +
(await scrobblenaut.track.love(track: 'Sunlight', artist: 'PLEEG'))
.toString());

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

// track.removeTag
print('Result of removeTag request: ' +
(await scrobblenaut.track
.removeTag(track: 'Flay', artist: 'PLEEG', tag: 'anime'))
.toString());

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

// track.scrobble
final scrobbleResponse = await scrobblenaut.track.scrobbleOnce(
track: 'Beautiful Moonlight',
artist: 'QU4RTZ',
timestamp: DateTime.now());
// YAY. IT WORKS!

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

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

// track.search
(await scrobblenaut.track.search(track: 'Horizon', artist: 'PLEEG'))
.tracks
?.forEach((Track track) {
print('Track Name from search: ${track.name}');
});

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

// track.unlove
print('Result of unlove request: ' +
(await scrobblenaut.track.unLove(track: 'Sunlight', artist: 'PLEEG'))
.toString());

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

// track.updateNowPlaying
final nowPlayingResponse = await scrobblenaut.track
.updateNowPlaying(track: 'Beautiful Moonlight', artist: 'QU4RTZ');
// YAY. IT WORKS!

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

print('####################################################################');
}
1 change: 1 addition & 0 deletions lib/scrobblenaut.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

/// Scrobblenaut Library.
/// This is the brain of my Wrapper.
library scrobblenaut;

export 'package:scrobblenaut/src/scrobblenaut.dart';
Expand Down
11 changes: 11 additions & 0 deletions lib/scrobblenaut_helpers.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Scrobblenaut - A deadly simple Last.FM API Wrapper for Dart.
* Copyright (c) 2020 Nebulino
*/

/// Scrobblenaut Library.
/// Import this if you want to use some helpers.
library scrobblenaut_helpers;

export 'package:scrobblenaut/src/helpers/scrobble_response.dart';
export 'package:scrobblenaut/src/helpers/scrobbled_track.dart';
3 changes: 2 additions & 1 deletion lib/src/exceptions/lastfm_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class LastFMException extends DioError {
} else {
// Else is a Json...
return LastFMException(
errorCode: errorObject['error'], description: errorObject['message']);
errorCode: errorObject['error'].toString(),
description: errorObject['message']);
}
}

Expand Down
45 changes: 35 additions & 10 deletions lib/src/helpers/lastfm_value_normalizer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class LastFMValueNormalizer {
}
}

/// It transforms a bool into a LastFM 'bool' [0,1].
static int BoolToIntBool(bool booleanToTransform) =>
booleanToTransform == null ? null : booleanToTransform ? 1 : 0;

/// It transforms a supposed artist into a real [Artist] object.
/// This because sometimes LastFM returns an artist as Map,
/// sometimes as String, which is the Artist name.
Expand Down Expand Up @@ -81,7 +85,7 @@ class LastFMValueNormalizer {
} else {
throw ScrobblenautException(
description:
'The supposed duration in milliseconds is not recognized.');
'The supposed duration in milliseconds is not recognized.');
}
} else {
return null;
Expand All @@ -108,6 +112,14 @@ class LastFMValueNormalizer {
}
}

/// It transforms a Duration into Milliseconds.
static int DurationToMilliseconds(Duration duration) =>
duration == null ? null : duration.inMilliseconds;

/// It transforms a Duration into Seconds.
static int DurationToSeconds(Duration duration) =>
duration == null ? null : duration.inSeconds;

/// It transforms a LastFM number received from [Artist][streamable]
/// into a bool.
static bool isArtistStreamable(dynamic supposedBool) {
Expand Down Expand Up @@ -144,6 +156,11 @@ class LastFMValueNormalizer {

/// It transforms a LastFM supposed unixTime into a DateTime.
static DateTime DateTimeFromUnixTime(dynamic unixTime) {
// I've found a nil.
if (unixTime.toString() == 'nil') {
return null;
}

if (unixTime != null) {
if (unixTime is String) {
return DateTime.fromMillisecondsSinceEpoch(int.parse(unixTime) * 1000);
Expand All @@ -159,9 +176,10 @@ class LastFMValueNormalizer {
}

/// It transforms a LastFM supposed unixTime from a DateTime.
static int DateTimeToUnixTime(DateTime dateTime) => dateTime == null
? null
: (dateTime.millisecondsSinceEpoch / 1000).round();
static int DateTimeToUnixTime(DateTime dateTime) =>
dateTime == null
? null
: (dateTime.millisecondsSinceEpoch / 1000).round();

/// Makes a meaning-less number into a string.
static String MeaninglessNumber(dynamic supposedText) =>
Expand All @@ -175,9 +193,10 @@ class LastFMValueNormalizer {
(i) => Track.fromJson(tracks['track'][i]));

/// Tags extractor.
static List<Tag> tagsExtractor(Map<String, dynamic> tags) => tags == null
? null
: List.generate(
static List<Tag> tagsExtractor(Map<String, dynamic> tags) =>
tags == null
? null
: List.generate(
(tags['tag'] as List).length, (i) => Tag.fromJson(tags['tag'][i]));

/// Albums extractor.
Expand All @@ -196,7 +215,7 @@ class LastFMValueNormalizer {

/// SimilarArtists extractor.
static List<Artist> similarArtistsExtractor(
Map<String, dynamic> similarArtists) =>
Map<String, dynamic> similarArtists) =>
similarArtists == null
? null
: List.generate((similarArtists['artist'] as List).length,
Expand All @@ -207,13 +226,19 @@ class LastFMValueNormalizer {
final supposedLinksList = links;
if (links != null) {
if (supposedLinksList is List) {
List.generate((links['link'] as List).length,
(i) => Link.fromJson(links['link'][i]));
return List.generate((links['link'] as List).length,
(i) => Link.fromJson(links['link'][i]));
} else {
return [Link.fromJson(links['link'])];
}
} else {
return null;
}
}

/// TimeStamp normalizer for POST methods.
static int timestampToSecondsSinceEpoch(DateTime timestamp) =>
timestamp == null
? null
: (timestamp.millisecondsSinceEpoch / 1000).round();
}
Loading

0 comments on commit 84f1e95

Please sign in to comment.