parseSongRun function

JsonMap parseSongRun(
  1. JsonMap run
)

Parses song run.

Implementation

JsonMap parseSongRun(JsonMap run) {
  final text = run['text'];
  if (run.containsKey('navigationEndpoint')) {
    final item = {
      'name': text,
      'id': nav(run, NAVIGATION_BROWSE_ID, nullIfAbsent: true),
    };
    final id = item['id'] as String?;
    if (id != null &&
        (id.startsWith('MPRE') || id.contains('release_detail'))) {
      return {'type': 'album', 'data': item};
    } else {
      return {'type': 'artist', 'data': item};
    }
  } else {
    // note: YT uses non-breaking space \xa0 to separate number and magnitude
    final viewsReg = RegExp(r'^\d([^ ])* [^ ]*$');
    final durationReg = RegExp(r'^(\d+:)*\d+:\d+$');
    final yearReg = RegExp(r'^\d{4}$');

    if (viewsReg.hasMatch(text as String)) {
      return {'type': 'views', 'data': text.split(' ')[0]};
    } else if (durationReg.hasMatch(text)) {
      return {'type': 'duration', 'data': text};
    } else if (yearReg.hasMatch(text)) {
      return {'type': 'year', 'data': text};
    } else if (!API_RESULT_TYPES.contains(text.toLowerCase())) {
      return {
        'type': 'artist',
        'data': {'name': text, 'id': null},
      };
    } else {
      return {'type': 'other', 'data': null};
    }
  }
}