Created
September 23, 2010 06:10
-
-
Save rtanglao/593211 to your computer and use it in GitHub Desktop.
pick 300 random 640x480 flickr pics and save them to current directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require 'json' | |
| require 'pp' | |
| require 'fileutils' | |
| require 'getoptlong' | |
| # requires serialized flickr json file to be $stdin or specified on the command line and then | |
| # copies flickr "Z" files which are 640x480 to the current directory | |
| # for 5MP original somehow height is 481 (flickr bug?) so 481 height jpgs are ignored | |
| # 1 parameter is required: "-d directory" where directory is the directory with the Z files | |
| opts = GetoptLong.new( | |
| ["--directory", "-d", GetoptLong::REQUIRED_ARGUMENT ] | |
| ) | |
| flickr_z_dir = nil | |
| opts.each do |opt, arg| | |
| flickr_z_dir = Dir.new(arg) | |
| end | |
| num_480 = 0 | |
| potential_files_to_copy = [] | |
| srand | |
| ARGF.each_line do |line| | |
| serializedJSON = line | |
| flickr_data_page = JSON.parse(serializedJSON) | |
| total = flickr_data_page["photos"]["total"].to_i | |
| page = flickr_data_page["photos"]["page"].to_i | |
| total_pages = flickr_data_page["photos"]["pages"].to_i | |
| if page == total_pages | |
| limit = total % 250 | |
| else | |
| limit = 250 | |
| end | |
| 0.upto limit-1 do |photo_index| | |
| if flickr_data_page["photos"]["photo"][photo_index].has_key?("url_z") | |
| height = flickr_data_page["photos"]["photo"][photo_index]["height_z"].to_i | |
| if height == 480 | |
| url = flickr_data_page["photos"]["photo"][photo_index]["url_z"] | |
| $stderr.printf("url:%s\n", url) | |
| filename = url.split(/\?/).first.split(/\//).last | |
| filepath = flickr_z_dir.path + filename | |
| $stderr.printf("filename with directory:%s\n", filename) | |
| potential_files_to_copy[num_480] = filepath | |
| num_480 += 1 | |
| end | |
| end | |
| end | |
| $stderr.printf("num480:%d\n", num_480) | |
| end | |
| random_nums = [] | |
| random_nums_index = 0 | |
| 0.upto 299 do |i| | |
| r = rand(num_480) | |
| while random_nums.index(r) | |
| r = rand(num_480) | |
| end | |
| random_nums[random_nums_index] = r | |
| random_nums_index += 1 | |
| file_to_copy = potential_files_to_copy[r] | |
| $stderr.printf("picked random:%d, filepath:%s\n", r, file_to_copy) | |
| FileUtils.cp file_to_copy, '.', :verbose => true | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
sample output:
http://dl.dropbox.com/u/361757/Z_300_RANDOM_1.zip
http://dl.dropbox.com/u/361757/Z_300_RANDOM_2.zip
http://dl.dropbox.com/u/361757/Z_300_RANDOM_3.zip