getVideos method

Future<Map<String, dynamic>> getVideos(
  1. String videoId
)

Implementation

Future<Map<String, dynamic>> getVideos(String videoId) async {
  try {
    final response = await post(
      Uri.parse('https://submagic-free-tools.fly.dev/api/youtube-info'),
      body: {
        "url": "https://www.youtube.com/watch?v=$videoId",
      },
    );
    if (response.statusCode == 200) {
      final body = jsonDecode(response.body);
      final allVideos = List<Map<String, dynamic>>.from(body['formats'])
          .where((element) => element['type'] == "video_only")
          .toList();
      List<Map<String, dynamic>> videos = [];
      for (var element in allVideos) {
        final index = videos.indexWhere(
          (vid) => vid['height'] == element['height'],
        );
        if (index == -1) {
          videos.add(element);
        }
      }
      final audios = List<Map<String, dynamic>>.from(body['formats'])
          .where((element) => element['type'] == "audio")
          .toList();
      return {
        "videos": videos,
        "audios": audios,
      };
    }
    throw Exception("Data not found");
  } catch (e) {
    return {};
  }
}