utorrent-webapi-ruby 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e1ce4177d5fd69367b27da37146883c24496d6e7
4
+ data.tar.gz: bb1d3c5272406c3e77c10e4b1402578fe21dafb8
5
+ SHA512:
6
+ metadata.gz: 5d6b38c39b3f9c10af85bcc1c2b51bb35175bc826999f67a42b1ed35387344589eefa814e9c99a685794939f03eaa6db976f3eb63726cfcb091010a30f25f097
7
+ data.tar.gz: bf068bdd3ee15df7cc36c8e220fb97c56137eef887b1b99aebd33599381d2f498e752274210e5b2ff43a3fd75938d1fe2929fa822d38b5393f505e5547520aac
@@ -0,0 +1,36 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ ## Specific to RubyMotion:
14
+ .dat*
15
+ .repl_history
16
+ build/
17
+
18
+ ## Documentation cache and generated files:
19
+ /.yardoc/
20
+ /_yardoc/
21
+ /doc/
22
+ /rdoc/
23
+
24
+ ## Environment normalization:
25
+ /.bundle/
26
+ /vendor/bundle
27
+ /lib/bundler/man/
28
+
29
+ # for a library or gem, you might want to ignore these files since the code is
30
+ # intended to run in multiple environments; otherwise, check them in:
31
+ Gemfile.lock
32
+ .ruby-version
33
+ .ruby-gemset
34
+
35
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
36
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Peter Wu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,54 @@
1
+ # utorrent-webapi-ruby
2
+
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ gem 'utorrent-webapi-ruby', git: 'git@github.com:PeterWuMC/utorrent-webapi-ruby.git'
9
+
10
+ ## Configuration
11
+
12
+ ```ruby
13
+ configuration = UTorrent::Configuration.new
14
+ configuration.url = '192.168.2.100'
15
+ configuration.port = '8085'
16
+ configuration.username = 'admin'
17
+ configuration.password = 'password'
18
+ UTorrent.configuration = configuration
19
+ ```
20
+
21
+ ## Limitation
22
+
23
+ The current version only support the following actions
24
+
25
+ * Only support HTTP
26
+ * Querying torrents [attributes](https://github.com/PeterWuMC/utorrent-webapi-ruby/blob/master/lib/u_torrent/torrent.rb#L20-L26)
27
+ * Perform the following actions to torrent: `start, stop, force_start, pause, unpause, recheck, remove, remove_data`
28
+ * Querying files within a specific torrent [attributes](https://github.com/PeterWuMC/utorrent-webapi-ruby/blob/master/lib/u_torrent/file.rb#L10-L13)
29
+ * Add new torrent using with url
30
+ * Updating a file [priority](https://github.com/PeterWuMC/utorrent-webapi-ruby/blob/master/lib/u_torrent/file.rb#L3-L8)
31
+
32
+ ## Usage
33
+
34
+ ### Query all torrents
35
+
36
+ UTorrent::Torrent.all
37
+
38
+ ### Find one torrent using HASH
39
+
40
+ UTorrent::Torrent.find(HASH)
41
+
42
+ ### Perform actions on torrent
43
+
44
+ e.g.
45
+
46
+ UTorrent::Torrent.all.first.stop
47
+
48
+ ### Query all file of a torrent
49
+
50
+ UTorrent::Torrent.all.first.files
51
+
52
+ ### Update priority of a file
53
+
54
+ UTorrent::Torrent.all.first.files.priority = 0 # Do not download
@@ -0,0 +1,27 @@
1
+ require 'cgi'
2
+ require 'uri'
3
+ require 'net/http'
4
+ require 'nokogiri'
5
+ require 'json'
6
+ require_relative 'u_torrent/configuration'
7
+ require_relative 'u_torrent/http'
8
+ require_relative 'u_torrent/base'
9
+ require_relative 'u_torrent/torrent'
10
+ require_relative 'u_torrent/file'
11
+
12
+
13
+ module UTorrent
14
+ module_function
15
+
16
+ def configuration=(configuration)
17
+ @configuration = configuration
18
+ end
19
+
20
+ def configuration
21
+ @configuration
22
+ end
23
+
24
+ def base_uri
25
+ URI.parse("#{@configuration.protocol}://#{@configuration.url}:#{@configuration.port}/gui/")
26
+ end
27
+ end
@@ -0,0 +1,22 @@
1
+ module UTorrent
2
+ module Base
3
+ attr_reader :raw_array
4
+
5
+ def initialize(array)
6
+ @raw_array = array
7
+ end
8
+
9
+ def self.included(base)
10
+ base.class_eval do
11
+ base::ATTRIBUTES.each_with_index do |attr_name, i|
12
+ unless attr_name.nil?
13
+ define_method attr_name do
14
+ @raw_array[i]
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,10 @@
1
+ module UTorrent
2
+ class Configuration
3
+ attr_accessor :url, :username, :password, :port
4
+ attr_writer :protocol
5
+
6
+ def protocol
7
+ @protocol ||= 'http'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,52 @@
1
+ module UTorrent
2
+ class File
3
+ PRIORITIES = {
4
+ 0 => "Don't Download",
5
+ 1 => 'Low Priority',
6
+ 2 => 'Normal Priority',
7
+ 3 => 'High Priority',
8
+ }
9
+
10
+ ATTRIBUTES = [
11
+ :name, :size, :downloaded, nil,
12
+ nil, nil, nil, nil, nil, nil, nil, nil, nil
13
+ ]
14
+
15
+ include UTorrent::Base
16
+
17
+ attr_reader :index, :torrent_id
18
+
19
+ def self.find_by_torrent_id_and_index(torrent_id, index)
20
+ UTorrent::Torrent.find(torrent_id).files[index]
21
+ end
22
+
23
+ def initialize(array, index, torrent_id)
24
+ super(array)
25
+ @index = index
26
+ @torrent_id = torrent_id
27
+ end
28
+
29
+ def priority_display_value
30
+ PRIORITIES[priority]
31
+ end
32
+
33
+ def priority
34
+ @raw_array[3]
35
+ end
36
+
37
+ def priority=(priority)
38
+ UTorrent::Http.get_with_authentication(
39
+ action: 'setprio',
40
+ hash: torrent_id,
41
+ f: index,
42
+ p: priority
43
+ )
44
+ refresh!
45
+ end
46
+
47
+ def refresh!
48
+ file = self.class.find_by_torrent_id_and_index(torrent_id, index)
49
+ @raw_array = file.raw_array
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,55 @@
1
+ module UTorrent
2
+ module Http
3
+ module_function
4
+
5
+ def get_with_authentication(query={})
6
+ query.merge!(token: token)
7
+ get(UTorrent.base_uri, query) do |request|
8
+ request['Cookie'] = cookie
9
+ end
10
+ end
11
+
12
+ def get(uri, query={})
13
+ uri.query = urify_query(query)
14
+ request = Net::HTTP::Get.new(uri)
15
+ request.basic_auth(
16
+ UTorrent.configuration.username,
17
+ UTorrent.configuration.password
18
+ )
19
+
20
+ yield request if block_given?
21
+
22
+ http = Net::HTTP.new(uri.host, uri.port)
23
+ http.request(request)
24
+ end
25
+
26
+ def token
27
+ @token ||= begin
28
+ generate!
29
+ @token
30
+ end
31
+ end
32
+
33
+ def cookie
34
+ @cookie ||= begin
35
+ generate!
36
+ @cookie
37
+ end
38
+ end
39
+
40
+ def generate!
41
+ uri = UTorrent.base_uri
42
+ uri.path = '/gui/token.html'
43
+
44
+ response = get(uri)
45
+
46
+ @token = Nokogiri::HTML(response.body).at_xpath('//div').content
47
+ @cookie = response['set-cookie']
48
+ end
49
+
50
+ def urify_query(query_hash)
51
+ URI.encode_www_form(query_hash.to_a)
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,86 @@
1
+ module UTorrent
2
+ class Torrent
3
+ STATUSES = {
4
+ 'Started' => 1,
5
+ 'Checking' => 2,
6
+ 'Start after check' => 4,
7
+ 'Checked' => 8,
8
+ 'Error' => 16,
9
+ 'Paused' => 32,
10
+ 'Queued' => 64,
11
+ 'Loaded' => 128,
12
+ }
13
+
14
+ ACTIONS = [
15
+ :start, :stop, :force_start,
16
+ :pause, :unpause, :recheck,
17
+ :remove, :remove_data
18
+ ]
19
+
20
+ ATTRIBUTES = [
21
+ :id, nil, :name, :size, nil, :downloaded, :uploaded, :ratio,
22
+ :upload_speed, :download_speed, :eta, :label, :peers_connected,
23
+ :peers_in_swarm, :seeds_connected, :seeds_in_swarm, :availability,
24
+ :queue_order, :remaining, nil, nil, :status, nil, nil, nil, nil,
25
+ :current_directory, nil, nil, nil
26
+ ]
27
+
28
+ include UTorrent::Base
29
+
30
+ def self.all
31
+ response = UTorrent::Http.get_with_authentication(list: 1)
32
+ torrents_array = JSON.parse(response.body)['torrents']
33
+ torrents_array.map { |torrent| self.new(torrent) }
34
+ end
35
+
36
+ def self.find(id)
37
+ all.detect { |torrent| torrent.id == id }
38
+ end
39
+
40
+ def self.add(url)
41
+ UTorrent::Http.get_with_authentication(
42
+ action: 'add-url',
43
+ s: url
44
+ )
45
+ UTorrent::Torrent.all.detect {|torrent| torrent.queue_order == UTorrent::Torrent.all.map(&:queue_order).max}
46
+ end
47
+
48
+ def statuses
49
+ status_code = @raw_array[1]
50
+ status = STATUSES.select { |_, value| status_code & value == value }
51
+ status.keys.reverse
52
+ end
53
+
54
+ def percentage
55
+ @raw_array[4].to_f / 10
56
+ end
57
+
58
+ def files
59
+ response = UTorrent::Http.get_with_authentication(action: 'getfiles', hash: id)
60
+ files_array = JSON.parse(response.body)['files'].last
61
+ files_array.each_with_index.map { |file, i| UTorrent::File.new(file, i, id) }
62
+ end
63
+
64
+ ACTIONS.each do |action|
65
+ define_method action do
66
+ execute(action.to_s.delete('_'))
67
+ true
68
+ end
69
+ end
70
+
71
+ def refresh!
72
+ torrent = self.class.find(id)
73
+ @raw_array = torrent.raw_array
74
+ end
75
+
76
+ private
77
+
78
+ def execute(action)
79
+ UTorrent::Http.get_with_authentication(
80
+ action: action,
81
+ hash: id
82
+ )
83
+ refresh!
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,3 @@
1
+ module UTorrent
2
+ VERSION = '0.0.1'.freeze
3
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'u_torrent/version'
5
+
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'utorrent-webapi-ruby'
9
+ spec.version = UTorrent::VERSION
10
+ spec.authors = ['Peter Wu']
11
+ spec.email = ['petergenius@gmail.com']
12
+ spec.description = %q{UTorrent webapi ruby library}
13
+ spec.summary = %q{Communicate to utorrent via webapi in ruby}
14
+ spec.homepage = 'https://github.com/PeterWuMC/utorrent-webapi-ruby'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files`.split($/)
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'nokogiri'
23
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: utorrent-webapi-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Peter Wu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: UTorrent webapi ruby library
28
+ email:
29
+ - petergenius@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - Gemfile
36
+ - LICENSE
37
+ - README.md
38
+ - lib/u_torrent.rb
39
+ - lib/u_torrent/base.rb
40
+ - lib/u_torrent/configuration.rb
41
+ - lib/u_torrent/file.rb
42
+ - lib/u_torrent/http.rb
43
+ - lib/u_torrent/torrent.rb
44
+ - lib/u_torrent/version.rb
45
+ - utorrent-webapi-ruby.gemspec
46
+ homepage: https://github.com/PeterWuMC/utorrent-webapi-ruby
47
+ licenses:
48
+ - MIT
49
+ metadata: {}
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 2.0.14
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Communicate to utorrent via webapi in ruby
70
+ test_files: []
71
+ has_rdoc: