Hexer10 / youtube_explode_dart

Dart library to interact with many Youtube APIs
https://pub.dev/packages/youtube_explode_dart
BSD 3-Clause "New" or "Revised" License
322 stars 141 forks source link

[BUG] FormatException when trying to parse duration of SHORTS videos #199

Closed 0xn33t closed 1 year ago

0xn33t commented 2 years ago

Describe the bug FormatException is thrown when trying to get duration of SHORTS videos. This happens when a channel contains a short videos. The method to convert string to duration failed to parse int.

To Reproduce

  /// Format: HH:MM:SS
  Duration? toDuration() {
    if (/*string == null ||*/ trim().isEmpty) {
      return null;
    }

    // Normal Videos will have a HH:MM:SS as string
    // SHORTS video will have a SHORTS string 

    var parts = split(':');
    assert(parts.length <= 3);

    // SHORTS VIDEOS => parts = [SHORTS]

    if (parts.length == 1) {
      // Here where exception is raised. It tries to parse string "SHORTS" to int
      return Duration(seconds: int.parse(parts.first));
    }
    if (parts.length == 2) {
      return Duration(
          minutes: int.parse(parts[0]), seconds: int.parse(parts[1]));
    }
    if (parts.length == 3) {
      return Duration(
          hours: int.parse(parts[0]),
          minutes: int.parse(parts[1]),
          seconds: int.parse(parts[2]));
    }
    // Shouldn't reach here.
    throw Error();
  }
Hexer10 commented 2 years ago

Hi, could you send a video id which causes this issue? I've just tried the follwing code and got the duration correctly:

  var short = await yt.videos.get('e22Nj4aS0J8');
  print(short.duration);