Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse torrent file from XMLHttpRequest #424

Closed
jakefb opened this issue Sep 7, 2015 · 4 comments
Closed

Parse torrent file from XMLHttpRequest #424

jakefb opened this issue Sep 7, 2015 · 4 comments

Comments

@jakefb
Copy link
Contributor

@jakefb jakefb commented Sep 7, 2015

I want to stream a video in the browser from a .torrent file. To do this I have made an XHR request to return a buffer, which I want to parse with parse-torrent and download with WebTorrent. This is because I want to load the whole torrent file, so I can start streaming for a HTTP WebSeed, without having to wait for WebRTC peers to connect.

I don't know how to do this so help would be greatly appreciated. Here's my code:

function reqListener () {
  console.log('.torrent file download')

  var videoTorrent =  new Uint8Array(this.response)
  var parsedTorrent =  parseTorrent(videoTorrent)
  client.add(parsedTorrent, onTorrent)
}

var oReq = new XMLHttpRequest()

oReq.addEventListener("load", reqListener)

oReq.responseType = 'arraybuffer';

oReq.open('GET', '/torrents/yosemite-hd.torrent', true)

oReq.send()

function onTorrent (torrent) {
  ...
}
@jakefb

This comment has been minimized.

Copy link
Contributor Author

@jakefb jakefb commented Sep 10, 2015

I managed to get this working by using stream-http - thanks @jhiesey. You can see it in action here http://fastcast.nz/videos/cityscape-chicago-ii.html. Here's some of the code from my project (source)

var parseTorrent = require('parse-torrent')
var http = require('stream-http')
var WebTorrent = require('webtorrent')

var torrentName = 'cityscape-chicago-ii.torrent'

http.get('/torrents/' + torrentName, function (res) {
  var data = [] // List of Buffer objects

  res.on("data", function(chunk) {
    data.push(chunk) // Append Buffer object
  })

  res.on("end", function() {
    data = Buffer.concat(data) // Make one large Buffer of it

    var torrentParsed = parseTorrent(data) // Parse the Buffer

    var client = new WebTorrent()

    client.add(torrentParsed, onTorrent)

    function onTorrent (torrent) {
      ...
    }
  })
})
@jakefb jakefb closed this Sep 10, 2015
@vintikzzz

This comment has been minimized.

Copy link

@vintikzzz vintikzzz commented Nov 29, 2017

It could be achieved without additional dependencies. The main idea is to convert from ArrayBuffer to Buffer.
This works!

            const xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function() {
              if (this.readyState == 4) {
                const ab = new Uint8Array(this.response);
                const buffer = new Buffer(ab.byteLength);
                const view = new Uint8Array(ab);
                for (let i = 0; i < buffer.length; ++i) {
                  buffer[i] = view[i];
                }
                const torrent = parseTorrent(buffer);
              }
            };
            xhr.open('GET', 'http://exmaple-url');
            xhr.responseType = 'arraybuffer';
            xhr.send();

@SilentBot1

This comment has been minimized.

Copy link
Member

@SilentBot1 SilentBot1 commented Nov 29, 2017

Hey @vintikzzz, since the original post, the WebTorrent library (parse-torrent) added support for passing through a URL to a .torrent file while calling client.add().

An example of this in action:

var WebTorrent = require('webtorrent')
var client = new WebTorrent()

var torrent = client.add("https://webtorrent.io/torrents/sintel.torrent")

Note: In a browser the URL must have the header 'Access-Control-Allow-Origin' set while in Node.js, it doesn't mind.

@jakefb

This comment has been minimized.

Copy link
Contributor Author

@jakefb jakefb commented Nov 30, 2017

Thanks @vintikzzz and @SilentBot1 😃

Good to know that this is supported in parse-torrent

@lock lock bot locked as resolved and limited conversation to collaborators May 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.