Skip to content

Instantly share code, notes, and snippets.

@GantMan
Forked from t2/birthday_liker.rb
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GantMan/530231fbc720bfd14f8d to your computer and use it in GitHub Desktop.
Save GantMan/530231fbc720bfd14f8d to your computer and use it in GitHub Desktop.
require 'date'
require 'koala'
class BirthdayLiker
FACEBOOK_TOKEN = 'your_oauth_key'
BIRTHDAY_WORDS = %w(birthday bday birfday birth born hbd)
THANKS_OPTIONS = ['Thank you!', 'Thanks!', 'Appreciate it!', 'Danke!', ':)']
DATE_TIME_FORMAT = '%Y-%m-%d'
def initialize(birthdate, opts={})
@api = Koala::Facebook::API.new(FACEBOOK_TOKEN)
@uid = @api.get_object('me')['id']
@bday = DateTime.strptime(birthdate, DATE_TIME_FORMAT)
@opts = {
limit: 1000,
range: 1
}.merge!(opts)
retrieve_comments
end
def show_appreciation
@comments.each do |comment|
@api.put_like(comment['id'])
@api.put_comment(comment['id'], THANKS_OPTIONS.sample)
end
p "Liked #{@comments.length} comments. You're so kind!"
end
private
def retrieve_comments
pre_bday = (@bday - @opts[:range]).strftime(DATE_TIME_FORMAT)
post_bday = (@bday + @opts[:range] + 1).strftime(DATE_TIME_FORMAT)
@comments = @api.get_connections("me", "feed", :since => pre_bday, :until => post_bday, :limit => @opts[:limit])
filter_comments
end
def filter_comments
remove_user_comments
remove_non_birthday
remove_already_liked
end
def remove_user_comments
@comments.delete_if { |comment| comment['from']['id'] == @uid }
end
def remove_non_birthday
@comments.delete_if do |comment|
(comment['message'].downcase.gsub(/[\.,;:!\?]/, '').split & BIRTHDAY_WORDS).empty?
end
end
def remove_already_liked
@comments.delete_if do |comment|
comment.has_key?('likes') && comment['likes']['data'].any? do |like|
like['id'] == @uid
end
end
end
end
liker = BirthdayLiker.new('2015-01-06')
liker.show_appreciation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment