#! /usr/bin/ruby require 'digest/md5' require 'net/http' require 'uri' $unknown_gravatar = 'd5fe5cbcc31cff5f8ac010db72eb000c' $email_addr_regex = /[^\[{(< @"']+@[^\]})> @"']+\.[^\]})> @"']+/ def md5(str) Digest::MD5.hexdigest str end def get(url) Net::HTTP.get URI.parse url end def gravatar(email) email_hash = md5(email) "https://secure.gravatar.com/avatar/#{email_hash}?s=512" unless \ $unknown_gravatar == md5(get('http://gravatar.com/avatar/' + email_hash)) end $seen = {} def find_gravatars(line) found = nil line.scan($email_addr_regex).each do |email| next if ($seen[email]) $seen[email] = 1 next unless (url = gravatar email) puts "#{url} #{email}" found = true end found end # if you give a bunch of non-file email addresses on the command line, try those if ARGV.select do |maybe_not_filename| true if File.file? maybe_not_filename find_gravatars maybe_not_filename end.length == 0 then # otherwise grep given files (or stdin, if none are given) for email addresses ARGF.each do |authors_line| $stderr.puts "Processing file #{ARGF.filename}" if (ARGF.file.lineno == 1) find_gravatars authors_line end end