Skip to content

Instantly share code, notes, and snippets.

@jwdomb
Created October 24, 2014 01:34
Show Gist options
  • Save jwdomb/577fc68fba099529bd56 to your computer and use it in GitHub Desktop.
Save jwdomb/577fc68fba099529bd56 to your computer and use it in GitHub Desktop.
Search The Noun Project for Public Domain Icons
#!/usr/bin/ruby
###############################################################################
# INFO
# Title:
# Search The Noun Project for Public Domain Icons
# Author:
# Joseph Dombroski <http://jwdomb.com/for/github-gist>
# Version:
# 0
# Description:
# Retrieves up to 500 (public domain) preview images from The Noun Project
# based on a keyword string; generally excludes non-public-domain results
# Usage:
# tnp-search.rb keyword-string [include-non-public-domain-results-boolean]
# Prerequisites:
# - Install gems:
# sudo gem install oauth
# sudo gem install typhoeus
#
# - Obtain API key and secret:
# https://thenounproject.com/developers/apps/
#
# - Configure API key and secret below.
###############################################################################
# CONFIGURATION
api_key = ""
api_secret = ""
###############################################################################
# CODE
require "oauth"
require "json"
# require "yaml"
require "typhoeus"
consumer = OAuth::Consumer.new(api_key, api_secret)
access_token = OAuth::AccessToken.new consumer
keywords = URI.escape(ARGV[0])
if (ARGV[1] != nil) && ARGV[1].match(/^(true|t|yes|y|1)$/i) then
endpoint = "http://api.thenounproject.com/icons/#{keywords}?limit=500"
else
endpoint = "http://api.thenounproject.com/icons/#{keywords}?limit=500&limit_to_public_domain=1"
end
response = access_token.get(endpoint)
response_obj = JSON.parse(response.body)['icons']
timestamp = Time.now.utc.strftime('%s')
Dir.mkdir("./results-#{timestamp}")
File.open("./results-#{timestamp}/_data.json",'w') {|f| f.puts response.body }
hydra = Typhoeus::Hydra.new
response_obj.each do |icon|
request = Typhoeus::Request.new(icon['preview_url'], followlocation: true)
request.on_complete do |hydra_response|
if (ARGV[1] != nil) && ARGV[1].match(/^(true|t|yes|y|1)$/i) then
File.open("./results-#{timestamp}/#{icon['id']}--#{icon['license_description']}.png",'w') {|f| f.puts hydra_response.body }
else
File.open("./results-#{timestamp}/#{icon['id']}.png",'w') {|f| f.puts hydra_response.body }
end
end
hydra.queue(request)
end
hydra.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment