Skip to content

@t2 /birthday_liker.rb
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Like and Comment on every 'Happy Birthday' post on your Facebook feed at once.
require 'date'
require 'koala'
class BirthdayLiker
FACEBOOK_TOKEN = 'your_oauth_key'
BIRTHDAY_WORDS = %w(birthday bday birfday birth born)
THANKS_OPTIONS = ['Thank you!', 'Thanks!', 'Appreciate it!']
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('2014-08-13')
liker.show_appreciation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.