0
votes

I need to write image parser from some website which will take images, some other info and save it to my local folder. So let's say we have image at this url : https://i.stack.imgur.com/MiqEv.jpg (this is someone's SO avatar)

So I want to to save it to local folder. Let's say to "~/test/image.png" I found this link

And I tried this in my terminal:

rails console

require 'open-uri'
  open('~/test/image.jpg', 'wb') do
  |file| file << open('https://i.stack.imgur.com/MiqEv.jpg').read
end

As you can see my home/test folder is empty enter image description here

And I got this output from console #<File:~/test/image.jpg (closed)>enter image description here

What do I do?

Also I tried this:

require 'open-uri'
download = open('https://i.stack.imgur.com/MiqEv.jpg')
IO.copy_stream(download, '~/test/image.jpg')

And got this output:

=> #https://i.stack.imgur.com/MiqEv.jpg>, @meta={"date"=>"Fri, 06 May 2016 11:58:05 GMT", "content-type"=>"image/jpeg", "content-length"=>"4276", "connection"=>"keep-alive", "set-cookie"=>"__cfduid=d7f982c0742bf40e58d626659c65a88841462535885; expires=Sat, 06-May-17 11:58:05 GMT; path=/; domain=.imgur.com; HttpOnly", "cache-control"=>"public, max-age=315360000", "etag"=>"\"b75caf18a116034fc3541978de7bac5b\"", "expires"=>"Mon, 04 May 2026 11:58:05 GMT", "last-modified"=>"Thu, 28 Mar 2013 15:05:35 GMT", "x-amz-version-id"=>"TP7cpPcf0jWeW2t1gUz66VXYlevddAYh", "cf-cache-status"=>"HIT", "vary"=>"Accept-Encoding", "server"=>"cloudflare-nginx", "cf-ray"=>"29ec4221fdbf267e-FRA"}, @metas={"date"=>["Fri, 06 May 2016 11:58:05 GMT"], "content-type"=>["image/jpeg"], "content-length"=>["4276"], "connection"=>["keep-alive"], "set-cookie"=>["__cfduid=d7f982c0742bf40e58d626659c65a88841462535885; expires=Sat, 06-May-17 11:58:05 GMT; path=/; domain=.imgur.com; HttpOnly"], "cache-control"=>["public, max-age=315360000"], "etag"=>["\"b75caf18a116034fc3541978de7bac5b\""], "expires"=>["Mon, 04 May 2026 11:58:05 GMT"], "last-modified"=>["Thu, 28 Mar 2013 15:05:35 GMT"], "x-amz-version-id"=>["TP7cpPcf0jWeW2t1gUz66VXYlevddAYh"], "cf-cache-status"=>["HIT"], "vary"=>["Accept-Encoding"], "server"=>["cloudflare-nginx"], "cf-ray"=>["29ec4221fdbf267e-FRA"]}, @status=["200", "OK"]> 2.3.0 :244 > IO.copy_stream(download, '~/test/image.jpg') => 4276 enter image description here

But my folder is still empty. What do I do??

2
I don't know if it suits your use case, but the carrierwave gem makes this quite easy for users (if you want users to upload their avatar for instance). github.com/carrierwaveuploader/…Matt
@Matt I don't have any users at all. =) I am writing an image parseruser2950593
try replacing ~ with /home/yourusername/Mladen Jablanović
@MladenJablanović It says Errno::ENOENT: No such file or directory @ rb_sysopen - home/jonstark/test/image.jpguser2950593
@MladenJablanović sorry I wrote home/jonstark and it gave me an error. Then I wrote /home/jonstark/ with / before home and it worked! write it as an answer! i will accept it=)user2950593

2 Answers

1
votes

The problem is that the file is not getting created. If you create the file using File.open or open and then execute the `IO.copy_stream' it will work.

Also ~/ doesn't work in ruby. You have to specify the whole path.

require 'open-uri'
download = open('https://i.stack.imgur.com/MiqEv.jpg')
open('/home/user/image.jpg', 'w')
IO.copy_stream(download, '~/test/image.jpg')

If you want a directory to be created as well, you will have to user Dir.mkdir. If you want to create nested directories use FileUtils::mkdir_p. If it is difficult to use either, I would suggest using system 'mkdir dirname' or system 'mkdir -p dir1/dir2/dir3'

Dir.mkdir '/home/user/test' # doesnt work for nested folder creation

require 'fileutils'
FileUtils::mkdir_p '/home/user/test1/test2' # for nested

system 'mkdir '~/test'   # Unix command for directory creation
system 'mkdir -p '~/test1/test2'   # Unix command for nested directory

Hope this helps

0
votes

If you are using Ubuntu, could you just use the wget?

You can use both wget 'https://i.stack.imgur.com/MiqEv.jpg' and system("wget 'https://i.stack.imgur.com/MiqEv.jpg'"). Or system("wget 'https://i.stack.imgur.com/MiqEv.jpg' > /your/path

Note: for the first command you need to wrap you command into the ` signs. This will cause ruby to call a system command.

Also, consider using /home/your_name instead of just ~. Also notice the leading / slash.