Created
October 23, 2012 19:30
-
-
Save pwmckenna/3941036 to your computer and use it in GitHub Desktop.
resolve_torrent_metadata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Btapp.prototype.resolve_torrent_metadata = function(magnet_link) { | |
var magnet_identifier = 'magnet:?xt=urn:btih:'; | |
if(magnet_link.indexOf(magnet_identifier) !== 0) | |
throw 'malformed magnet link'; | |
var hash = magnet_link.substring(magnet_identifier.length).substr(0, 40); | |
console.log(hash); | |
if(!hash.match(/[a-zA-Z0-9]/)) | |
throw 'only support hex encoded info hashes'; | |
var ret = new jQuery.Deferred(); | |
var torrent_selector = 'torrent'; | |
var wait_for_torrent_handler = function(torrents) { | |
this.die(torrent_selector, wait_for_torrent_handler, this); | |
torrents.download({ | |
url: magnet_link, | |
priority: Btapp.TORRENT.PRIORITY.METADATA_ONLY | |
}); | |
var wait_for_resolved_torrents_files = function(torrent) { | |
var file_selector = 'file'; | |
var wait_for_file_handler = function(files) { | |
files.keys().then(function(file_names) { | |
var file_add_selector = 'add'; | |
if(file_names.length === 0) | |
throw 'bad file name list'; | |
var check_files_list = function() { | |
if(file_names.length === files.length) { | |
files.off(file_add_selector, check_files_list); | |
ret.resolve(torrent); | |
} | |
}; | |
files.on(file_add_selector, check_files_list); | |
check_files_list(); | |
}); | |
torrent.die(file_selector, wait_for_file_handler); | |
}; | |
torrent.live(file_selector, wait_for_file_handler); | |
}; | |
var properties_selector = hash + ' properties'; | |
var wait_for_torrent_properties_handler = function(properties, torrent) { | |
torrents.die(properties_selector, wait_for_torrent_properties_handler); | |
if(properties.get('metadata_resolved')) { | |
wait_for_resolved_torrents_files(torrent); | |
} else { | |
var wait_for_resolved_torrents_files_handler = function() { | |
properties.off('change:metadata_resolved', wait_for_resolved_torrents_files_handler); | |
wait_for_resolved_torrents_files(torrent); | |
}; | |
properties.on('change:metadata_resolved', wait_for_resolved_torrents_files_handler); | |
} | |
}; | |
torrents.live(properties_selector, wait_for_torrent_properties_handler); | |
}; | |
this.live(torrent_selector, wait_for_torrent_handler, this); | |
return ret; | |
}; |
Btapp.prototype.resolve_torrent_metadata = (magnet_link) ->
magnet_identifier = 'magnet:?xt=urn:btih:'
if magnet_link.indexOf(magnet_identifier) != 0
throw 'malformed magnet link'
hash = magnet_link.substring(magnet_identifier.length).substr 0, 40
if !hash.match(/[a-zA-Z0-9]/)
throw 'only support hex encoded info hashes'
ret = new jQuery.Deferred()
torrent_selector = 'torrent'
wait_for_torrent_handler = (torrents) =>
@die torrent_selector, wait_for_torrent_handler,
torrents.download
url: magnet_link,
priority: Btapp.TORRENT.PRIORITY.METADATA_ONLY
wait_for_resolved_torrents_files = (torrent) =>
file_selector = 'file'
wait_for_file_handler = (files) =>
files.keys().then (file_names) =>
file_add_selector = 'add'
if file_names.length == 0
throw 'bad file name list'
check_files_list = () =>
if file_names.length == files.length
files.off file_add_selector, check_files_list
ret.resolve torrent
files.on file_add_selector, check_files_list
check_files_list()
torrent.die file_selector, wait_for_file_handler
torrent.live file_selector, wait_for_file_handler
properties_selector = hash + ' properties'
wait_for_torrent_properties_handler = (properties, torrent) =>
torrents.die properties_selector, wait_for_torrent_properties_handler
if properties.get 'metadata_resolved'
wait_for_resolved_torrents_files torrent
else
wait_for_resolved_torrents_files_handler = () =>
properties.off 'change:metadata_resolved', wait_for_resolved_torrents_files_handler
wait_for_resolved_torrents_files torrent
properties.on 'change:metadata_resolved', wait_for_resolved_torrents_files_handler
torrents.live properties_selector, wait_for_torrent_properties_handler
@live torrent_selector, wait_for_torrent_handler, @
return ret
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it would seem that we should utilize comments' preview ability