Skip to content

Instantly share code, notes, and snippets.

@pwmckenna
Created October 23, 2012 19:30
Show Gist options
  • Save pwmckenna/3941036 to your computer and use it in GitHub Desktop.
Save pwmckenna/3941036 to your computer and use it in GitHub Desktop.
resolve_torrent_metadata
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;
};
@pwmckenna
Copy link
Author

btapp = new Btapp;
btapp.connect();
btapp.resolve_torrent_metadata('magnet:?xt=urn:btih:5E60858AA93FBB5CE4D77CBC7B8FB7428517D8B8').then(function(torrent) { //do something with your fully resolved torrent });

@pwmckenna
Copy link
Author

btapp = new Btapp;
btapp.connect();
btapp.resolve_torrent_metadata('magnet:?xt=urn:btih:5E60858AA93FBB5CE4D77CBC7B8FB7428517D8B8')
    .then(function(torrent) { //do something with your fully resolved torrent });

@pwmckenna
Copy link
Author

var btapp = new Btapp;
btapp.connect();
var magnet_link = 'magnet:?xt=urn:btih:5E60858AA93FBB5CE4D77CBC7B8FB7428517D8B8';
btapp.resolve_torrent_metadata(magnet_link).then(function(torrent) { 
    //do something with your fully resolved torrent 
});

@BrandonBT
Copy link

coffee-ified:
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

@pwmckenna
Copy link
Author

var btapp = new Btapp;
btapp.connect();
var magnet_link = 'magnet:?xt=urn:btih:5E60858AA93FBB5CE4D77CBC7B8FB7428517D8B8';
btapp.resolve_torrent_metadata(magnet_link).then(function(torrent) { 
    //do something with your fully resolved torrent 
});

@pwmckenna
Copy link
Author

it would seem that we should utilize comments' preview ability

@BrandonBT
Copy link

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