Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Like and Comment on every 'Happy Birthday' post on your Facebook feed at once.

View birthday_liker.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
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.