Class: QbtClient::WebUI

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/qbt_client/web_ui.rb

Instance Method Summary collapse

Constructor Details

#initialize(ip, port, user, pass) ⇒ WebUI

constructor



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/qbt_client/web_ui.rb', line 27

def initialize(ip, port, user, pass)
  @ip         = ip
  @port       = port
  @user       = user
  @pass       = pass
  @sid        = nil

  #self.class.digest_auth(user, pass)
  host = "#{ip}:#{port}"
  self.class.base_uri host
  self.class.headers "Referer" => host
  authenticate
  self.class.cookies.add_cookies(@sid)
end

Instance Method Details

#add_trackers(torrent_hash, urls) ⇒ Object

Add one or more trackers to a torrent

If passing mulitple urls, pass them as an array.



213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/qbt_client/web_ui.rb', line 213

def add_trackers torrent_hash, urls
  urls = Array(urls)
  # Ampersands in urls must be escaped.
  urls = urls.map { |url| url.gsub('&', '%26') }
  urls = urls.join('%0A')

  options = {
    body: "hash=#{torrent_hash}&urls=#{urls}"
  }

  self.class.post('/command/addTrackers', options)
end

#api_min_versionObject

Get the application’s minimum API version

Returns an integer



85
86
87
88
# File 'lib/qbt_client/web_ui.rb', line 85

def api_min_version
  self.class.format :json
  self.class.get('/version/api_min').parsed_response
end

#api_versionObject

Get the application’s API version

Returns an integer



75
76
77
78
# File 'lib/qbt_client/web_ui.rb', line 75

def api_version
  self.class.format :json
  self.class.get('/version/api').parsed_response
end

#authenticateObject

Authenticate with the server

Login with username and password. Store returned SID cookie value used as auth token for later calls.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/qbt_client/web_ui.rb', line 49

def authenticate
  options = {
    body: "username=#{@user}&password=#{@pass}"
  }

  # Have to clear out the cookies or the old SID gets sent while requesting
  # the new SID (and it fails).
  self.class.cookies.clear

  res = self.class.post('/login', options)
  if res.success?
    token = res.headers["Set-Cookie"]
    raise QbtClientError.new("Login failed: no SID (cookie) returned") if token.nil?

    token = token.split(";")[0]
    @sid = token
  else
    raise QbtClientError.new(res)
  end
end

#contents(torrent_hash) ⇒ Object

Get torrent contents (files data)

Example response:

[
  {
    "is_seed"=>false,
    "name"=>"Grimm.S04E12.720p.HDTV.X264-DIMENSION.mkv",
    "priority"=>1,
    "progress"=>0.0,
    "size"=>"825.4 MiB"
  }
]


240
241
242
243
# File 'lib/qbt_client/web_ui.rb', line 240

def contents torrent_hash
  self.class.format :json
  self.class.get('/query/propertiesFiles/' + torrent_hash).parsed_response
end

#decrease_priority(torrent_hashes) ⇒ Object

Decrease the priority of one or more torrents

If passing multiple torrent hashes, pass them as an array. Note: This does nothing unless queueing has been enabled via preferences.



477
478
479
480
481
482
483
484
485
486
# File 'lib/qbt_client/web_ui.rb', line 477

def decrease_priority torrent_hashes
  torrent_hashes = Array(torrent_hashes)
  torrent_hashes = torrent_hashes.join('|')

  options = {
    body: "hashes=#{torrent_hashes}"
  }

  self.class.post('/command/decreasePrio', options)
end

#delete(torrent_hashes) ⇒ Object

Delete one or more torrents (doesn’t delete their data)

If passing multiple torrent hashes, pass them as an array.



430
431
432
433
434
435
436
437
438
439
# File 'lib/qbt_client/web_ui.rb', line 430

def delete torrent_hashes
  torrent_hashes = Array(torrent_hashes)
  torrent_hashes = torrent_hashes.join('|')

  options = {
    body: "hashes=#{torrent_hashes}"
  }

  self.class.post('/command/delete', options)
end

#delete_torrent_and_data(torrent_hashes) ⇒ Object

Delete one or more torrents AND THEIR DATA

If passing multiple torrent hashes, pass them as an array.



414
415
416
417
418
419
420
421
422
423
# File 'lib/qbt_client/web_ui.rb', line 414

def delete_torrent_and_data torrent_hashes
  torrent_hashes = Array(torrent_hashes)
  torrent_hashes = torrent_hashes.join('|')

  options = {
    body: "hashes=#{torrent_hashes}"
  }

  self.class.post('/command/deletePerm', options)
end

#download(urls) ⇒ Object

Begin downloading one or more torrents.

If passing mulitple urls, pass them as an array.



398
399
400
401
402
403
404
405
406
407
# File 'lib/qbt_client/web_ui.rb', line 398

def download urls
  urls = Array(urls)
  urls = urls.join('%0A')

  options = {
    body: "urls=#{urls}"
  }

  self.class.post('/command/download', options)
end

#download_limit(torrent_hash) ⇒ Object

Get a torrent’s download limit

A limit of 0 means unlimited.

Returns an integer (bytes)



604
605
606
607
608
609
610
611
612
613
614
# File 'lib/qbt_client/web_ui.rb', line 604

def download_limit torrent_hash
  self.class.format :json

  options = {
    body: "hashes=#{torrent_hash}"
  }

  self.class
    .post('/command/getTorrentsDlLimit', options)
    .parsed_response[torrent_hash]
end

#global_download_limitObject

Get the application’s global download limit

A limit of 0 means unlimited.

Returns an integer (bytes)



546
547
548
549
# File 'lib/qbt_client/web_ui.rb', line 546

def global_download_limit
  self.class.format :json
  self.class.post('/command/getGlobalDlLimit').parsed_response
end

#global_upload_limitObject

Get the application’s global upload limit

A limit of 0 means unlimited.

Returns an integer (bytes)



575
576
577
578
# File 'lib/qbt_client/web_ui.rb', line 575

def global_upload_limit
  self.class.format :json
  self.class.post('/command/getGlobalUpLimit').parsed_response
end

#increase_priority(torrent_hashes) ⇒ Object

Increase the priority of one or more torrents

If passing multiple torrent hashes, pass them as an array. Note: This does nothing unless queueing has been enabled via preferences.



459
460
461
462
463
464
465
466
467
468
# File 'lib/qbt_client/web_ui.rb', line 459

def increase_priority torrent_hashes
  torrent_hashes = Array(torrent_hashes)
  torrent_hashes = torrent_hashes.join('|')

  options = {
    body: "hashes=#{torrent_hashes}"
  }

  self.class.post('/command/increasePrio', options)
end

#maximize_priority(torrent_hashes) ⇒ Object

Increase the priority of one or more torrents to the maximum value

If passing multiple torrent hashes, pass them as an array. Note: This does nothing unless queueing has been enabled via preferences.



495
496
497
498
499
500
501
502
503
504
# File 'lib/qbt_client/web_ui.rb', line 495

def maximize_priority torrent_hashes
  torrent_hashes = Array(torrent_hashes)
  torrent_hashes = torrent_hashes.join('|')

  options = {
    body: "hashes=#{torrent_hashes}"
  }

  self.class.post('/command/topPrio', options)
end

#minimize_priority(torrent_hashes) ⇒ Object

Decrease the priority of one or more torrents to the minimum value

If passing multiple torrent hashes, pass them as an array. Note: This does nothing unless queueing has been enabled via preferences.



513
514
515
516
517
518
519
520
521
522
# File 'lib/qbt_client/web_ui.rb', line 513

def minimize_priority torrent_hashes
  torrent_hashes = Array(torrent_hashes)
  torrent_hashes = torrent_hashes.join('|')

  options = {
    body: "hashes=#{torrent_hashes}"
  }

  self.class.post('/command/bottomPrio', options)
end

#pause(torrent_hash) ⇒ Object

Pause a torrent



360
361
362
363
364
365
366
# File 'lib/qbt_client/web_ui.rb', line 360

def pause torrent_hash
  options = {
    body: "hash=#{torrent_hash}"
  }

  self.class.post('/command/pause', options)
end

#pause_allObject

Pause all torrents



371
372
373
# File 'lib/qbt_client/web_ui.rb', line 371

def pause_all
  self.class.post('/command/pauseAll')
end

#preferencesObject

Get application preferences (options)

Example response:

{
  "alt_dl_limit"=>10,
  "alt_up_limit"=>10,
  "anonymous_mode"=>false,
  "autorun_enabled"=>false,
  "autorun_program"=>"",
  "bypass_local_auth"=>false,
  "dht"=>true,
  "dhtSameAsBT"=>true,
  "dht_port"=>6881,
  "dl_limit"=>-1,
  "dont_count_slow_torrents"=>false,
  "download_in_scan_dirs"=>[],
  "dyndns_domain"=>"changeme.dyndns.org",
  "dyndns_enabled"=>false,
  "dyndns_password"=>"",
  "dyndns_service"=>0,
  "dyndns_username"=>"",
  "enable_utp"=>true,
  "encryption"=>0,
  "export_dir"=>"",
  "export_dir_enabled"=>false,
  "incomplete_files_ext"=>false,
  "ip_filter_enabled"=>false,
  "ip_filter_path"=>"",
  "limit_tcp_overhead"=>false,
  "limit_utp_rate"=>true,
  "listen_port"=>6881,
  "locale"=>"en_US",
  "lsd"=>true,
  "mail_notification_auth_enabled"=>false,
  "mail_notification_email"=>"",
  "mail_notification_enabled"=>false,
  "mail_notification_password"=>"",
  "mail_notification_smtp"=>"smtp.changeme.com",
  "mail_notification_ssl_enabled"=>false,
  "mail_notification_username"=>"",
  "max_active_downloads"=>3,
  "max_active_torrents"=>5,
  "max_active_uploads"=>3,
  "max_connec"=>500,
  "max_connec_per_torrent"=>100,
  "max_uploads_per_torrent"=>4,
  "pex"=>true,
  "preallocate_all"=>false,
  "proxy_auth_enabled"=>false,
  "proxy_ip"=>"0.0.0.0",
  "proxy_password"=>"",
  "proxy_peer_connections"=>false,
  "proxy_port"=>8080,
  "proxy_type"=>-1,
  "proxy_username"=>"",
  "queueing_enabled"=>false,
  "save_path"=>"/home/jeff/Downloads",
  "scan_dirs"=>[],
  "schedule_from_hour"=>8,
  "schedule_from_min"=>0,
  "schedule_to_hour"=>20,
  "schedule_to_min"=>0,
  "scheduler_days"=>0,
  "scheduler_enabled"=>false,
  "ssl_cert"=>"",
  "ssl_key"=>"",
  "temp_path"=>"/home/jeff/Downloads/temp",
  "temp_path_enabled"=>false,
  "up_limit"=>50,
  "upnp"=>true,
  "use_https"=>false,
  "web_ui_password"=>"ae150cdc82b40c4373d2e15e0ffe8f67",
  "web_ui_port"=>8083,
  "web_ui_username"=>"admin"
}


336
337
338
339
# File 'lib/qbt_client/web_ui.rb', line 336

def preferences
  self.class.format :json
  self.class.get('/query/preferences').parsed_response
end

#properties(torrent_hash) ⇒ Object

Get properties of a torrent (different data than what’s returned in #torrent_list).

Example response:

{
  "comment"=>"Visit us: https://eztv.ch/ - Bitcoin: 1EZTVaGQ6UsjYJ9fwqGnd45oZ6HGT7WKZd",
  "creation_date"=>"Friday, February 6, 2015 8:01:22 PM MST",
  "dl_limit"=>"",
  "nb_connections"=>"0 (100 max)",
  "piece_size"=>"512.0 KiB",
  "save_path"=>"/home/jeff/Downloads/",
  "share_ratio"=>"0.0",
  "time_elapsed"=>"< 1m",
  "total_downloaded"=>"646.8 KiB (657.8 KiB this session)",
  "total_uploaded"=>"0 B (0 B this session)",
  "total_wasted"=>"428 B",
  "up_limit"=>""
}


171
172
173
174
# File 'lib/qbt_client/web_ui.rb', line 171

def properties torrent_hash
  self.class.format :json
  self.class.get('/query/propertiesGeneral/' + torrent_hash).parsed_response
end

#qbittorrent_versionObject

Get the application’s version

Returns an integer



95
96
97
98
99
# File 'lib/qbt_client/web_ui.rb', line 95

def qbittorrent_version
  self.class.format :plain
  self.class.get('/version/qbittorrent').parsed_response
  #self.class.get('/version/qbittorrent')
end

#recheck(torrent_hash) ⇒ Object

Recheck a torrent



444
445
446
447
448
449
450
# File 'lib/qbt_client/web_ui.rb', line 444

def recheck torrent_hash
  options = {
    body: "hash=#{torrent_hash}"
  }

  self.class.post('/command/recheck', options)
end

#resume(torrent_hash) ⇒ Object

Resume downloading/seeding of a torrent



378
379
380
381
382
383
384
# File 'lib/qbt_client/web_ui.rb', line 378

def resume torrent_hash
  options = {
    body: "hash=#{torrent_hash}"
  }

  self.class.post('/command/resume', options)
end

#resume_allObject

Resume downloading/seeding of all torrents



389
390
391
# File 'lib/qbt_client/web_ui.rb', line 389

def resume_all
  self.class.post('/command/resumeAll')
end

#set_download_limit(torrent_hash, limit) ⇒ Object

Set a torrent’s download limit

A limit of 0 means unlimited.

torrent_hash: string limit: integer (bytes)



624
625
626
627
628
629
630
631
632
# File 'lib/qbt_client/web_ui.rb', line 624

def set_download_limit torrent_hash, limit
  query = ["hashes=#{torrent_hash}", "limit=#{limit}"]

  options = {
    body: query.join('&')
  }

  self.class.post('/command/setTorrentsDlLimit', options)
end

#set_file_priority(torrent_hash, file_id, priority) ⇒ Object

Set the download priority of a file within a torrent

file_id is a 0 based position of the file within the torrent



529
530
531
532
533
534
535
536
537
# File 'lib/qbt_client/web_ui.rb', line 529

def set_file_priority torrent_hash, file_id, priority
  query = ["hash=#{torrent_hash}", "id=#{file_id}", "priority=#{priority}"]

  options = {
    body: query.join('&')
  }

  self.class.post('/command/setFilePrio', options)
end

#set_global_download_limit(limit) ⇒ Object

Set the application’s global download limit

A limit of 0 means unlimited.

limit: integer (bytes)



558
559
560
561
562
563
564
565
566
# File 'lib/qbt_client/web_ui.rb', line 558

def set_global_download_limit limit
  query = "limit=#{limit}"

  options = {
    body: query
  }

  self.class.post('/command/setGlobalDlLimit', options)
end

#set_global_upload_limit(limit) ⇒ Object

Set the application’s global upload limit

A limit of 0 means unlimited.

limit: integer (bytes)



587
588
589
590
591
592
593
594
595
# File 'lib/qbt_client/web_ui.rb', line 587

def set_global_upload_limit limit
  query = "limit=#{limit}"

  options = {
    body: query
  }

  self.class.post('/command/setGlobalUpLimit', options)
end

#set_preferences(pref_hash) ⇒ Object

Set application preferences

Note: When setting password, pass it as plain text. You can send only the key/value pairs you want to change (in a hash), rather than the entire set of data.



348
349
350
351
352
353
354
355
# File 'lib/qbt_client/web_ui.rb', line 348

def set_preferences pref_hash
  pref_hash = Hash(pref_hash)
  options = {
    body: "json=#{pref_hash.to_json}"
  }

  self.class.post('/command/setPreferences', options)
end

#set_upload_limit(torrent_hash, limit) ⇒ Object

Set a torrent’s upload limit

A limit of 0 means unlimited.

torrent_hash: string limit: integer (bytes)



661
662
663
664
665
666
667
668
669
# File 'lib/qbt_client/web_ui.rb', line 661

def set_upload_limit torrent_hash, limit
  query = ["hashes=#{torrent_hash}", "limit=#{limit}"]

  options = {
    body: query.join('&')
  }

  self.class.post('/command/setTorrentsUpLimit', options)
end

#torrent_data(torrent_hash) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/qbt_client/web_ui.rb', line 141

def torrent_data torrent_hash
  torrents = torrent_list

  torrents.each do |t|
    if t["hash"] == torrent_hash
      return t
    end
  end
end

#torrent_listObject

Get array of all torrents

Example response:

[
  {
      "dlspeed"=>"3.1 MiB/s",
      "eta"=>"9m",
      "hash"=>"156b69b8643bd11849a5d8f2122e13fbb61bd041",
      "name"=>"slackware64-14.1-iso",
      "num_leechs"=>"1 (14)",
      "num_seeds"=>"97 (270)",
      "priority"=>"*",
      "progress"=>0.172291,
      "ratio"=>"0.0",
      "size"=>"2.2 GiB",
      "state"=>"downloading",
      "upspeed"=>"0 B/s"
  },
  {
    "dlspeed"=>"1.8 KiB/s",
    "eta"=>"28d 1h",
    "hash"=>"1fe5775d32d3e58e48b3a96dd2883c5250882cda",
    "name"=>"Grimm.S04E12.720p.HDTV.X264-DIMENSION.mkv",
    "num_leechs"=>"7 (471)",
    "num_seeds"=>"15 (1866)",
    "priority"=>"*",
    "progress"=>1.53669e-07,
    "ratio"=>"0.0",
    "size"=>"825.4 MiB",
    "state"=>"downloading",
    "upspeed"=>"0 B/s"
  }
]


136
137
138
139
# File 'lib/qbt_client/web_ui.rb', line 136

def torrent_list
  self.class.format :json
  self.class.get('/query/torrents').parsed_response
end

#trackers(torrent_hash) ⇒ Object

Get tracker data for a torrent

Example response:

[
  {
    "msg"=>"",
    "num_peers"=>"0",
    "status"=>"Working",
    "url"=>"udp://open.demonii.com:1337"},
  {
    "msg"=>"",
    "num_peers"=>"0",
    "status"=>"Not contacted yet",
    "url"=>"udp://tracker.coppersurfer.tk:6969"},
  {
    "msg"=>"",
    "num_peers"=>"0",
    "status"=>"Not contacted yet",
    "url"=>"udp://tracker.leechers-paradise.org:6969"},
  {
    "msg"=>"",
    "num_peers"=>"0",
    "status"=>"Not contacted yet",
    "url"=>"udp://exodus.desync.com:6969"}
]


203
204
205
206
# File 'lib/qbt_client/web_ui.rb', line 203

def trackers torrent_hash
  self.class.format :json
  self.class.get('/query/propertiesTrackers/' + torrent_hash).parsed_response
end

#transfer_infoObject

Get application transfer info

Example response:

{
  "dl_info"=>"D: 0 B/s/s - T: 657.8 KiB",
  "up_info"=>"U: 0 B/s/s - T: 0 B"
}


254
255
256
257
# File 'lib/qbt_client/web_ui.rb', line 254

def transfer_info
  self.class.format :json
  self.class.get('/query/transferInfo').parsed_response
end

#upload_limit(torrent_hash) ⇒ Object

Get a torrent’s upload limit

A limit of 0 means unlimited.

Returns an integer (bytes)



641
642
643
644
645
646
647
648
649
650
651
# File 'lib/qbt_client/web_ui.rb', line 641

def upload_limit torrent_hash
  self.class.format :json

  options = {
    body: "hashes=#{torrent_hash}"
  }

  self.class
    .post('/command/getTorrentsUpLimit', options)
    .parsed_response[torrent_hash]
end