osdb 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 92d5c8afe4f667570f34fc70eb58f92fc18b8992
4
+ data.tar.gz: e9d13b0eef33b5575c8c3e7cf0e63f856331587e
5
+ SHA512:
6
+ metadata.gz: 5ab8e63b010b4351cbf20b678456d5ae68a41e240eafc6ee6444d5102b8378b19886374b8ea342a6d9398f8a40401dce341e608dc25e8df8310aa37a06e9a80d
7
+ data.tar.gz: 4d3db1f8c1e04e7adc99e490f7d8bba4d5df4d889ef76f310649f85f105e8e1fbbc17c3a31a75d7ff417c86bd9044fac63189e858f6eacbf4af2aca29e400432
data/bin/getsub CHANGED
@@ -86,6 +86,7 @@ class GetSub
86
86
  end
87
87
 
88
88
  def report_exception(exception)
89
+ puts
89
90
  puts "Something crashed."
90
91
  puts "Feel free to report the error here: https://github.com/byroot/ruby-osdb/issues"
91
92
  puts "With the following debug informations:"
@@ -132,39 +133,24 @@ class GetSub
132
133
 
133
134
  def server
134
135
  @server ||= OSDb::Server.new(
135
- :host => 'api.opensubtitles.org',
136
- :path => '/xml-rpc',
137
136
  :timeout => 90,
138
137
  :useragent => "SubDownloader 2.0.10" # register useragent ? WTF ? too boring.
139
138
  )
140
139
  end
141
140
 
142
141
  def download_sub!(sub, movie_file)
143
- sub_path = movie_file.sub_path(sub.format)
144
- download!(sub.url, sub_path)
145
- end
146
-
147
- def curl_available?
148
- %x{ curl --version 2> /dev/null > /dev/null }
149
- $?.success?
150
- end
151
-
152
- def wget_available?
153
- %x{ wget --version 2> /dev/null > /dev/null }
154
- $?.success?
155
- end
142
+ local_path = movie_file.sub_path(sub.format)
143
+ print "* download #{sub.url} to #{local_path} ... "
144
+ content = sub.body
145
+ unless content
146
+ puts "failed"
147
+ return
148
+ end
156
149
 
157
- def download!(url, local_path)
158
- # TODO: use Net::HTTP and ZLib to avoid shell dependency
159
- puts "* download #{url} to #{local_path}"
160
- if curl_available?
161
- %x{ curl -s '#{url}' | gunzip > "#{local_path}" }
162
- elsif wget_available?
163
- %x{ wget -O - -q '#{url}' | gunzip > "#{local_path}"}
164
- else
165
- puts "Can't found any curl or wget please install one of them or manualy download your sub"
166
- puts url
150
+ File.open(movie_file.sub_path(sub.format), 'w+') do |file|
151
+ file.write(content)
167
152
  end
153
+ puts "done"
168
154
  end
169
155
 
170
156
  end
@@ -2,6 +2,8 @@ require 'xmlrpc/client'
2
2
 
3
3
  module OSDb
4
4
  base_path = File.expand_path(File.dirname(__FILE__) + '/osdb')
5
+ require "#{base_path}/xmlrpc_monkey_patch"
6
+
5
7
  autoload :Finder, "#{base_path}/finder"
6
8
  autoload :Language, "#{base_path}/language"
7
9
  autoload :Movie, "#{base_path}/movie"
@@ -9,12 +9,21 @@ module OSDb
9
9
 
10
10
  attr_reader :username, :password, :language, :useragent, :client
11
11
 
12
+ CLIENT_ARGS = [:host, :path, :port, :proxy_host, :proxy_port, :http_user, :http_password, :use_ssl, :timeout]
13
+
14
+ DEFAULT_OPTIONS = {
15
+ :host => 'api.opensubtitles.org',
16
+ :path => '/xml-rpc',
17
+ :timeout => 10
18
+ }.freeze
19
+
12
20
  def initialize(options={})
13
21
  @username = options[:username] || ''
14
22
  @password = options[:password] || ''
15
23
  @language = options[:language] || 'eng'
16
24
  @useragent = options[:useragent] || 'ruby-osdb v0.1'
17
- @client = ::XMLRPC::Client.new(*options.values_at(:host, :path, :port, :proxy_host, :proxy_port, :http_user, :http_password, :use_ssl, :timeout))
25
+ options = DEFAULT_OPTIONS.merge(options)
26
+ @client = ::XMLRPC::Client.new(*options.values_at(*CLIENT_ARGS))
18
27
  end
19
28
 
20
29
  def token
@@ -1,4 +1,7 @@
1
1
  require 'uri'
2
+ require 'net/http'
3
+ require 'zlib'
4
+ require 'stringio'
2
5
 
3
6
  module OSDb
4
7
 
@@ -35,6 +38,18 @@ module OSDb
35
38
  user_ranks.empty? ? 1 : 2
36
39
  end
37
40
 
41
+ def body
42
+ @body ||= fetch_body
43
+ end
44
+
45
+ def fetch_body
46
+ StringIO.open do |buffer|
47
+ buffer.write(Net::HTTP.get(url))
48
+ buffer.rewind
49
+ return Zlib::GzipReader.new(buffer).read
50
+ end
51
+ end
52
+
38
53
  end
39
54
 
40
55
  end
@@ -1,3 +1,3 @@
1
1
  module OSDb
2
- VERSION = '0.1.2'
2
+ VERSION = '0.1.3'
3
3
  end
@@ -0,0 +1,88 @@
1
+ # OpenSubtitle.org return invalid content-length that mek the stdlib xmlrpc client raise
2
+ # This is a dirty monkey patch to workaround this. :'(
3
+
4
+ module XMLRPC
5
+ class Client
6
+ def do_rpc(request, async=false)
7
+ header = {
8
+ "User-Agent" => USER_AGENT,
9
+ "Content-Type" => "text/xml; charset=utf-8",
10
+ "Content-Length" => request.bytesize.to_s,
11
+ "Connection" => (async ? "close" : "keep-alive")
12
+ }
13
+
14
+ header["Cookie"] = @cookie if @cookie
15
+ header.update(@http_header_extra) if @http_header_extra
16
+
17
+ if @auth != nil
18
+ # add authorization header
19
+ header["Authorization"] = @auth
20
+ end
21
+
22
+ resp = nil
23
+ @http_last_response = nil
24
+
25
+ if async
26
+ # use a new HTTP object for each call
27
+ http = net_http(@host, @port, @proxy_host, @proxy_port)
28
+ http.use_ssl = @use_ssl if @use_ssl
29
+ http.read_timeout = @timeout
30
+ http.open_timeout = @timeout
31
+
32
+ # post request
33
+ http.start {
34
+ resp = http.request_post(@path, request, header)
35
+ }
36
+ else
37
+ # reuse the HTTP object for each call => connection alive is possible
38
+ # we must start connection explicitely first time so that http.request
39
+ # does not assume that we don't want keepalive
40
+ @http.start if not @http.started?
41
+
42
+ # post request
43
+ resp = @http.request_post(@path, request, header)
44
+ end
45
+
46
+ @http_last_response = resp
47
+
48
+ data = resp.body
49
+
50
+ if resp.code == "401"
51
+ # Authorization Required
52
+ raise "Authorization failed.\nHTTP-Error: #{resp.code} #{resp.message}"
53
+ elsif resp.code[0,1] != "2"
54
+ raise "HTTP-Error: #{resp.code} #{resp.message}"
55
+ end
56
+
57
+ # assume text/xml on instances where Content-Type header is not set
58
+ ct_expected = resp["Content-Type"] || 'text/xml'
59
+ ct = parse_content_type(ct_expected).first
60
+ if ct != "text/xml"
61
+ if ct == "text/html"
62
+ raise "Wrong content-type (received '#{ct}' but expected 'text/xml'): \n#{data}"
63
+ else
64
+ raise "Wrong content-type (received '#{ct}' but expected 'text/xml')"
65
+ end
66
+ end
67
+
68
+ expected = resp["Content-Length"] || "<unknown>"
69
+ if data.nil? or data.bytesize == 0
70
+ raise "Wrong size. Was #{data.bytesize}, should be #{expected}"
71
+ elsif expected != "<unknown>" and expected.to_i != data.bytesize and resp["Transfer-Encoding"].nil?
72
+ # HACK: here is the monkey patched line
73
+ # raise "Wrong size. Was #{data.bytesize}, should be #{expected}"
74
+ end
75
+
76
+ set_cookies = resp.get_fields("Set-Cookie")
77
+ if set_cookies and !set_cookies.empty?
78
+ require 'webrick/cookie'
79
+ @cookie = set_cookies.collect do |set_cookie|
80
+ cookie = WEBrick::Cookie.parse_set_cookie(set_cookie)
81
+ WEBrick::Cookie.new(cookie.name, cookie.value).to_s
82
+ end.join("; ")
83
+ end
84
+
85
+ return data
86
+ end
87
+ end
88
+ end
metadata CHANGED
@@ -1,52 +1,46 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: osdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
5
- prerelease:
4
+ version: 0.1.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - Jean Boussier
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-01-27 00:00:00.000000000 Z
11
+ date: 2013-08-14 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rake
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rspec
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: webmock
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - ~>
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - ~>
60
53
  - !ruby/object:Gem::Version
@@ -62,7 +55,6 @@ dependencies:
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: vcr
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
59
  - - ~>
68
60
  - !ruby/object:Gem::Version
@@ -70,7 +62,6 @@ dependencies:
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
66
  - - ~>
76
67
  - !ruby/object:Gem::Version
@@ -110,6 +101,7 @@ files:
110
101
  - lib/osdb/sub.rb
111
102
  - lib/osdb/subtitle_finder.rb
112
103
  - lib/osdb/version.rb
104
+ - lib/osdb/xmlrpc_monkey_patch.rb
113
105
  - osdb.gemspec
114
106
  - spec/fixtures/http/check_movie_hash.yml
115
107
  - spec/fixtures/http/get_imdb_movie_details.yml
@@ -127,33 +119,26 @@ files:
127
119
  - spec/spec_helper.rb
128
120
  homepage: http://github.com/byroot/ruby-osdb
129
121
  licenses: []
122
+ metadata: {}
130
123
  post_install_message:
131
124
  rdoc_options: []
132
125
  require_paths:
133
126
  - lib
134
127
  required_ruby_version: !ruby/object:Gem::Requirement
135
- none: false
136
128
  requirements:
137
- - - ! '>='
129
+ - - '>='
138
130
  - !ruby/object:Gem::Version
139
131
  version: '0'
140
- segments:
141
- - 0
142
- hash: -4594447033621309387
143
132
  required_rubygems_version: !ruby/object:Gem::Requirement
144
- none: false
145
133
  requirements:
146
- - - ! '>='
134
+ - - '>='
147
135
  - !ruby/object:Gem::Version
148
136
  version: '0'
149
- segments:
150
- - 0
151
- hash: -4594447033621309387
152
137
  requirements: []
153
138
  rubyforge_project: osdb
154
- rubygems_version: 1.8.24
139
+ rubygems_version: 2.0.3
155
140
  signing_key:
156
- specification_version: 3
141
+ specification_version: 4
157
142
  summary: Ruby library to access OSDb services like OpenSubtitles.org
158
143
  test_files:
159
144
  - spec/fixtures/http/check_movie_hash.yml