downloadLinksByEpisodeId method

Future<List> downloadLinksByEpisodeId(
  1. String id
)

Implementation

Future<List> downloadLinksByEpisodeId(String id) async {
  // get request using the provided id
  final res = await http.Client().get(Uri.parse('$ANIME_VIDEO_URL$id'));
  if (res.statusCode == 200) {
    // parse html to string and look for the table with the downloads info
    final body = res.body.toString();
    final soup = BeautifulSoup(body);
    final table = soup.find('table', attrs: {'class': 'RTbl'});

    try {
      // extract the links and save them into ret
      final rows = parseTable(table);
      var ret = [];

      for (var row in rows) {
        if (row['FORMATO'].string == 'SUB') {
          ret.add({
            'server': row['SERVIDOR'].string,
            'url': row['DESCARGAR'].a['href'].toString().replaceAllMapped(
                RegExp(
                    r'^http[s]?://ouo.io/[A-Za-z0-9]+/[A-Za-z0-9]+\?[A-Za-z0-9]+='),
                (match) => '"${match.group}"')
          });
        }
      }
      // for zippyshare we can get a direct download link so we create it and replace it
      for (var server in ret) {
        if (server['server'] == 'Zippyshare') {
          final resZS = await http.Client().get(Uri.parse(server['url']));
          if (resZS.statusCode == 200) {
            final body = resZS.body.toString();
            final soup = BeautifulSoup(body);

            final scripts = soup.findAll('script');
            for (var script in scripts) {
              final content = script.toString();
              if (content.contains('var n = ')) {
                final n = int.parse(content
                        .split('\n')[1]
                        .trim()
                        .split('var n = ')[1]
                        .split('%')[0]) %
                    2;
                final b = int.parse(content
                        .split('\n')[2]
                        .trim()
                        .split('var b = ')[1]
                        .split('%')[0]) %
                    3;
                final z = int.parse(content
                    .split('\n')[3]
                    .trim()
                    .split('var z = ')[1]
                    .split(';')[0]);
                final title = content.split('\n')[4].trim().split('"')[3];
                final serverurl = server['url']
                    .replaceAll('v', 'd')
                    .replaceAll('file.html', '${n + b + z - 3}$title');
                server['url'] = serverurl;
              }
            }
          }
        }
      }
      // return a list with the download links and info
      return ret;
    } catch (e) {}
  }
  return [];
}