Search code examples
rimagecontent-type

Force content type image/* only download.file R


I'm downloading some images from links in googlesheet, the problem is that some of those links, redirect to other link eg. google drive or transfer big files, and despite this, R tries to download something and end up with a Content type 'text/html; ...' instead of telling me it found an error or nothing to download, I need a way to tell R, if you can recognize Content type 'image/*' then download, otherwise pop an error or tell me. That´s beacuse at the end of the exercise, I need to know which links gave me empty files, to correct them.

Here's some code with Content type 'image/*' and what I need to download

e = 'https://kanu.vteximg.com.br/arquivos/ids/155806/Alimento-Agility-Gold-Grandes-Adultos-para-perro-1_1.jpg'
f = 'https://barranquilla.gruposotillo.com/wp-content/uploads/2016/12/rosa-veuve.png'
download.file(url = e,'name.jpg',mode = 'wb')
download.file(url = f,'name.jpg',mode = 'wb')

Expected behaviour enter image description here

And some code with links which I need to recognize as 'bad' and not procced with the downloading

a = 'https://www.google.com/url?sa=i&url=https%3A%2F%2Fsupermercadocomunal.com%2Flicores%2F4369-tequila-jose-cuervo-reposado-375-ml-7501035010284.html&psig=AOvVaw2CyVcs9npcsOt9IZuG2s1g&ust=1592092231256000&source=images&cd=vfe&ved=0CAIQjRxqFwoTCMjO6fG7_ekCFQAAAAAdAAAAABAJ'
c = 'http://tbf.me/a/BbF0Ud'
d = 'https://drive.google.com/drive/folders/1-puT-0__ZXkEcKJpZQQPZuu4rrdNSwCC?usp=sharing'
download.file(url = a,'name.jpg',mode = 'wb')
download.file(url = c,'name.jpg',mode = 'wb')
download.file(url = d,'name.jpg',mode = 'wb')

Links I need to recognize as error: enter image description here


Solution

  • You can get the content of a link in R using httr library. Say this is your link:

    a <- "http://norwegianarts.org.uk/wp-content/uploads/2015/09/NBM-Immortal-copyright-Nuclear-Blast-e1443452460395-1440x640.jpg"
    

    Then, by using httr's get() function you'll get the link content:

    example <- GET(a)
    

    And this will return a json, rendered as a list in R, the you just move to the part where you wish to be, which in this case must be $content.