torrents 1.0.5 → 1.0.6

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/.gitignore CHANGED
@@ -2,4 +2,5 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
- authentication/*
5
+ authentication/*
6
+ tmp/*
data/Isolate ADDED
@@ -0,0 +1,13 @@
1
+ env :production do
2
+ gem "rest-client"
3
+ gem "nokogiri"
4
+ gem "rchardet19"
5
+ gem "classify", "~> 0.0.3"
6
+ gem "movie_searcher", "~> 0.1.4"
7
+ gem "undertexter", "~> 0.1.5"
8
+ end
9
+
10
+ env :development do
11
+ gem "rspec"
12
+ gem "isolate"
13
+ end
@@ -1,6 +1,7 @@
1
1
  $:.push File.expand_path("../../lib/torrents", __FILE__)
2
2
  $:.push File.expand_path("../../lib/torrents/trackers", __FILE__)
3
3
 
4
+ require "isolate/now"
4
5
  require 'rest_client'
5
6
  require 'nokogiri'
6
7
  require 'torrents/container'
@@ -15,16 +15,17 @@ module Container
15
15
  include Trackers
16
16
  # Downloads the URL, returns an empty string if an error occurred
17
17
  # Here we try to convert the downloaded content to UTF8,
18
- # if we"re at least 60% sure that the content that was downloaded actally is was we think
19
- # The timeout is set to 10 seconds, after that time, an empty string will be returned
18
+ # if we're at least 80% sure that the content that was downloaded actally is was we think.
19
+ # The timeout is set to 10 seconds, after that time, an empty string will be returned.
20
20
  # {url} (String) The URL to download
21
21
  def download(url)
22
22
  begin
23
23
  data = RestClient.get self.url_cleaner(url), {:timeout => 10, :cookies => @cookies}
24
- cd = CharDet.detect(data)
25
- return (cd["confidence"] > 0.9) ? (Iconv.conv(cd["encoding"] + "//IGNORE", "UTF-8", data) rescue data) : data
26
- rescue
27
- self.error("Something when wrong when trying to fetch #{url}", $!)
24
+ cd = CharDet.detect(data, silent: true)
25
+ raise Exception.new("The confidence level for #{url} is to low: #{cd.confidence}") if not cd.encoding.to_s.match(/^UTF(-)?8$/) and cd.confidence < 0.8
26
+ return Iconv.conv(cd.encoding + "//IGNORE", "UTF-8", data) rescue data
27
+ rescue Exception => error
28
+ self.error("Something when wrong when trying to fetch #{url}", error)
28
29
  end
29
30
 
30
31
  # The default value, if {RestClient} for some reason craches (like wrong encoding or a timeout)
@@ -5,16 +5,45 @@ describe Container::Shared do
5
5
  @shared = Container::Shared.new
6
6
  end
7
7
  context "the download method" do
8
+ def rest_client(data = "123")
9
+ RestClient.should_receive(:get).with("http://example.com", {:timeout => 10, :cookies => nil}).exactly(1).times.and_return(data)
10
+ end
11
+
12
+ def char_det(encoding, confidence)
13
+ CharDet.should_receive(:detect).with("123", silent: true).and_return(Struct.new(:encoding, :confidence).new(encoding, confidence))
14
+ end
15
+
8
16
  it "should return an empty string when trying to download an empty or non valid URL" do
9
17
  @shared.download("non_working_url").should be_empty
10
18
  @shared.download(nil).should be_empty
11
19
  end
12
20
 
13
21
  it "should return the content of the site if called with the right url" do
14
- RestClient.should_receive(:get).with("http://example.com", {:timeout => 10, :cookies => nil}).exactly(1).times.and_return("123")
22
+ rest_client
15
23
  @shared.download("http://example.com").should eq("123")
16
24
  end
17
- end
25
+
26
+ it "should return an empty string when the confidence level is to low. encoding = Latin 1" do
27
+ rest_client; char_det("Latin 1", 0.6)
28
+ @shared.download("http://example.com").should be_empty
29
+ end
30
+
31
+ it "should not return an empty string when the confidence level is to low. encoding = UTF8" do
32
+ rest_client; char_det("UTF8", 0.6)
33
+ @shared.download("http://example.com").should eq("123")
34
+ end
35
+
36
+ it "should not return an empty string when the confidence level is okey. encoding = UTF-8" do
37
+ rest_client; char_det("UTF-8", 0.9)
38
+ @shared.download("http://example.com").should eq("123")
39
+ end
40
+
41
+ it "should try to encode the string if the confidence level is okey and the string isn't UTF8" do
42
+ rest_client("123"); char_det("Latin 1", 0.9)
43
+ Iconv.should_receive(:conv).with("Latin 1//IGNORE", "UTF-8", "123").and_return("my value")
44
+ @shared.download("http://example.com").should eq("my value")
45
+ end
46
+ end
18
47
 
19
48
  context "the error method" do
20
49
  it "should have a error method" do
@@ -1,8 +1,9 @@
1
- require 'rspec'
2
- require 'torrents'
3
- require 'torrents/container'
4
- require 'uri'
5
- require 'yaml'
1
+ require "isolate/now"
2
+ require "rspec"
3
+ require "torrents"
4
+ require "torrents/container"
5
+ require "uri"
6
+ require "yaml"
6
7
 
7
8
  RSpec.configure do |config|
8
9
  config.mock_with :rspec
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "torrents"
6
- s.version = "1.0.5"
6
+ s.version = "1.0.6"
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Linus Oleander"]
9
9
  s.email = ["linus@oleander.nu"]
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
28
28
  s.add_dependency('undertexter', '~> 0.1.5')
29
29
 
30
30
  s.add_development_dependency('rspec')
31
+ s.add_development_dependency('isolate')
31
32
 
32
33
  s.required_ruby_version = '>= 1.9.0'
33
34
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: torrents
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.5
5
+ version: 1.0.6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Linus Oleander
@@ -90,6 +90,17 @@ dependencies:
90
90
  version: "0"
91
91
  type: :development
92
92
  version_requirements: *id007
93
+ - !ruby/object:Gem::Dependency
94
+ name: isolate
95
+ prerelease: false
96
+ requirement: &id008 !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: "0"
102
+ type: :development
103
+ version_requirements: *id008
93
104
  description: "Search and download torrents from your favorite bittorrent tracker using Ruby 1.9. \n Get information like; subtitles, movie information from IMDB (actors, grade, original title, length, trailers and so on.), direct download link to the torrent.\n "
94
105
  email:
95
106
  - linus@oleander.nu
@@ -103,6 +114,7 @@ files:
103
114
  - .gitignore
104
115
  - .rspec
105
116
  - Gemfile
117
+ - Isolate
106
118
  - README.md
107
119
  - Rakefile
108
120
  - lib/torrents.rb