从CRML中获取图像二进制数据

时间:2017-04-20 20:43:49

标签: ruby rets phrets mls

尝试从CRML媒体资源中获取:all(first:item)。使用Estately RETS repo。这是我的ruby示例文件:

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

但我只得到一张图片而不是预期的26张图片。在我得到第一个工作之后,还不确定这是否是检索所有列表的所有图像的最佳方法。以下是有关此问题的更多信息https://github.com/estately/rets/issues/210

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您可以尝试以逗号分隔列表ID,以便在查询部分中一次获取多个商家信息的所有图片。

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