Class: UTorrent

Inherits:
Object
  • Object
show all
Defined in:
lib/torrentsync.rb

Instance Method Summary collapse

Constructor Details

#initialize(host, port, user = nil, pass = nil) ⇒ UTorrent

Returns a new instance of UTorrent.



61
62
63
64
65
66
# File 'lib/torrentsync.rb', line 61

def initialize(host, port, user = nil, pass = nil)
  @host = host
  @port = port
  @user = user
  @pass = pass
end

Instance Method Details

#add(torrent) ⇒ Object

TODO DRY



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/torrentsync.rb', line 87

def add(torrent)
  Net::HTTP.start(@host, @port) do |http|
    req = Net::HTTP::Get.new('/gui/token.html')
    req.basic_auth @user, @pass
    res = http.request(req)
    h = Nokogiri::HTML.parse(res.body)
    token = h.css('#token').text
    req = Net::HTTP::Post.new('/gui/?action=add-file&token=%s' % token)
    req.basic_auth @user, @pass
    req.set_content_type('multipart/form-data; boundary=myboundary')
    req.body = "--myboundary\\r\nContent-Disposition: form-data; name=\"torrent_file\"\\r\nContent-Type: application/octet-stream\\r\nContent-Transfer-Encoding: binary\\r\n\\r\n\#{torrent}\\r\n--myboundary--\\r\n"
    http.request(req)
  end
end

#listObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/torrentsync.rb', line 68

def list
  Net::HTTP.start(@host, @port) do |http|
    req = Net::HTTP::Get.new('/gui/token.html')
    req.basic_auth @user, @pass
    res = http.request(req)
    h = Nokogiri::HTML.parse(res.body)
    token = h.css('#token').text
    req = Net::HTTP::Get.new('/gui/?list=1&token=%s' % token)
    req.basic_auth @user, @pass
    res = http.request(req)
    result = JSON.parse(res.body)
    transmissionlike = result['torrents'].map do |t|
      { 'hashString' => t[0].downcase, 'name' => t[2] }
    end
    { 'arguments' => { 'torrents' => transmissionlike } }
  end
end