Texture from url?
-
You may be interested in this plugin effort - downlod files plugin using the cUrl library.
-
Digging up this old thread because I'm trying to do almost the same as the OP. Since the last post in this topic, Ruby has been updated so I'm hoping its more simple now.
I have been trying several methods. This code saves the image in the local documents folder so I could pick it up there etc.
require 'net/http' Net;;HTTP.start("www.google.nl") do |http| resp = http.get("/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png") open("googlelogo_color_272x92dp.png", "wb") do |file| file.write(resp.body) end end puts "Done"
Problem is, my images need a different type of url. Something like:
https://url/coordinates/?key=1234abc&index=0
This sort of link doesn't work in the code above.Another method I'm trying is loading the image in a webdialog and then grabbing it from Ruby.
The test html code (the url could easily replaced with the actual https code) is:
<!DOCTYPE html> <html><head></head><body> <img id ="webimage" src = "https://www.google.nl/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"> </body></html>
And to grab the image in ruby:
img = @wd.get_element_value('webimage')
Of course this doesn't work because the image isn't a value. I'm stuck.
Anyone has a suggestion? That would be very welcome.
-
Something like this should work:
require('open-uri') fail = false url = "http://www.google.nl/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" file = File.join(Dir.pwd, File.basename(url)) begin download = open(url) rescue Exception => error puts "#{error}" fail = true end unless fail begin File.delete(file) ### in case it exists rescue ### end begin IO.copy_stream(download, file) rescue Exception => error puts "#{error}" end end
It should work with a 'php' formatted URL, but remember that then you need to parse out the file name another way. Perhaps [assuming 'key=' gives the file name] split the URL at '?', then take [1], then split at '&', then find the array element starting "key=", then split that at '=' and then [1] is the file name given by 'key=' ?
I also saved the downloaded image file in the 'current directory', but you can of course choose any other folder you want... -
Thanks for the reply TIG. Your code does download a file but when trying to open it I get:
Decode Error! Invalid or unsupported PNG fileEdit:
I was also trying with net/http. It does work with simple files but the actual https request still fails. -
In case someone runs into the same question. It seems to work now.
require 'net/http' require 'tempfile' uri = URI('https://www.google.nl/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png') model = Sketchup.active_model materials=model.materials m = materials.add "Test Color" loc = '' begin request = Net;;HTTP;;Get.new uri Net;;HTTP.start(uri.host, uri.port, ;use_ssl => uri.scheme == 'https') do |http| http.request(request) response = http.request request # Net;;HTTPResponse object file = Tempfile.new(['foo','.png'], Dir.tmpdir, 'wb+') begin file.binmode file.write(response.body) file.flush loc = file.path.gsub("/", '\\\\\\') ensure file.close end end rescue Exception => e puts e.message end m.texture = loc m.texture.size = [m.texture.image_width.to_m, m.texture.image_height.to_m]
-
for completeness you need a conditional for osx
loc = file.path if Sketchup.platform == ;platform_osx
john
-
When a image is loaded as a web page resource, it is present in the "Temporary Internet files" folder. But some security settings might prevent the copying of a file from there to a trusted local folder.
Anyway:
require "FileUtils"
then try the
copy
method.See: http://ruby-doc.org/stdlib-2.0.0/libdoc/fileutils/rdoc/index.html
-
John and Dan, good suggestions. Thanks
-
When you say different type of URL, what do you mean? HTTPS?
-
@thomthom said:
When you say different type of URL, what do you mean? HTTPS?
Yes, something like
https://url/coordinates/?key=1234abc&index=0
-
For HTTPS connections you need to enable SSL when you create the HTTP connection.
Note that there's a bug in Ruby that shipped with SU2014-2016 that might cause problems under Windows where the shipped SSL certificates might not work.
Advertisement