getVideoServers method

Future<List> getVideoServers(
  1. String episodeId
)

Implementation

Future<List> getVideoServers(String episodeId) async {
  // get request with the anime url using the given id
  final res =
      await http.Client().get(Uri.parse('$ANIME_VIDEO_URL$episodeId'));
  if (res.statusCode == 200) {
    // get html and look for the scripts as animeflv saves the servers in one
    final body = res.body.toString();
    final soup = BeautifulSoup(body);
    final scripts = soup.findAll('script');
    var servers = [];
    // for every script found we'll look for the one with the servers
    for (var script in scripts) {
      final content = script.toString();
      if (content.contains('var videos = {')) {
        final videos = content.split('var videos = ')[1].split(';')[0];
        final data = json.decode(videos);
        if (data.containsKey('SUB')) servers.add(data['SUB']);
      }
    }
    // return a list of available servers with their data
    return servers[0];
  }
  return [];
}