Build BitTorrent Web Apps – Dependencies – Source



Build BitTorrent Web Apps – Dependencies – Source

0 0


BtappsPresentation

BtappsPresentation

On Github lporras / BtappsPresentation

Build BitTorrent Web Apps

Created by Luis Porras / @lporras16 Software Developer at Selfie Inc

What is Btapp.js?

  • Javascript Library built on top of Backbone.js
  • Brought to you by BitTorrent Inc.
  • Brings you more than just downloads

Interesting Features

Dependencies

Dependencies

Btapp.js has hard dependencies on jQuery, Backbone, Json2 and jStorage

  • <script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
  • <script src="http://cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.js"></script>
  • <script src="http://underscorejs.org/underscore-min.js"></script>
  • <script src="http://backbonejs.org/backbone-min.js"></script>
  • <script src="https://raw.github.com/andris9/jStorage/master/jstorage.js"></script>

Dependencies

Backbrace is also heavily recommended, to alleviate some of the trouble you might have using deep trees of Backbone models and collections.

  • <script src="https://raw.github.com/bittorrenttorque/backbrace/master/backbrace.js"></script>

Source

  • <script src="http://torque.bittorrent.com/btapp/btapp.min.js"></script>

or... Download Btapp.js source

Api Compatibility

Api Compatibility

By default, the api will try to use Torque

which means, it facilitates the installation of a browser plugin, and the underlying torrent client if it isn't already available.

Connect to Torrent Clients

Torque

//Connecting with Torque (by default)
var btapp = new Btapp;
btapp.connect();
              

Or...

//Using plugin sintax
var btapp = new Btapp;
btapp.connect({
  product: 'Torque'
  plugin: true
});
              

Connect to Torrent Clients

uTorrent

var btapp = new Btapp;
btapp.connect({
  product: 'uTorrent',
  plugin: false,
  pairing_type: 'native'
});
              

BitTorrent

var btapp = new Btapp;
btapp.connect({
  product: 'BitTorrent',
  plugin: false,
  pairing_type: 'native'
});
            

Code Snippets

Asynchronous Warning

Don't

var btapp = new Btapp();
btapp.connect();

btapp.get('torrent').each(function(torrent) {
  torrent.remove();
});
            

Do

var btapp = new Btapp();
btapp.connect();

// This code will wait for the traditional backbone add event to be thrown.
// We can listen to a specific attribute (in this case torrent) by adding :attribute
btapp.on('add:torrent', function(torrents) {
  torrents.each(function(torrent) {
    torrent.remove();
  });
});
            

Adding a torrent

var magnet = 'magnet:?xt=urn:btih:f0d665f264393a7dafd7d05d739e1097df652e80&dn=The.Yes.Men.Fix.The.World';

//var url = 'http://vodo.net/media/torrents/Deadside.Pilot.2012.720p.x264-VODO.torrent';

var callback = function() {
  alert('added the torrent');
};

btapp.get('add').torrent({
  url: magnet,
  callback: callback
});
            

Set priorities

// To add a torrent and stop after the torrent file has been added (or the magnet link has resolved to a torrent file), try the following:

var url = 'http://vodo.net/media/torrents/Deadside.Pilot.2012.720p.x264-VODO.torrent';

btapp.get('add').torrent({
  url: url,
  priority: Btapp.TORRENT.PRIORITY.METADATA_ONLY
});
            

Torrent Files

var files = btapp.get('torrent').get('D9C70109CB05C181F9EC9373BE876A0D40C4D7B0').get('file');

> files.pluck('id');

 ["Deadside.2012.720p.x264-VODO.mkv", "Want.to.see.what.happens.next.url", "vodo.nfo"]
            

Torrent download percentage

var btapp = new Btapp;
btapp.connect();

var url = 'http://vodo.net/media/torrents/Deadside.Pilot.2012.720p.x264-VODO.torrent';

btapp.live('add', function(add) {
  add.torrent(url);
});

btapp.live('torrent * properties download_url', function(download_url, properties) {
  // Only interested in the torrent that has a download_url that matches the location we provided
  if(download_url == url) {
    var name = properties.get('name');
    // We found our torrent!
    // Now lets listen for progress changes
    properties.on('change:progress', function(progress) {
      // We have an update!
      alert('Download progress for ' + name + ': ' + progress);
    });
  }
});
            

Live Examples?

Other Material

Live Coding: DHT App using btapp.js from Patrick Williams on Vimeo.

Questions?

Thanks