Created
January 2, 2013 17:07
-
-
Save brianjlandau/4436164 to your computer and use it in GitHub Desktop.
This will go through all the gifs for the past year in one campfire room (back to the first of the current year) and calculate the most commonly posted gifs. It does this by grabbing the URL of the gif, getting the data and MD5ing it for comparison.
This file contains 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 'rubygems' | |
require 'set' | |
require 'digest/md5' | |
require 'open-uri' | |
require 'tinder' | |
require 'active_support/all' | |
require 'awesome_print' | |
class CampfireGifCounter | |
attr_reader :campfire, :room, :results | |
def initialize(subdomain, token, room) | |
@campfire = Tinder::Campfire.new subdomain, :token => token | |
@room = campfire.find_room_by_name(room) | |
@results = {} | |
end | |
def top_gifs | |
find_gifs | |
results.sort_by{|k,v| v[:count] }.reverse.take(25) | |
end | |
private | |
def find_gifs | |
@results = {} | |
(Date.today.beginning_of_year..Date.today).each do |date| | |
gifs_for_date(date).each {|m| | |
digest = existing_digest_for_url?(m[:message]) | |
if digest | |
append_to_existing_digest(digest, m[:message]) | |
else | |
digest = md5_url(m[:message]) | |
if digest && results[digest] | |
append_to_existing_digest(digest, m[:message]) | |
elsif digest | |
create_new_digest_entry(digest, m[:message]) | |
end | |
end | |
} | |
end | |
end | |
def gifs_for_date(date) | |
ap date | |
room.transcript(date).select do |m| | |
m[:message] =~ /\Ahttps?\:\/\/.+\.gif/ | |
end | |
end | |
def md5_url(url) | |
Digest::MD5.digest(open(url).read) rescue nil | |
end | |
def create_new_digest_entry(digest, url) | |
results[digest] = { | |
:urls => Set.new([url]), | |
:count => 1 | |
} | |
end | |
def append_to_existing_digest(digest, url) | |
results[digest][:count] += 1 | |
results[digest][:urls] << url | |
end | |
def existing_digest_for_url?(url) | |
results.each do |digest, data| | |
return digest if data[:urls].include?(url) | |
end | |
return false | |
end | |
end | |
gif_counter = CampfireGifCounter.new('SUBDOMAIN', 'TOKEN', "ROOM NAME") | |
ap gif_counter.top_gifs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment