torrentsync 0.0.0
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.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +17 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/bin/torrentsync +53 -0
- data/lib/torrentsync.rb +210 -0
- data/test/helper.rb +10 -0
- data/test/test_torrentsync.rb +6 -0
- metadata +75 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 OHASHI Hideya
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
= torrentsync
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 OHASHI Hideya. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "torrentsync"
|
8
|
+
gem.summary = %Q{sync torrents with peers}
|
9
|
+
gem.description = %Q{support Transmission and uTorrent}
|
10
|
+
gem.email = "ohachige@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/ohac/torrentsync"
|
12
|
+
gem.authors = ["OHASHI Hideya"]
|
13
|
+
gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/test_*.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "torrentsync #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
data/bin/torrentsync
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
self_file =
|
4
|
+
if File.symlink?(__FILE__)
|
5
|
+
require 'pathname'
|
6
|
+
Pathname.new(__FILE__).realpath
|
7
|
+
else
|
8
|
+
__FILE__
|
9
|
+
end
|
10
|
+
$:.unshift(File.dirname(self_file) + "/../lib")
|
11
|
+
|
12
|
+
require 'torrentsync'
|
13
|
+
require 'choice'
|
14
|
+
|
15
|
+
Choice.options do
|
16
|
+
option :sync do
|
17
|
+
short '-s'
|
18
|
+
end
|
19
|
+
option :html do
|
20
|
+
short '-h'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
c = Choice.choices
|
25
|
+
if c.sync
|
26
|
+
peers = get_peers
|
27
|
+
torrents = get_torrents(peers)
|
28
|
+
sync_torrents(peers, torrents)
|
29
|
+
end
|
30
|
+
if c.html
|
31
|
+
peers = get_peers
|
32
|
+
torrents, dead_peers = get_torrents(peers)
|
33
|
+
puts "<html>"
|
34
|
+
puts "<body>"
|
35
|
+
|
36
|
+
puts "<table>"
|
37
|
+
puts "<tr><th>Client</th><th>Host</th><th>Port</th><th>Dead?</th></tr>"
|
38
|
+
peers.each do |peer|
|
39
|
+
dead = dead_peers.include? peer
|
40
|
+
puts "<tr><td>#{peer[0]}</td><td>#{peer[1]}</td><td>#{peer[2]}</td><td>#{dead}</td></tr>"
|
41
|
+
end
|
42
|
+
puts "</table>"
|
43
|
+
|
44
|
+
puts "<table>"
|
45
|
+
puts "<tr><th>Hash</th><th>Name</th><th>Peers</th></tr>"
|
46
|
+
torrents.each do |hash, t|
|
47
|
+
puts "<tr><td>#{hash}</td><td>#{t[:name]}</td><td>#{t[:peers].size}</td></tr>"
|
48
|
+
end
|
49
|
+
puts "</table>"
|
50
|
+
|
51
|
+
puts "</body>"
|
52
|
+
puts "</html>"
|
53
|
+
end
|
data/lib/torrentsync.rb
ADDED
@@ -0,0 +1,210 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'nokogiri'
|
3
|
+
require 'net/http'
|
4
|
+
require 'json'
|
5
|
+
require 'timeout'
|
6
|
+
require 'fileutils'
|
7
|
+
require 'open-uri'
|
8
|
+
require 'base64'
|
9
|
+
|
10
|
+
class Transmission
|
11
|
+
def initialize(host, port, user = nil, pass = nil)
|
12
|
+
@host = host
|
13
|
+
@port = port
|
14
|
+
@user = user
|
15
|
+
@pass = pass
|
16
|
+
end
|
17
|
+
|
18
|
+
def list
|
19
|
+
sessionid = Net::HTTP.start(@host, @port) do |http|
|
20
|
+
res = http.get('/transmission/rpc')
|
21
|
+
h = Nokogiri::HTML.parse(res.body)
|
22
|
+
h.css('code').text.split.last
|
23
|
+
end
|
24
|
+
header = {
|
25
|
+
'Content-Type' => 'application/json',
|
26
|
+
}
|
27
|
+
header['X-Transmission-Session-Id'] = sessionid unless sessionid.nil?
|
28
|
+
sessionid = Net::HTTP.start(@host, @port) do |http|
|
29
|
+
json = {
|
30
|
+
:method => 'torrent-get',
|
31
|
+
:arguments => { :fields => [ :hashString, :id, :name ] }
|
32
|
+
}
|
33
|
+
res = http.post('/transmission/rpc', json.to_json, header)
|
34
|
+
JSON.parse(res.body)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# TODO DRY
|
39
|
+
def add(torrent)
|
40
|
+
sessionid = Net::HTTP.start(@host, @port) do |http|
|
41
|
+
res = http.get('/transmission/rpc')
|
42
|
+
h = Nokogiri::HTML.parse(res.body)
|
43
|
+
h.css('code').text.split.last
|
44
|
+
end
|
45
|
+
header = {
|
46
|
+
'Content-Type' => 'application/json',
|
47
|
+
}
|
48
|
+
header['X-Transmission-Session-Id'] = sessionid unless sessionid.nil?
|
49
|
+
Net::HTTP.start(@host, @port) do |http|
|
50
|
+
json = {
|
51
|
+
:method => 'torrent-add',
|
52
|
+
:arguments => { :metainfo => Base64::encode64(torrent) }
|
53
|
+
}
|
54
|
+
http.post('/transmission/rpc', json.to_json, header)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
class UTorrent
|
61
|
+
def initialize(host, port, user = nil, pass = nil)
|
62
|
+
@host = host
|
63
|
+
@port = port
|
64
|
+
@user = user
|
65
|
+
@pass = pass
|
66
|
+
end
|
67
|
+
|
68
|
+
def list
|
69
|
+
Net::HTTP.start(@host, @port) do |http|
|
70
|
+
req = Net::HTTP::Get.new('/gui/token.html')
|
71
|
+
req.basic_auth @user, @pass
|
72
|
+
res = http.request(req)
|
73
|
+
h = Nokogiri::HTML.parse(res.body)
|
74
|
+
token = h.css('#token').text
|
75
|
+
req = Net::HTTP::Get.new('/gui/?list=1&token=%s' % token)
|
76
|
+
req.basic_auth @user, @pass
|
77
|
+
res = http.request(req)
|
78
|
+
result = JSON.parse(res.body)
|
79
|
+
transmissionlike = result['torrents'].map do |t|
|
80
|
+
{ 'hashString' => t[0].downcase, 'name' => t[2] }
|
81
|
+
end
|
82
|
+
{ 'arguments' => { 'torrents' => transmissionlike } }
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
# TODO DRY
|
87
|
+
def add(torrent)
|
88
|
+
Net::HTTP.start(@host, @port) do |http|
|
89
|
+
req = Net::HTTP::Get.new('/gui/token.html')
|
90
|
+
req.basic_auth @user, @pass
|
91
|
+
res = http.request(req)
|
92
|
+
h = Nokogiri::HTML.parse(res.body)
|
93
|
+
token = h.css('#token').text
|
94
|
+
req = Net::HTTP::Post.new('/gui/?action=add-file&token=%s' % token)
|
95
|
+
req.basic_auth @user, @pass
|
96
|
+
req.set_content_type('multipart/form-data; boundary=myboundary')
|
97
|
+
req.body = <<EOF
|
98
|
+
--myboundary\r
|
99
|
+
Content-Disposition: form-data; name="torrent_file"\r
|
100
|
+
Content-Type: application/octet-stream\r
|
101
|
+
Content-Transfer-Encoding: binary\r
|
102
|
+
\r
|
103
|
+
#{torrent}\r
|
104
|
+
--myboundary--\r
|
105
|
+
EOF
|
106
|
+
http.request(req)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
HOME_DIR = ENV['HOME']
|
112
|
+
SETTING_DIR = "#{HOME_DIR}/.torrentsync"
|
113
|
+
PEERS_FILE = File.join(SETTING_DIR, 'peers')
|
114
|
+
TORRENTS_FILE = File.join(SETTING_DIR, 'torrents')
|
115
|
+
unless File.exist?(SETTING_DIR)
|
116
|
+
FileUtils.mkdir SETTING_DIR
|
117
|
+
open(PEERS_FILE, 'w') do |fd|
|
118
|
+
fd.puts('transmission localhost 9091')
|
119
|
+
end
|
120
|
+
open(TORRENTS_FILE, 'w') do |fd|
|
121
|
+
fd.puts("file:#{HOME_DIR}/.config/transmission/torrents")
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def find_torrent(name)
|
126
|
+
uris = File.open(TORRENTS_FILE).readlines.map(&:chomp).map{|u| URI.parse(u)}
|
127
|
+
rv = nil
|
128
|
+
uris.each do |uri|
|
129
|
+
ts = case uri.scheme
|
130
|
+
when 'file'
|
131
|
+
Dir.glob(File.join(uri.path, '*.torrent')).map{|t|File.basename(t)}
|
132
|
+
when 'http'
|
133
|
+
body = open(uri).read
|
134
|
+
h = Nokogiri::HTML.parse(body)
|
135
|
+
h.css('a').map{|a| a.text}.select{|t| /\.torrent$/ === t}
|
136
|
+
else
|
137
|
+
raise
|
138
|
+
end
|
139
|
+
rp = ts.find{|t| !t.index(name).nil?}
|
140
|
+
next if rp.nil?
|
141
|
+
uri2 = URI.parse(URI.encode("#{uri.to_s}/#{rp}"))
|
142
|
+
rv = case uri2.scheme
|
143
|
+
when 'file'
|
144
|
+
open(uri2.path){|f|f.read}
|
145
|
+
when 'http'
|
146
|
+
open(uri2){|f|f.read}
|
147
|
+
end
|
148
|
+
# TODO need to check info_hash too
|
149
|
+
break
|
150
|
+
end
|
151
|
+
rv
|
152
|
+
end
|
153
|
+
|
154
|
+
def type2class(type)
|
155
|
+
case type
|
156
|
+
when 'transmission'
|
157
|
+
Transmission
|
158
|
+
when 'utorrent'
|
159
|
+
UTorrent
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
def get_peers
|
164
|
+
File.open(PEERS_FILE).readlines.map(&:chomp).map(&:split)
|
165
|
+
end
|
166
|
+
|
167
|
+
def get_torrents(peers)
|
168
|
+
torrents = {}
|
169
|
+
dead_peers = []
|
170
|
+
peers.each do |peer|
|
171
|
+
type = peer[0]
|
172
|
+
next if type[0, 1] == '#'
|
173
|
+
host, port, user, pass = peer[1], peer[2].to_i, peer[3], peer[4]
|
174
|
+
tr = begin
|
175
|
+
timeout(2) do
|
176
|
+
type2class(type).new(host, port, user, pass).list
|
177
|
+
end
|
178
|
+
rescue TimeoutError, Errno::ECONNREFUSED
|
179
|
+
dead_peers << peer
|
180
|
+
nil
|
181
|
+
end
|
182
|
+
next if tr.nil?
|
183
|
+
tr['arguments']['torrents'].each do |t|
|
184
|
+
h = t['hashString']
|
185
|
+
torrents[h] = { :name => t['name'], :peers => [] } unless torrents.key?(h)
|
186
|
+
torrents[h][:peers] << [host, port].join(':')
|
187
|
+
end
|
188
|
+
end
|
189
|
+
[torrents, dead_peers]
|
190
|
+
end
|
191
|
+
|
192
|
+
def sync_torrents(peers, torrents)
|
193
|
+
torrents.each do |hash, t|
|
194
|
+
name = t[:name]
|
195
|
+
hps = t[:peers]
|
196
|
+
next if hps.size >= 2
|
197
|
+
body = find_torrent(name)
|
198
|
+
next if body.nil?
|
199
|
+
hps = hps.map{|hp| host, port = hp.split(':'); [host, port.to_i]}
|
200
|
+
dests = peers.select do |peer|
|
201
|
+
hps.any?{|hp| peer[1] != hp[0] && peer[2] != hp[1]}
|
202
|
+
end
|
203
|
+
dest = dests.shuffle.first
|
204
|
+
puts "mirroring: %s to %s" % [name, dest.join(',')]
|
205
|
+
type = dest[0]
|
206
|
+
host, port, user, pass = dest[1], dest[2].to_i, dest[3], dest[4]
|
207
|
+
dest = type2class(type).new(host, port, user, pass)
|
208
|
+
dest.add(body)
|
209
|
+
end
|
210
|
+
end
|
data/test/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: torrentsync
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- OHASHI Hideya
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-03-25 00:00:00 +09:00
|
13
|
+
default_executable: torrentsync
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: thoughtbot-shoulda
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: support Transmission and uTorrent
|
26
|
+
email: ohachige@gmail.com
|
27
|
+
executables:
|
28
|
+
- torrentsync
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- bin/torrentsync
|
42
|
+
- lib/torrentsync.rb
|
43
|
+
- test/helper.rb
|
44
|
+
- test/test_torrentsync.rb
|
45
|
+
has_rdoc: true
|
46
|
+
homepage: http://github.com/ohac/torrentsync
|
47
|
+
licenses: []
|
48
|
+
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options:
|
51
|
+
- --charset=UTF-8
|
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
|
+
version:
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.3.5
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: sync torrents with peers
|
73
|
+
test_files:
|
74
|
+
- test/test_torrentsync.rb
|
75
|
+
- test/helper.rb
|