royw-tmdb 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
- :patch: 2
2
+ :patch: 3
3
3
  :major: 0
4
4
  :minor: 1
@@ -4,10 +4,11 @@ class TmdbImage
4
4
  # imdb_id => String IMDB ID either with or without the 'tt' prefix
5
5
  # api_key => String containing the themovieDb.com API key
6
6
  # logger => nil or logger instance
7
- def initialize(ident, api_key, logger)
7
+ def initialize(ident, api_key, logger, filespec=nil)
8
8
  @imdb_id = 'tt' + ident.gsub(/^tt/, '') unless ident.blank?
9
9
  @api_key = api_key
10
10
  @logger = OptionalLogger.new(logger)
11
+ @filespec = filespec
11
12
  end
12
13
 
13
14
  # return an Array of fanart sizes as Strings
@@ -26,7 +27,7 @@ class TmdbImage
26
27
  src_url = nil
27
28
  if fanart_sizes.include?(size)
28
29
  src_url = image_url(@imdb_id, 'fanarts', size)
29
- copy_image(src_url, dest_filespec) unless dest_filespec.nil?
30
+ copy_image(src_url, dest_filespec)
30
31
  end
31
32
  src_url
32
33
  end
@@ -37,7 +38,7 @@ class TmdbImage
37
38
  src_url = nil
38
39
  if poster_sizes.include?(size)
39
40
  src_url = image_url(@imdb_id, 'posters', size)
40
- copy_image(src_url, dest_filespec) unless dest_filespec.nil?
41
+ copy_image(src_url, dest_filespec)
41
42
  end
42
43
  src_url
43
44
  end
@@ -92,7 +93,7 @@ class TmdbImage
92
93
  # size => member of either fanart_sizes or poster_sizes
93
94
  def image_url(imdb_id, type, size)
94
95
  src_url = nil
95
- profile = TmdbProfile.first(:imdb_id => imdb_id, :api_key => @api_key, :filespec => nil, :logger => @logger)
96
+ profile = TmdbProfile.first(:imdb_id => imdb_id, :api_key => @api_key, :filespec => @filespec, :logger => @logger)
96
97
  indexes = {}
97
98
  unless profile.nil? || profile.movie.blank?
98
99
  movie = profile.movie
@@ -110,19 +111,29 @@ class TmdbImage
110
111
  end
111
112
 
112
113
  # download the fanart
114
+ # returns nil if no attempt to copy was made, 0 on error, or the image size in bytes on success
113
115
  def copy_image(src_url, dest_filespec)
114
- begin
115
- extension = File.extname(src_url)
116
- unless extension.blank?
117
- dest_filespec += extension
118
- end
119
- data = fetch(src_url.escape_unicode)
120
- File.open(dest_filespec, 'w') do |file|
121
- file.print(data)
116
+ image_size = nil
117
+ unless src_url.blank? || dest_filespec.blank?
118
+ begin
119
+ image_size = 0
120
+ extension = File.extname(src_url)
121
+ unless extension.blank?
122
+ dest_filespec += extension
123
+ end
124
+ unless File.exist?(dest_filespec) && (File.size(dest_filespec) > 0)
125
+ @logger.info { "Downloading: #{src_url}" }
126
+ data = fetch(src_url.escape_unicode)
127
+ File.open(dest_filespec, 'w') do |file|
128
+ file.print(data)
129
+ end
130
+ end
131
+ image_size = File.size(dest_filespec)
132
+ rescue Exception => e
133
+ @logger.error { "Error fetching image.\n src_url => #{src_url},\n dest_filespec => #{dest_filespec}\n #{e.to_s}" }
122
134
  end
123
- rescue Exception => e
124
- @logger.error { "Error fetching image.\n src_url => #{src_url},\n dest_filespec => #{dest_filespec}\n #{e.to_s}" }
125
135
  end
136
+ image_size
126
137
  end
127
138
 
128
139
  MAX_ATTEMPTS = 3
@@ -62,7 +62,7 @@ class TmdbProfile
62
62
 
63
63
  # return the TmdbImage for this profile
64
64
  def image
65
- TmdbImage.new(@imdb_id.gsub(/^tt/, ''), @api_key, @logger) rescue nil
65
+ TmdbImage.new(@imdb_id.gsub(/^tt/, ''), @api_key, @logger, @filespec) rescue nil
66
66
  end
67
67
 
68
68
  protected
@@ -107,6 +107,19 @@ describe "TmdbImage" do
107
107
  (File.exist?(filespec).should be_true) && (File.size(filespec).should > 0)
108
108
  end
109
109
 
110
+ # these are a couple of white box tests to show a bug where
111
+ # src_url was nil but copy_image was trying to copy from it anyway.
112
+
113
+ it "should not attempt to copy if src_url is blank" do
114
+ dest_filespec = get_temp_filename
115
+ @image.send('copy_image', nil, dest_filespec).should be_nil
116
+ end
117
+
118
+ it "should not attempt to copy if dest_filespec is blank" do
119
+ src_url = @image.send('image_url', 'tt0465234', 'fanarts', 'original')
120
+ @image.send('copy_image', src_url, nil).should be_nil
121
+ end
122
+
110
123
  def get_temp_filename
111
124
  outfile = Tempfile.new('tmdb_image_spec', TMPDIR)
112
125
  filespec = outfile.path
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: royw-tmdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roy Wright