dtorrent_task 0.3.2 copy "dtorrent_task: ^0.3.2" to clipboard
dtorrent_task: ^0.3.2 copied to clipboard

BitTorrent download client package written by pure Dart language.

example/dtorrent_task_example.dart

import 'dart:async';
import 'dart:developer';
import 'dart:io';

import 'package:dtorrent_common/dtorrent_common.dart';
import 'package:dtorrent_parser/dtorrent_parser.dart';
import 'package:dtorrent_task/dtorrent_task.dart';

void main() async {
  try {
    var torrentFile = 'example${Platform.pathSeparator}test4.torrent';
    var savePath = 'tmp${Platform.pathSeparator}test';
    var model = await Torrent.parse(torrentFile);
    // model.announces.clear();
    var task = TorrentTask.newTask(model, savePath);
    Timer? timer;
    Timer? timer1;
    var startTime = DateTime.now().millisecondsSinceEpoch;
    task.onTaskComplete(() {
      print(
          'Complete! spend time : ${((DateTime.now().millisecondsSinceEpoch - startTime) / 60000).toStringAsFixed(2)} minutes');
      timer?.cancel();
      timer1?.cancel();
      task.stop();
    });
    task.onStop(() async {
      print('Task Stopped');
    });
    var map = await task.start();

    // ignore: unawaited_futures
    findPublicTrackers().listen((alist) {
      for (var element in alist) {
        task.startAnnounceUrl(element, model.infoHashBuffer);
      }
    });
    log('Adding dht nodes');
    for (var element in model.nodes) {
      log('dht node $element');
      task.addDHTNode(element);
    }
    print(map);

    timer = Timer.periodic(Duration(seconds: 2), (timer) async {
      var progress = '${(task.progress * 100).toStringAsFixed(2)}%';
      var ads = ((task.averageDownloadSpeed) * 1000 / 1024).toStringAsFixed(2);
      var aps = ((task.averageUploadSpeed) * 1000 / 1024).toStringAsFixed(2);
      var ds = ((task.currentDownloadSpeed) * 1000 / 1024).toStringAsFixed(2);
      var ps = ((task.uploadSpeed) * 1000 / 1024).toStringAsFixed(2);

      var utpd = ((task.utpDownloadSpeed) * 1000 / 1024).toStringAsFixed(2);
      var utpu = ((task.utpUploadSpeed) * 1000 / 1024).toStringAsFixed(2);
      var utpc = task.utpPeerCount;

      var active = task.connectedPeersNumber;
      var seeders = task.seederNumber;
      var all = task.allPeersNumber;
      print(
          'Progress : $progress , Peers:($active/$seeders/$all)($utpc) . Download speed : ($utpd)($ads/$ds)kb/s , upload speed : ($utpu)($aps/$ps)kb/s');
    });
  } catch (e) {
    print(e);
  }
}
12
likes
0
pub points
59%
popularity

Publisher

unverified uploader

BitTorrent download client package written by pure Dart language.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

b_encode_decode, bittorrent_dht, dart_ipify, dtorrent_common, dtorrent_parser, dtorrent_tracker, utp_protocol

More

Packages that depend on dtorrent_task