Skip to content

improvement To implement #31

@AlenToma

Description

@AlenToma

Hi, saw the component and I really liked it.

As if now it has many issue. So since I wanted to use it I hade to improve it a little.

Those are the improvement I did.
1- Support multi torrent download.
2- Support getting files information and more methods to use.
3- upgraded com.github.se_bastiaan.torrentstream to the latest

What more we need.

We need to be able to create a server/client socket to stream the video file instead of using com.github.se_bastiaan.torrentstream which dose clearly not work 100%

Those improment you could find in my fork(https://github.com/AlenToma/react-native-torrent-streamer)

I hope you can use it and implement it on this library.

Here is how you could use it as of now

import TorrentItem from 'react-native-torrent-streamer'
import useState from '@alentoma/usestate'
    const state = useState({
        progress: 0,
        buffer: 0,
        downloadSpeed: 0,
        seeds: 0,
        url: undefined, // "/storage/emulated/0/Download/Antlers (2021) [720p] [WEBRip] [YTS.MX]/Antlers.2021.720p.WEBRip.x264.AAC-[YTS.MX].mp4",
        loading: false,
        torrent: undefined,
        reload: false
    }, false)
  useEffect(() => {
        requestPermissions(); // permission to download folder, this has to be triggered
        var torrent = new TorrentItem("https://yts.mx/torrent/download/E68EE3FC711024F89D37F539A404F3211F326F97");
        state.torrent = torrent;
        torrent.addEventListener('error', (e) => {
            console.log(e)
        }).addEventListener('status', ({ progress, buffer, downloadSpeed, seeds, torrent }) => {
            var item = {
                progress,
                buffer,
                downloadSpeed,
                seeds,
                loading: false
            }
            state.setValue(item)
        }).addEventListener('ready', (data) => {
                // state.reload = true;
                state.url = data.url;
            }).addEventListener('stop', (d) => {
                console.log(d);
                state.loading = false;
            })

        return () => state.torrent?.clearEvents();
    }, [])


// and here is the Torrent class
import { DeviceEventEmitter, NativeModules } from 'react-native';

const { TorrentStreamer } = NativeModules;

const TORRENT_STREAMER_DOWNLOAD_EVENTS = {
  error: 'error',
  progress: 'progress',
  status: 'status',
  ready: 'ready',
  stop: 'stop'
};


export class Torrent {
  constructor(magnetUrl, saveLocation, removeAfterStop) {
    this.magnetUrl = magnetUrl;
    this.saveLocation = saveLocation;
    this.removeAfterStop = removeAfterStop;
    this.files = [];
    this._TorrentStreamerDownloadHandlers = {};
    if (!this.magnetUrl)
      throw "magnetUrl cant be empty";

    if (!this.saveLocation)
      this.saveLocation = null;

    if (!this.removeAfterStop)
      this.removeAfterStop = true;

    this.addEventListener("progress", (params) => {
      if (this.files.length <= 0)
        this.files = params.files;

      this.setSelectedFileIndex(-1);
    });
  }

  setSelectedFileIndex(selectedFileIndex) {
    TorrentStreamer.setSelectedFileIndex(this.magnetUrl, selectedFileIndex);
  }

  addEventListener(type, handler) {
    var ty = TORRENT_STREAMER_DOWNLOAD_EVENTS[type] + this.magnetUrl;
    this._TorrentStreamerDownloadHandlers[handler] = DeviceEventEmitter.addListener(ty, (torrentStreamerData) => {
      var tr = Object.assign({}, this);
      delete tr._TorrentStreamerDownloadHandlers;

      handler(Object.assign(torrentStreamerData, { torrent: tr }))
    });

    return this;
  }

  clearEvents() {
    Object.keys(this._TorrentStreamerDownloadHandlers).forEach(x => {
      var item = this._TorrentStreamerDownloadHandlers[x];
      if (item && item.remove) {
        item.remove();
        delete this._TorrentStreamerDownloadHandlers[x];
      }
    })
  }

  async start() {
    await TorrentStreamer.createTorrent(this.magnetUrl, this.saveLocation, this.removeAfterStop);
    TorrentStreamer.start(this.magnetUrl);
  }

  stop() {
    TorrentStreamer.stop(this.magnetUrl);
  }

  destroy() {
    TorrentStreamer.stop(this.magnetUrl);
  }
}

export default Torrent


You will also have too look at the java files.

Hop those changes may help you improve the library more

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions