Skip to content

Instantly share code, notes, and snippets.

@mrsimo
Created October 2, 2008 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrsimo/14357 to your computer and use it in GitHub Desktop.
Save mrsimo/14357 to your computer and use it in GitHub Desktop.
def get_images
require 'action_controller'
require 'action_controller/test_process.rb'
require 'mime/types' # gem install mime-types
@log = Array.new
@posts = WpaPost.find :all
@results = Array.new
# hacer un bucle recorriendo lo que sea que tiene las posibles imagenes.
@posts.each do |p|
@results = @results + (p.post_content.scan /src=\"(\S+)\"/)
end
Dir.chdir('images') do
for img in @results
`curl -O #{img}` # wget #{img} si estamos en linux
image = File.basename(img.to_s)
if File.exists? image
mimetype = MIME::Types.type_for(image)
if p = Photo.create(:uploaded_data => ActionController::TestUploadedFile.new(image,mimetype))
@log << "Imagen #{image} importada"
else
@log << "Fallo al importar #{image}"
end
else
@log << "La imagen #{img} no se ha podido descargar"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment