Skip to content
Browse files

parse torrent file

  • Loading branch information...
1 parent 9e52381 commit 96c634df2339569232f07c147b3f2e9568e80040 @feross committed Feb 25, 2014
Showing with 24 additions and 11 deletions.
  1. +1 −7 lib/app.js
  2. +1 −1 lib/torrent-manager.js
  3. +21 −2 lib/torrent.js
  4. +1 −1 package.json
View
8 lib/app.js
@@ -170,13 +170,7 @@ App.prototype.downloadTorrentFile = function (torrent) {
console.log('write complete')
}
- var metadata = bncode.encode({
- 'announce-list': [],
- infoHash: torrent.infoHash,
- info: bncode.decode(torrent.metadataRaw)
- })
-
- writer.write(new Blob([metadata]), { type: 'application/x-bittorrent' })
+ writer.write(new Blob([torrent.torrentFile]), { type: 'application/x-bittorrent' })
}, errorHandler)
})
}
View
2 lib/torrent-manager.js
@@ -95,7 +95,7 @@ TorrentManager.prototype.add = function (uri) {
})
self.torrents.push(torrent)
- self._reemitEvents(torrent, 'torrent', ['listening', 'metadata'])
+ self._reemitEvents(torrent, 'torrent', ['listening'])
self.emit('addTorrent', torrent)
torrent.on('listening', function (port) {
View
23 lib/torrent.js
@@ -4,6 +4,7 @@ var bncode = require('bncode')
var EventEmitter = require('events').EventEmitter
var inherits = require('inherits')
var magnet = require('magnet-uri')
+var parseTorrent = require('parse-torrent')
var Swarm = require('bittorrent-swarm')
var METADATA_BLOCK_SIZE = 16 * 1024
@@ -176,7 +177,13 @@ Torrent.prototype._onUtMetadata = function (wire, buf) {
self.metadataRaw = wire.metadata
self.metadata = bncode.decode(wire.metadata)
- self.onMetadata(self.metadata)
+ self.torrentFile = bncode.encode({
+ 'announce-list': [],
+ infoHash: self.infoHash,
+ info: self.metadata
+ })
+
+ self.onMetadata()
break
// ut_metadata reject (peer doesn't have piece we requested)
// {'msg_type': 2, 'piece': 0}
@@ -186,8 +193,20 @@ Torrent.prototype._onUtMetadata = function (wire, buf) {
}
}
-Torrent.prototype.onMetadata = function (metadata) {
+Torrent.prototype.onMetadata = function () {
+ var self = this
+
+ var parsed
+ try {
+ parsed = parseTorrent(self.torrentFile)
+ } catch (e) {
+ console.error(e)
+ return
+ }
+ console.log(parsed)
+ // self.file = new Buffer(parsed)
}
+
//
// HELPER METHODS
//
View
2 package.json
@@ -37,7 +37,7 @@
"jquery": "^2.1.0",
"keymaster": "git://github.com/feross/keymaster",
"magnet-uri": "1.x",
- "read-torrent": "^0.2.0",
+ "parse-torrent": "^0.1.1",
"speedometer": "^0.1.2"
},
"devDependencies": {

0 comments on commit 96c634d

Please sign in to comment.
Something went wrong with that request. Please try again.