-
-
Save xta/6fc6854f8bef2a4d4b9d to your computer and use it in GitHub Desktop.
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
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