HomePage RecentChanges Contattami Random Page Le mie foto Il mio Blog RSS feed

DeviantArt Gallery Parse.rb

#!/usr/bin/ruby

require 'open-uri'
require 'hpricot'

username = 'YOUR_USER'
url = 'http://' << username << '.deviantart.com/gallery/'
max_images = 4 # total will be max_images + 1

deviantart = Hpricot(open( url ))

images = []

(deviantart/"span.shadow"/:a).each do |image|
  break if images.length > max_images

  # link
  link = image.attributes['href']

  # skip if not image link
  next if not link =~ /^http/

  # remove search attributes
  link.gsub!(/\?.+$/, '')

  # title
  title = image.attributes['title']
  title.gsub!(/\ on\ /, '(')
  title << ')'

  # image
  img = image.search('img')[0].attributes['src']
  h = image.search('img')[0].attributes['height']
  w = image.search('img')[0].attributes['width']

  htmlstr =  '<a href="'    + link
  htmlstr << '"><img src="' + img
  htmlstr << '" height="'   + h
  htmlstr << '" width="'    + w
  htmlstr << '"></a><br />' + title
  htmlstr << '<br /><br />'

  images.push( htmlstr )

end

puts images.to_s