Posted By

Affix on 06/10/12


Tagged

uTorrent


Versions (?)

uTorrent WebUI API Ruby Class


 / Published in: Ruby
 

URL: http://affix.me

uTorrent for ruby

  1. #!/usr/bin/env ruby
  2. # uTorrent WebUI API Class for Ruby
  3. # Copyright (C) 2012 Keiran "Affix" Smith
  4. # http://affix.me
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18.  
  19.  
  20. require 'rubygems'
  21. require 'json'
  22. require 'net/http'
  23.  
  24.  
  25. class Utorrent
  26.  
  27. def get_token(hostname,port,user,pass)
  28.  
  29. Net::HTTP.start(hostname, port) { |http|
  30. req = Net::HTTP::Get.new('/gui/token.html')
  31. req.basic_auth user, pass
  32. response = http.request(req)
  33. token = response.body.gsub(%r{</?[^>]+?>}, '')
  34. return token
  35. }
  36.  
  37. end
  38.  
  39. def get_torrents(hostname, port, user, pass, token)
  40. Net::HTTP.start(hostname, port) { |http|
  41. req = Net::HTTP::Get.new('/gui/?list=1&token=#{token}')
  42. req.basic_auth user, pass
  43. response = http.request(req)
  44. result = JSON.parse(response.body)
  45. torrents = result['torrents']
  46. return torrents
  47. }
  48. end
  49.  
  50. def pause_torrent(hostname, port, user, pass, token, torrent_hash)
  51. Net::HTTP.start(hostname, port) { |http|
  52. req = Net::HTTP::Get.new('/gui/?action=pause&token=#{token}&hash=#{torrent_hash}')
  53. req.basic_auth user, pass
  54. return http.request(req)
  55. }
  56.  
  57. end
  58.  
  59. def start_torrent(hostname, port, user, pass, token, torrent_hash)
  60. Net::HTTP.start(hostname, port) { |http|
  61. req = Net::HTTP::Get.new('/gui/?action=start&token=#{token}&hash=#{torrent_hash}')
  62. req.basic_auth user, pass
  63. return http.request(req)
  64. }
  65.  
  66. end
  67.  
  68. def stop_torrent(hostname, port, user, pass, token, torrent_hash)
  69. Net::HTTP.start(hostname, port) { |http|
  70. req = Net::HTTP::Get.new('/gui/?action=stop&token=#{token}&hash=#{torrent_hash}')
  71. req.basic_auth user, pass
  72. return http.request(req)
  73. }
  74.  
  75. end
  76.  
  77. def forcestart_torrent(hostname, port, user, pass, token, torrent_hash)
  78. Net::HTTP.start(hostname, port) { |http|
  79. req = Net::HTTP::Get.new('/gui/?action=forcestart&token=#{token}&hash=#{torrent_hash}')
  80. req.basic_auth user, pass
  81. return http.request(req)
  82. }
  83.  
  84. end
  85.  
  86. def unpause_torrent(hostname, port, user, pass, token, torrent_hash)
  87. Net::HTTP.start(hostname, port) { |http|
  88. req = Net::HTTP::Get.new('/gui/?action=unpause&token=#{token}&hash=#{torrent_hash}')
  89. req.basic_auth user, pass
  90. return http.request(req)
  91. }
  92.  
  93. end
  94.  
  95. def recheck_torrent(hostname, port, user, pass, token, torrent_hash)
  96. Net::HTTP.start(hostname, port) { |http|
  97. req = Net::HTTP::Get.new('/gui/?action=recheck&token=#{token}&hash=#{torrent_hash}')
  98. req.basic_auth user, pass
  99. return http.request(req)
  100. }
  101.  
  102. end
  103.  
  104. def remove_torrent(hostname, port, user, pass, token, torrent_hash)
  105. Net::HTTP.start(hostname, port) { |http|
  106. req = Net::HTTP::Get.new('/gui/?action=remove&token=#{token}&hash=#{torrent_hash}')
  107. req.basic_auth user, pass
  108. return http.request(req)
  109. }
  110.  
  111. end
  112.  
  113. def removedata_torrent(hostname, port, user, pass, token, torrent_hash)
  114. Net::HTTP.start(hostname, port) { |http|
  115. req = Net::HTTP::Get.new('/gui/?action=removedata&token=#{token}&hash=#{torrent_hash}')
  116. req.basic_auth user, pass
  117. return http.request(req)
  118. }
  119.  
  120. end
  121.  
  122. def addurl_torrent(hostname, port, user, pass, token, torrent_url)
  123. Net::HTTP.start(hostname, port) { |http|
  124. req = Net::HTTP::Get.new('/gui/?action=add-url&token=#{token}&s=#{torrent_url}')
  125. req.basic_auth user, pass
  126. return http.request(req)
  127. }
  128.  
  129. end
  130.  
  131. def addfile_torrent(hostname, port, user, pass, token, torrent_file)
  132. File.open(torrent_file) do |torrent|
  133. req = Net::HTTP::Post::Multipart.new "/gui/?action=add-file",
  134. "file" => UploadIO.new(torrent, "application/x-bittorrent")
  135. res = Net::HTTP.start(hostname, port) do |http|
  136. http.basic_auth user, pass
  137. return http.request(req)
  138. end
  139.  
  140. end
  141. end
  142. end

Report this snippet  

You need to login to post a comment.