Gil Perez
Gil Perez

Reputation: 1125

Fetching images binary data from the CRML

Trying to fetch :all (first :item) from the CRML Media Resource. Using Estately RETS repo. Here is my ruby example file:

require 'rets'

client = Rets::Client.new({
  login_url: 'url',
  username: 'user',
  password: 'password',
  version: 'RETS/1.7.2' 
})

begin
    client.login
rescue => e
    puts 'Error: ' + e.message
    exit!
end

puts 'We connected! Lets get all the photos for a property...'


photos = client.find (:first), {
  search_type: 'Media',
  class: 'Media',
  query: '(MediaModificationTimestamp=2017-04-15+),(MediaType=Image)'
}


photo = open(photo = photos['MediaURL'])
require 'base64'
image = Base64.encode64(photo.read)


File.open('property-1.gif', 'wb') do|f|
  f.write(Base64.decode64(image))
end

puts photos.length.to_s + ' photos saved.'
client.logout

but I'm only getting one image instead of the 26 expected. Not sure also if this will be the best method of retrieving all of the images for all of the listings, after I get the first one working. Here is more information regarding this issue https://github.com/estately/rets/issues/210

Upvotes: 2

Views: 207

Answers (2)

Mark
Mark

Reputation: 776

You can try giving listing IDs comma separated to get all images of multiple listings at a time, in your query part.

photos = client.find (:all), {
search_type: 'Media',
  class: 'Media',
  query: '(ResourceRecordKeyNumeric=117562969,117562970,117562971),(MediaType=Image)'
}

Upvotes: 0

Gil Perez
Gil Perez

Reputation: 1125

require 'rets'

client = Rets::Client.new({
  login_url: 'url',
  username: 'username',
  password: 'password',
  version: 'RETS/1.7.2' 
})

begin
    client.login
rescue => e
    puts 'Error: ' + e.message
    exit!
end

puts 'We connected! Lets get all the photos for a property...'


photos = client.find (:all), {
  search_type: 'Media',
  class: 'Media',
  query: '(ResourceRecordKeyNumeric=117562969),(MediaType=Image)'
}

photos.each_with_index do |data, index|
  photo = open(photo = data['MediaURL'])
  puts data['MediaURL']
  require 'base64'
  image = Base64.encode64(photo.read)
  File.open("property-#{index.to_s}.jpg", 'wb') do |f|
    f.write(Base64.decode64(image))
  end
end


puts photos.length.to_s + ' photos saved.'
client.logout

Upvotes: 1

Related Questions