Net-BitTorrentSync

 view release on metacpan or  search on metacpan

lib/Net/BitTorrentSync.pm  view on Meta::CPAN

our @ISA = 'Exporter';

our @EXPORT = qw(
    start_btsync
    set_config
    set_listened_address
    add_folder
    get_folders
    remove_folder
    get_files
    set_file_prefs
    get_folder_peers
    get_secrets
    get_folder_prefs
    set_folder_prefs
    get_folder_hosts
    set_folder_hosts
    get_prefs
    set_prefs
    get_os
    get_version
    get_speed
    shutdown_btsync
);

our $VERSION = '0.21';

my ($config, $listen);

lib/Net/BitTorrentSync.pm  view on Meta::CPAN


sub get_files {
    my ($secret, $path) = @_;
    my $request = "get_files&secret=$secret";

    $request .= "&path=$path" if $path;

    return _access_api($request);
}

=head2 set_file_prefs

Selects file for download for selective sync folders.
Returns file information with applied preferences.

=over 4

=item secret (required)

=item path (required)

Specifies path to a subfolder of the sync folder.

=item download (required)

Specifies if file should be downloaded (yes - 1, no - 0)

=back

=cut

sub set_file_prefs {
    my ($secret, $path, $download) = @_;
    my $request = "get_files&secret=$secret&path=$path&download=$download";

    return _access_api($request);
}

=head2 get_folder_peers

Returns list of peers connected to the specified folder.

lib/Net/BitTorrentSync.pm  view on Meta::CPAN


sub get_secrets {
    my ($secret, $type) = @_;

    my $request = "get_secrets";
    $request .= "&secret=$secret" if $secret;
    $request .= "&type=encryption" if $type;
    return _access_api($request);
}

=head2 get_folder_prefs

Returns preferences for the specified sync folder.

    {
        search_lan       => 1,
        use_dht          => 0,
        use_hosts        => 0,
        use_relay_server => 1,
        use_sync_trash   => 1,
        use_tracker      => 1
    }

=over 4

=item secret (required)

=back

=cut

sub get_folder_prefs {
    my ($secret) = @_;
    my $request = "get_folder_prefs&secret=$secret";
    return _access_api($request);
}

=head2 set_folder_prefs

Sets preferences for the specified sync folder.
Parameters are the same as in ‘Get folder preferences’.
Returns current settings.

=over 4

=item secret (required)

=item preferences

lib/Net/BitTorrentSync.pm  view on Meta::CPAN

=item use_tracker

=item use_sync_trash

=back

=back

=cut

sub set_folder_prefs {
    my ($secret, $prefs) = @_;
    my $request = "set_folder_prefs&secret=$secret";

    foreach my $pref (keys %{$prefs}) {
        $request .= '&' . $pref . '=' . $prefs->{$pref};
    }

    return _access_api($request);
}

=head2 get_folder_hosts

Returns list of predefined hosts for the folder,
or error code if a secret is not specified.

lib/Net/BitTorrentSync.pm  view on Meta::CPAN


sub set_folder_hosts {
    my ($secret, $hosts) = @_;
    my $request = "set_folder_hosts&secret=$secret&hosts=";

    $request .= join ',', @{$hosts};

    return _access_api($request);
}

=head2 get_prefs

Returns BitTorrent Sync preferences.
Contains dictionary with advanced preferences.
Please see Sync user guide for description of each option.

    {
        device_name                     => "iMac",
        disk_low_priority               => "true",
        download_limit                  => 0,
        folder_rescan_interval          => "600",

lib/Net/BitTorrentSync.pm  view on Meta::CPAN

        send_buf_size                   => "5",
        sync_max_time_diff              => "600",
        sync_trash_ttl                  => "30",
        upload_limit                    => 0,
        use_upnp                        => 0,
        recv_buf_size                   => "5"
    }

=cut

sub get_prefs {
    return _access_api("get_prefs");
}

=head2 set_prefs

Sets BitTorrent Sync preferences.
Parameters are the same as in ‘Get preferences’.
Advanced preferences are set as general settings. Returns current settings.

=over 4

=item preferences (required)

A hashref (see get_prefs) containing the preferences you wish to change.

=back

=cut

sub set_prefs {
    my ($secret, $prefs) = @_;
    my $request = "set_prefs";

    foreach my $pref (keys %{$prefs}) {
        $request .= '&' . $pref . '=' . $prefs->{$pref};
    }

    return _access_api($request);
}

=head2 get_os

Returns OS name where BitTorrent Sync is running.

    {

t/api.t  view on Meta::CPAN

];

is_deeply ($response, $compare, 'matching file structures');

# get_folder_peers

$response = get_folder_peers($secret);

is_deeply ($response , [], 'Should get an empty arrayref');

# get_folder_prefs

$response = get_folder_prefs($secret);

$compare = {
  search_lan       => 1,
  selective_sync   => 0,
  use_dht          => 0,
  use_hosts        => 0,
  use_relay_server => 1,
  use_sync_trash   => 1,
  use_tracker      => 1,
};

is_deeply ($response, $compare, 'Correct folder preferences');

# set_folder_prefs

$response = set_folder_prefs($secret, {
  selective_sync => 1,
  use_hosts      => 1,
  use_sync_trash => 0,
});

$compare = {
  search_lan       => 1,
  selective_sync   => 1,
  use_dht          => 0,
  use_hosts        => 1,

t/api.t  view on Meta::CPAN

};

is_deeply ($response, $compare, 'Correct new folder preferences');

# get_folder_hosts

$response = get_folder_hosts($secret);

is_deeply($response, { hosts => [] }, 'empty arrayref for now');

# get_prefs

$response = get_prefs();

my @keys = qw/
              device_name disk_low_priority download_limit
              folder_rescan_interval lan_encrypt_data lan_use_tcp
              lang listening_port max_file_size_diff_for_patching
              max_file_size_for_versioning rate_limit_local_peers
              recv_buf_size send_buf_size sync_max_time_diff sync_trash_ttl
              upload_limit use_upnp
             /;

is_deeply([sort keys %{$response}], [@keys], 'same keys');

# TODO: set_file_prefs
# TODO: set_folder_hosts
# TODO: set_prefs

# remove_folder
is_deeply(
  remove_folder($secret),
  {error => 0},
  'folder removed ok'
);

$response = get_folders();



( run in 0.766 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )