scrobble method

Future<ScrobbleResponse> scrobble({
  1. required String track,
  2. String? album,
  3. required String artist,
  4. int? trackNumber,
  5. Duration? duration,
  6. DateTime? timestamp,
  7. String? context,
  8. String? streamId,
  9. bool chosenByUser = false,
  10. String? mbid,
  11. String? albumArtist,
})

Used to add a track-play to a user's profile. Scrobble a track, or a batch of tracks. Tracks are passed to the service using array notation for each of the below params, up to a maximum of 50 scrobbles per batch 0<=i<=49. If you are only sending a single scrobble the array notation may be omitted.

Note: Extra care should be taken while calculating the signature when using array notation as the parameter names MUST be sorted according to the ASCII table (i.e., artist10 comes before artist1).

It is important to not use the corrections returned by the now playing service as input for the scrobble request, unless they have been explicitly approved by the user. Parameter names are case sensitive.

Implementation

Future<ScrobbleResponse> scrobble({
  required String track,
  String? album,
  required String artist,
  int? trackNumber,
  Duration? duration,
  DateTime? timestamp,
  String? context,
  String? streamId,
  bool chosenByUser = false,
  String? mbid,
  String? albumArtist,
}) async {
  if (!_api.isAuth) {
    return Future.error(
      ScrobblenautException(
        description: "You can't use this method unless you Authenticate.",
      ),
    );
  }

  timestamp ??= DateTime.now();

  final parameters = {
    'track': track,
    'album': album,
    'artist': artist,
    'albumArtist': albumArtist,
    'trackNumber': trackNumber,
    'duration': duration?.inSeconds,
    'timestamp': LastFMValueNormalizer.timestampToSecondsSinceEpoch(
      timestamp,
    ),
    'context': context,
    'streamId': streamId,
    'chosenByUser': (chosenByUser ? 1 : 0),
    'mbid': mbid,
  };

  final request = Request(
    api: _api,
    method: 'track.scrobble',
    parameters: parameters,
  )..signRequest();

  final response = (await request.send(mode: RequestMode.POST));

  return ScrobbleResponse.parse(response);
}