undertexter 0.0.7 → 0.1.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/Gemfile.lock CHANGED
@@ -1,7 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- undertexter (0.0.6)
4
+ undertexter (0.1.0)
5
+ levenshteinish
6
+ mimer_plus
5
7
  nokogiri
6
8
  rest-client
7
9
 
@@ -9,7 +11,11 @@ GEM
9
11
  remote: http://rubygems.org/
10
12
  specs:
11
13
  diff-lcs (1.1.2)
14
+ hintable_levenshtein (0.0.3)
15
+ levenshteinish (0.0.1)
16
+ hintable_levenshtein
12
17
  mime-types (1.16)
18
+ mimer_plus (0.0.4)
13
19
  nokogiri (1.4.4)
14
20
  rest-client (1.6.1)
15
21
  mime-types (>= 1.16)
@@ -26,5 +32,9 @@ PLATFORMS
26
32
  ruby
27
33
 
28
34
  DEPENDENCIES
35
+ levenshteinish
36
+ mimer_plus
37
+ nokogiri
38
+ rest-client
29
39
  rspec
30
40
  undertexter!
data/README.markdown CHANGED
@@ -33,6 +33,39 @@ You can also provide an language option.
33
33
  $ Undertexter.find("tt0840361", :language => :swedish).count
34
34
  => 8
35
35
 
36
+ Download the subtitle to disk
37
+
38
+ $ Undertexter.find("tt0840361").first.download!
39
+ => "/tmp/The.Town.2010.EXTENDED.480p.BRRip.XviD-NYDIC.rar"
40
+ $ File.exists?("/tmp/The.Town.2010.EXTENDED.480p.BRRip.XviD-NYDIC.rar")
41
+ => true
42
+
43
+ You can also specify a destination folder to download the file, both relative and absolute
44
+
45
+ $ Undertexter.find("tt0840361").first.download!(:to => /some/dir)
46
+ => "/some/dir/The.Town.2010.EXTENDED.480p.BRRip.XviD-NYDIC.rar"
47
+
48
+ $ Dir.pwd
49
+ => /Users/linus/Downloads
50
+ $ Undertexter.find("tt0840361").first.download!(:to => 'my_dir')
51
+ => "/Users/linus/Downloads/my_dir/The.Town.2010.EXTENDED.480p.BRRip.XviD-NYDIC.rar"
52
+
53
+ Find the right subtitle based on the release name of the movie
54
+
55
+ $ Undertexter.find("tt0840361").based_on("The Town EXTENDED 2010 480p BRRip XviD AC3 FLAWL3SS")
56
+ => #<Subtitle:0x00000101b739d0 @cds=1, @downloads=1644, @title="The.Town.EXTENDED.2010.480p.BRRip.XviD.AC3-FLAWL3SS", @details="http://www.undertexter.se/?p=undertext&id=23752", @movie_title="The Town", @language=:swedish>
57
+
58
+ Specify how sensitive the `based_on` method should be, where `0` is a perfect match and `1` is dont care. Default is 0.4
59
+
60
+ $ Undertexter.find("tt0840361").based_on("The Town EXTENDED 2010 480p BRRip XviD AC3 FLAWL3SS", limit: 0)
61
+ => nil
62
+
63
+ $ Undertexter.find("tt0840361").based_on("The Town EXTENDED 2010 480p BRRip XviD AC3 FLAWL3SS", limit: 0.4)
64
+ => #<Subtitle:0x00000101b8d808 @cds=1, @downloads=1644, @title="The.Town.EXTENDED.2010.480p.BRRip.XviD.AC3-FLAWL3SS", @details="http://www.undertexter.se/?p=undertext&id=23752", @movie_title="The Town", @language=:swedish>
65
+
66
+ $ Undertexter.find("tt0840361").based_on("The.Town.EXTENDED.2010.480p.BRRip.XviD.AC3-FLAWL3SS", limit: 0.0)
67
+ => #<Subtitle:0x00000101b8d718 @cds=1, @downloads=1644, @title="The.Town.EXTENDED.2010.480p.BRRip.XviD.AC3-FLAWL3SS", @details="http://www.undertexter.se/?p=undertext&id=23752", @movie_title="The Town", @language=:swedish>
68
+
36
69
  If no language option is being passed to find, it will fall back to swedish
37
70
 
38
71
  What is being returned from the find method?
data/lib/subtitle.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require 'fileutils'
2
+ require 'mimer_plus'
3
+
1
4
  class Subtitle
2
5
  attr_accessor :details, :downloads, :cds, :title, :movie_title, :url
3
6
 
@@ -9,8 +12,54 @@ class Subtitle
9
12
  @language == :english ? "http://eng.undertexter.se/subtitle.php?id=#{id}" : "http://www.undertexter.se/utext.php?id=#{id}"
10
13
  end
11
14
 
15
+ # Downloading the file and saves it disk
16
+ def download!(args = {})
17
+ if args[:to].nil?
18
+ dir = "/tmp"
19
+ file_name = "#{dir}/#{generate_file_name}"
20
+ else
21
+ dir = generate_custom_file_path(args)
22
+ file_name = "#{dir}/#{generate_file_name}"
23
+ end
24
+
25
+ data = RestClient.get(self.url, :timeout => 10) rescue nil
26
+ file = File.new(file_name, 'w')
27
+ file.write(data)
28
+ file.close
29
+
30
+ type = Mimer.identify(file_name)
31
+
32
+ if type.zip?
33
+ file_ending = ".zip"
34
+ elsif type.rar?
35
+ file_ending = ".rar"
36
+ else
37
+ file_ending = ""
38
+ end
39
+
40
+ new_file_name = "#{dir}/#{title.gsub(/\s+/, '.')}#{file_ending}"
41
+
42
+ # Changing the name on the file
43
+ FileUtils.mv(file_name, new_file_name)
44
+
45
+ # I like return :)
46
+ return new_file_name
47
+ end
48
+
12
49
  private
13
50
  def id
14
51
  @details.match(/id=(\d+)/)[1]
15
52
  end
53
+
54
+ def generate_file_name
55
+ (0...30).map{65.+(rand(25)).chr}.join.downcase
56
+ end
57
+
58
+ def generate_custom_file_path(args)
59
+ # If the path is relative
60
+ args[:to] = File.expand_path(args[:to]) unless args[:to].match(/^\//)
61
+
62
+ # Makes sure that every directory structure looks the same
63
+ Dir.new(args[:to]).path
64
+ end
16
65
  end
data/lib/undertexter.rb CHANGED
@@ -4,6 +4,7 @@ require 'rest-client'
4
4
  require 'subtitle'
5
5
  require 'nokogiri'
6
6
  require 'iconv'
7
+ require 'undertexter/array'
7
8
 
8
9
  class Undertexter
9
10
  attr_accessor :raw_data, :base_details, :subtitles
@@ -0,0 +1,13 @@
1
+ require 'levenshteinish'
2
+
3
+ class Array
4
+ def based_on(string, args = {})
5
+ self.any? ? self.map do|s|
6
+ [Levenshtein.distance(s.title, string, args[:limit] || 0.4), s]
7
+ end.reject do |value|
8
+ value.first.nil?
9
+ end.sort_by do |value|
10
+ value.first
11
+ end.map(&:last).first : nil
12
+ end
13
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Array do
4
+ before(:each) do
5
+ @subtitles = Undertexter.find("tt0840361")
6
+ end
7
+
8
+ it "should have a based_on method" do
9
+ lambda{
10
+ @subtitles.based_on("some args")
11
+ }.should_not raise_error(NoMethodError)
12
+ end
13
+
14
+ it "should return the right one" do
15
+ @subtitles.based_on("The Town EXTENDED 2010 480p BRRip XviD AC3 FLAWL3SS").details.should match(/id=23752/)
16
+ end
17
+
18
+ it "should not return anything if the limit is set to low" do
19
+ @subtitles.based_on("The Town EXTENDED 2010 480p BRRip XviD AC3 FLAWL3SS", :limit => 0).should be_nil
20
+ end
21
+
22
+ it "should not return the right movie if to litle info i passed" do
23
+ @subtitles.based_on("The Town").should be_nil
24
+ end
25
+
26
+ it "should return the right instance" do
27
+ @subtitles.based_on("The Town EXTENDED 2010 480p BRRip XviD AC3 FLAWL3SS").should be_instance_of(Subtitle)
28
+ end
29
+
30
+ it "should return nil if trying to fetch an non existing imdb id" do
31
+ Undertexter.find("tt123456789").based_on("some random argument").should be_nil
32
+ end
33
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'rspec'
2
2
  require "#{File.dirname(__FILE__)}/../lib/undertexter"
3
+ require "#{File.dirname(__FILE__)}/../lib/subtitle"
3
4
 
4
5
  RSpec.configure do |config|
5
6
  config.mock_with :rspec
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+ describe Subtitle do
3
+ before(:each) do
4
+ @title = "127 Hours 2010 DVDSCR XViD-MC8"
5
+ @new_file = "/tmp/#{@title.gsub(/\s+/, '.')}.zip"
6
+
7
+ @subtitle = Subtitle.new({
8
+ :details => "http://www.undertexter.se/?p=undertext&id=23984",
9
+ :downloads => 100,
10
+ :cds => 1,
11
+ :title => @title,
12
+ :movie_title => "127 Hours (127 Timmar)",
13
+ :language => :swedish
14
+ })
15
+ end
16
+
17
+ it "should be an instance of subtitle" do
18
+ @subtitle.should be_instance_of(Subtitle)
19
+ end
20
+
21
+ it "should have the right accessors" do
22
+ @subtitle.details.should eq("http://www.undertexter.se/?p=undertext&id=23984")
23
+ @subtitle.downloads.should eq(100)
24
+ @subtitle.cds.should eq(1)
25
+ @subtitle.title.should eq("127 Hours 2010 DVDSCR XViD-MC8")
26
+ @subtitle.movie_title.should eq("127 Hours (127 Timmar)")
27
+ @subtitle.url.should eq("http://www.undertexter.se/utext.php?id=23984")
28
+ end
29
+
30
+ it "should be able to download the subtitle" do
31
+ @subtitle.download!.should eq(@new_file)
32
+ end
33
+
34
+ it "should have created an existing file" do
35
+ FileUtils.rm(@new_file)
36
+ @subtitle.download!
37
+ File.exists?(@new_file).should be_true
38
+ end
39
+
40
+ it "should be able to download the file to a specific absolute directory" do
41
+ %x{rm -r /tmp/new_dir; mkdir /tmp/new_dir}
42
+ @subtitle.download!(:to => '/tmp/new_dir')
43
+ %x{ls /tmp/new_dir}.should_not be_empty
44
+ end
45
+
46
+ it "should be able to download the file to a specific relative directory" do
47
+ folder = "#{Dir.pwd}/spec/data"
48
+
49
+ @subtitle.download!(:to => 'spec/data')
50
+
51
+ %x{ls #{folder}}.should_not be_empty
52
+
53
+ %x{cd #{folder} && rm 127.Hours.2010.DVDSCR.XViD-MC8.zip}
54
+ end
55
+ end
data/undertexter.gemspec CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "undertexter"
6
- s.version = "0.0.7"
6
+ s.version = "0.1.0"
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Linus Oleander"]
9
9
  s.email = ["linus@oleander.nu"]
@@ -20,5 +20,7 @@ Gem::Specification.new do |s|
20
20
 
21
21
  s.add_dependency('rest-client')
22
22
  s.add_dependency('nokogiri')
23
+ s.add_dependency('mimer_plus')
24
+ s.add_dependency('levenshteinish')
23
25
  s.add_development_dependency('rspec')
24
26
  end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: undertexter
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
5
- prerelease:
4
+ prerelease: false
6
5
  segments:
7
6
  - 0
7
+ - 1
8
8
  - 0
9
- - 7
10
- version: 0.0.7
9
+ version: 0.1.0
11
10
  platform: ruby
12
11
  authors:
13
12
  - Linus Oleander
@@ -26,7 +25,6 @@ dependencies:
26
25
  requirements:
27
26
  - - ">="
28
27
  - !ruby/object:Gem::Version
29
- hash: 3
30
28
  segments:
31
29
  - 0
32
30
  version: "0"
@@ -40,26 +38,50 @@ dependencies:
40
38
  requirements:
41
39
  - - ">="
42
40
  - !ruby/object:Gem::Version
43
- hash: 3
44
41
  segments:
45
42
  - 0
46
43
  version: "0"
47
44
  type: :runtime
48
45
  version_requirements: *id002
49
46
  - !ruby/object:Gem::Dependency
50
- name: rspec
47
+ name: mimer_plus
51
48
  prerelease: false
52
49
  requirement: &id003 !ruby/object:Gem::Requirement
53
50
  none: false
54
51
  requirements:
55
52
  - - ">="
56
53
  - !ruby/object:Gem::Version
57
- hash: 3
58
54
  segments:
59
55
  - 0
60
56
  version: "0"
61
- type: :development
57
+ type: :runtime
62
58
  version_requirements: *id003
59
+ - !ruby/object:Gem::Dependency
60
+ name: levenshteinish
61
+ prerelease: false
62
+ requirement: &id004 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ type: :runtime
71
+ version_requirements: *id004
72
+ - !ruby/object:Gem::Dependency
73
+ name: rspec
74
+ prerelease: false
75
+ requirement: &id005 !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ type: :development
84
+ version_requirements: *id005
63
85
  description: A subtitle search client to search for swedish subtitles on undertexter.se
64
86
  email:
65
87
  - linus@oleander.nu
@@ -78,7 +100,10 @@ files:
78
100
  - Rakefile
79
101
  - lib/subtitle.rb
80
102
  - lib/undertexter.rb
103
+ - lib/undertexter/array.rb
104
+ - spec/array_spec.rb
81
105
  - spec/spec_helper.rb
106
+ - spec/subtitle_spec.rb
82
107
  - spec/undertexter_spec.rb
83
108
  - undertexter.gemspec
84
109
  has_rdoc: true
@@ -95,7 +120,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
95
120
  requirements:
96
121
  - - ">="
97
122
  - !ruby/object:Gem::Version
98
- hash: 3
99
123
  segments:
100
124
  - 0
101
125
  version: "0"
@@ -104,17 +128,18 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
128
  requirements:
105
129
  - - ">="
106
130
  - !ruby/object:Gem::Version
107
- hash: 3
108
131
  segments:
109
132
  - 0
110
133
  version: "0"
111
134
  requirements: []
112
135
 
113
136
  rubyforge_project: undertexter
114
- rubygems_version: 1.4.2
137
+ rubygems_version: 1.3.7
115
138
  signing_key:
116
139
  specification_version: 3
117
140
  summary: A subtitle search client for undertexter.se
118
141
  test_files:
142
+ - spec/array_spec.rb
119
143
  - spec/spec_helper.rb
144
+ - spec/subtitle_spec.rb
120
145
  - spec/undertexter_spec.rb