yayimdbs 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +23 -2
  2. data/lib/yay_imdbs.rb +10 -6
  3. metadata +2 -2
data/README.md CHANGED
@@ -16,12 +16,33 @@ Features
16
16
 
17
17
  Installation
18
18
  ------------
19
- TODO
19
+
20
+ gem install yayimdbs
20
21
 
21
22
  Examples
22
23
  --------
23
- TODO
24
24
 
25
+ ruby-1.9.2-preview3 > require 'yay_imdbs'
26
+ => true
27
+ ruby-1.9.2-preview3 > YayImdbs.search_for_imdb_id('Avatar')
28
+ => "0499549"
29
+ ruby-1.9.2-preview3 > YayImdbs.search_for_imdb_id('Avatar', 2004)
30
+ => "0270841"
31
+ ruby-1.9.2-preview3 > YayImdbs.search_for_imdb_id('Avatar', nil, :tv_show)
32
+ => "0417299"
33
+ ruby-1.9.2-preview3 > info = YayImdbs.scrap_movie_info('0499549')
34
+ => {lots of stuff here}
35
+ ruby-1.9.2-preview3 > info[:title]
36
+ => "Avatar"
37
+ ruby-1.9.2-preview3 > info[:small_image]
38
+ => "http://ia.media-imdb.com/images/M/MV5BMTA3MzcxNTI2MjNeQTJeQWpwZ15BbWU3MDYwMTc0MzM@._V1._SX100_SY122_.jpg"
39
+ ruby-1.9.2-preview3 > info[:large_image]
40
+ => "http://ia.media-imdb.com/images/M/MV5BMTA3MzcxNTI2MjNeQTJeQWpwZ15BbWU3MDYwMTc0MzM@._V1._SX488_SY595_.jpg"
41
+ ruby-1.9.2-preview3 > info[:tagline]
42
+ => "Enter the World"
43
+ ruby-1.9.2-preview3 > YayImdbs.scrap_movie_info('0411008')[:episodes].first
44
+ => {"series"=>1, "episode"=>1, "title"=>"Pilot: Part 1", "date"=>Wed, 22 Sep 2004, "plot"=>"Forty-eight survivors of an airline flight originating from Australia, bound for the U.S., which crash-lands onto an unknown island 1000 miles off course, struggle to figure out a way to survive, why trying to find a way to be rescued."}
45
+
25
46
  Licence
26
47
  -------
27
48
  MIT
data/lib/yay_imdbs.rb CHANGED
@@ -1,4 +1,3 @@
1
- # encoding: utf-8
2
1
  require 'open-uri'
3
2
  require 'nokogiri'
4
3
  require 'active_support/all'
@@ -8,15 +7,15 @@ class YayImdbs
8
7
  IMDB_SEARCH_URL = IMDB_BASE_URL + 'find?s=tt&q='
9
8
  IMDB_MOVIE_URL = IMDB_BASE_URL + 'title/tt'
10
9
 
11
- STRIP_WHITESPACE = /(\s{2,}|\n|\||\302\240\302\273)/
10
+ STRIP_WHITESPACE = /(\s{2,}|\n|\||\302\240\302\273)/u
12
11
 
13
- def self.search_for_imdb_id(name, year, tv_series=false)
12
+ def self.search_for_imdb_id(name, year=nil, type=nil)
14
13
  search_results = self.search_imdb(name)
15
14
  return nil if search_results.empty?
16
15
 
17
16
  search_results.each do |result|
18
17
  # Ensure result is the correct video type
19
- next if (result[:video_type] == :tv_show) != tv_series
18
+ next if type && (result[:video_type] != type)
20
19
 
21
20
  # If no year provided just return first result
22
21
  return result[:imdb_id] if !year || result[:year] == year
@@ -78,9 +77,9 @@ class YayImdbs
78
77
  key = div.xpath(".//h5").first.inner_text.sub(':', '').downcase
79
78
  value_search = ".//div[@class = 'info-content']"
80
79
  # Try to only get text values and ignore links as some info blocks have a "click for more info" type link at the end
81
- value = div.xpath(value_search).first.children.map{|e| e.text? ? e.to_s : ''}.join.gsub(STRIP_WHITESPACE, '').strip
80
+ value = strip_whitespace div.xpath(value_search).first.children.map{|e| e.text? ? e.to_s : ''}.join
82
81
  if value.empty?
83
- value = div.xpath(value_search).first.content.gsub(STRIP_WHITESPACE, '')
82
+ value = strip_whitespace div.xpath(value_search).first.content
84
83
  end
85
84
  if key == 'release date'
86
85
  begin
@@ -184,6 +183,11 @@ class YayImdbs
184
183
  return movie_title.strip
185
184
  end
186
185
 
186
+ # Hackyness to get around ruby 1.9 encoding issue
187
+ def self.strip_whitespace(s)
188
+ s.encode('UTF-8').gsub(STRIP_WHITESPACE, '').strip
189
+ end
190
+
187
191
  def self.video_type(td)
188
192
  return :tv_show if td.content =~ /\((TV series|TV)\)/
189
193
  return :movie
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 3
9
- version: 0.1.3
8
+ - 4
9
+ version: 0.1.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Sam Cavenagh