mercredi 6 juillet 2022

not able to download image or video file from url in ruby [duplicate]

My below code snippet is not working as expected. I wanted to download the mp4 or image file from given link/URL. However my code is not working as expected. I found the similar solution over internet but it is not working. I am using ruby 3.0 version in my environment.

url = "https://images.pexels.com/photos/60597/dahlia-red-blossom-bloom-60597.jpeg?cs=srgb&dl=pexels-pixabay-60597.jpg&fm=jpg"
new_file_path = "/Users/{username}/Documents/test.jpg"
open(new_file_path, "wb") do |file| 
  file.print open(url).read
end

I am getting below error here.

test.rb:4:in `initialize': No such file or directory @ rb_sysopen - https://images.pexels.com/photos/60597/dahlia-red-blossom-bloom-60597.jpeg?cs=srgb&dl=pexels-pixabay-60597.jpg&fm=jpg (Errno::ENOENT)
    from test.rb:4:in `open'
    from test.rb:4:in `block in <main>'
    from test.rb:3:in `open'
    from test.rb:3:in `<main>'

Any suggestion here to make it work.

Aucun commentaire:

Enregistrer un commentaire