PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/flickr.rb

https://bitbucket.org/ostros/flickrip
Ruby | 54 lines | 35 code | 12 blank | 7 comment | 2 complexity | 9330d4c4fed3ba86f9ae965e063a9720 MD5 | raw file
  1. userName = 'flickrUserName';
  2. flickrPhotosUrl = 'http://www.flickr.com/photos/'
  3. numberOfPages = "5";
  4. ##################################################
  5. require 'rubygems'
  6. require 'nokogiri'
  7. require 'open-uri'
  8. require 'FileUtils'
  9. class NextNumber
  10. def NextNumber.init
  11. @@next = 0;
  12. end
  13. def NextNumber.get
  14. @@next = @@next + 1
  15. end
  16. end
  17. url = flickrPhotosUrl + userName
  18. doc = Nokogiri::HTML(open(url))
  19. #fetching amount of pages
  20. # pages = Array.new
  21. # doc.css('div.Pages div.Paginator a').each do |link|
  22. # pages.push link.content.to_i
  23. # end
  24. # puts amountOfPages = pages.max
  25. amountOfPages = numberOfPages;
  26. NextNumber.init
  27. FileUtils.mkdir(userName.to_s)
  28. for i in 1..amountOfPages
  29. puts i
  30. puts photoUrl = url + '/page' + i.to_s + '/'
  31. photoPage = Nokogiri::HTML(open(photoUrl))
  32. photoPage.css('p.Photo span a').each do |link|
  33. temp_url = 'http://www.flickr.com' + link['href'] + '/sizes/o/'
  34. photo_page = Nokogiri::HTML(open(temp_url))
  35. photo_page.css('div.DownloadThis p img').each do |photo_remote|
  36. if photo_remote['src'].include? ".jpg"
  37. puts photo_remote['src']
  38. open(userName.to_s + '/' + NextNumber.get.to_s + ".jpg", 'wb').write(open(photo_remote['src']).read)
  39. end
  40. end
  41. end
  42. end