Class: LikeBomb

Inherits:
Object
  • Object
show all
Defined in:
lib/like_bomb.rb

Overview

lb.post_likes(unliked_statuses)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ LikeBomb

TODO:

Use watir-webdriver to automate grabbing of keys from Graph API site.

Default String constructor

Examples:

Invalid key which will in turn throw an exception at runtime

LikeBomb.new("HI") # "ArgumentError: The provided key is invalid..."

Valid key created by Billy Bob and used for a LikeBomb

lb = LikeBomb.new("<VALID KEY>") 
lb.fb_name # "Billy Bob"

Parameters:

  • key (String)

    A valid Facebook Graph API key which can be generated here, please consult README for additional directions.

Raises:

  • (ArgumentError)

    if the provided Facebook Graph API key is invalid due to insufficent permissions



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/like_bomb.rb', line 33

def initialize(key)
  begin
    permissions = Oj.load(Typhoeus::Request.get(
      "https://graph.facebook.com/me/permissions?access_token=#{key}").body)["data"].first
    if is_valid?(permissions) 
      @key = key
      @fb_name = Oj.load(Typhoeus::Request.get(
          "https://graph.facebook.com/me?access_token=#{@key}").body)["name"] 
    else
      raise ArgumentError , 
        "The provided key is invalid, please consult the README for how to generate a valid API key"
    end
   rescue
      raise ArgumentError , 
        "The provided key is invalid, please consult the README for how to generate a valid API key"
   end
end

Instance Attribute Details

#fb_nameObject (readonly)

Returns the value of attribute fb_name.



20
21
22
# File 'lib/like_bomb.rb', line 20

def fb_name
  @fb_name
end

#keyObject (readonly)

Returns the value of attribute key.



20
21
22
# File 'lib/like_bomb.rb', line 20

def key
  @key
end

Instance Method Details

#get_friendsHash<String,String>

A method that can be used to retrieve user_id’s of your friends. You will need to use these ids to get status and/or photo object_ids

Examples:

Let’s grab Spongebob’s friends

spongebob_lb = LikeBomb.new("<SPONGEBOB_KEY>")
spongebob_lb.get_friends # "{"Patrick" => "111231321221", Squidward" => "22212221222"}"

Returns:

  • (Hash<String,String>)

    a Hash containing all your friend’s names as String keys and their unique ID as the corresponding String value



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/like_bomb.rb', line 58

def get_friends
  res = Typhoeus::Request.get(
             "https://graph.facebook.com/me/friends?access_token=#{@key}")
  unless res.nil?  
    json_data = Oj.load res.body
    friend_hash = Hash.new
    json_data["data"].each do |x|
      friend_hash[x["name"]] = x["id"]
    end
    return friend_hash
  end
end

#get_photos(user_id) ⇒ Hash<Symbol,Array<String>>

Grabs all photos of a specific user when provided a user_id

Examples:

A typical use of get_photos

patrick_id = spongebob_lb.get_friends["Patrick"]
spongebob_lb.get_photos(patrick_id)[:all] # ["11123312333","33143444444",.....]

Parameters:

  • user_id (String)

    A user_id of one of your friends

Returns:

  • (Hash<Symbol,Array<String>>)

    a Hash of Symbol, Array<String> pairs.

    Three keys exist, :all, :liked, :cooled, which represent all photo ids, photo ids that are liked and photo ids that have been commented with “Cool!”. These keys exist as to prevent double posting on a specific photo. Negations :not_liked and :not_cooled also exist

See Also:



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/like_bomb.rb', line 120

def get_photos(user_id)
  result_hash = Hash.new
  result_hash[:all] = []
  result_hash[:liked] = []
  result_hash[:cooled] = []
  all_links = []
  res = Typhoeus::Request.get("https://graph.facebook.com/#{user_id}/photos?access_token=#{@key}")
  take_photos = Oj.load(res.body)["data"].empty? ? false : true
  while take_photos
    unless Oj.load(res.body)["data"].nil?
      Oj.load(res.body)["data"].each do |photo|
        unless result_hash[:all].include? photo["id"]
          result_hash[:all].push photo["id"]
          result_hash[:liked].push photo["id"] if liked?(photo)
          result_hash[:cooled].push photo["id"] if cooled?(photo)
        end
      end
    end
    begin
      next_url = Oj.load(res.body)["paging"]["next"]
    rescue NoMethodError
      break
    end
    if all_links.include? next_url || next_url.nil?
      take_photos = false
    else
      all_links.push next_url
      res = Typhoeus::Request.get("#{Oj.load(res.body)["paging"]["next"]}?access_token=#{@key}")
    end
  end
  result_hash[:not_liked] = result_hash[:all] - result_hash[:liked]
  result_hash[:not_cooled] = result_hash[:all] - result_hash[:cooled]
  result_hash
end

#get_statuses(user_id) ⇒ Hash<Symbol,Array<String>>

Grabs all statuses of a specific user when provided a user_id

Examples:

A typical use of get_statuses

patrick_id = spongebob_lb.get_friends["Patrick"]
spongebob_lb.get_statuses(patrick_id)[:all] # ["11123212333","33343444444",.....]

Parameters:

  • user_id (String)

    A user_id of one of your friends

Returns:

  • (Hash<Symbol,Array<String>>)

    a Hash of Symbol, Array<String> pairs.

    Three keys exist, :all, :liked, :cooled, which represent all status ids, status ids that are liked and status ids that have been commented with “Cool!”. These keys exist as to prevent double posting on a specific status. Negations :not_liked and :not_cooled also exist

See Also:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/like_bomb.rb', line 81

def get_statuses(user_id)
  result_hash = Hash.new
  result_hash[:all] = []
  result_hash[:liked] = []
  result_hash[:cooled] = []
  all_links = []
  res = Typhoeus::Request.get("https://graph.facebook.com/#{user_id}/statuses?access_token=#{@key}")
  take_statuses = Oj.load(res.body)["data"].empty? ? false : true
  while take_statuses
    Oj.load(res.body)["data"].each do |status|
      unless result_hash[:all].include? status["id"]
        result_hash[:all].push status["id"]
        result_hash[:liked].push status["id"] if liked?(status)
        result_hash[:cooled].push status["id"] if cooled?(status)
      end
    end
    next_url = Oj.load(res.body)["paging"]["next"]
    if all_links.include? next_url || next_url.nil?
      take_statuses = false
    else
      all_links.push next_url
      res = Typhoeus::Request.get("#{Oj.load(res.body)["paging"]["next"]}?access_token=#{@key}")
    end
  end
  result_hash[:not_liked] = result_hash[:all] - result_hash[:liked]
  result_hash[:not_cooled] = result_hash[:all] - result_hash[:cooled]
  result_hash
end

#post_cools(obj_ids) ⇒ Object

Posts the comment “Cool!” on any provided object_id

Examples:

common example

unliked_ids = lb.get_photos(friend_hash["Gumby"])[:not_cooled] # Grab all previously uncommented photos
lb.post_cools(unliked_ids) # Comment "Cool!" on all of Gumby's previously uncommented photos

Parameters:

  • obj_ids (Array<String>)

    an Array of Strings representing object_ids of statuses and/or photos



178
179
180
181
182
183
184
# File 'lib/like_bomb.rb', line 178

def post_cools(obj_ids)
   hydra = Typhoeus::Hydra.new
   complete_urls = obj_ids.collect{|id| "https://graph.facebook.com/#{id}/comments?access_token=#{@key}&publish_stream&message=Cool!"}   
   complete_urls.each do |url|
      Typhoeus::Request.post url
   end
end

#post_likes(obj_ids) ⇒ Object

Likes any provided object_id

Examples:

common example

unliked_ids = lb.get_statuses(friend_hash["Gumby"])[:not_liked] # Grab all previously unliked statuses
lb.post_likes(unliked_ids) # Like all of Gumby's previously unliked statuses

Parameters:

  • obj_ids (Array<String>)

    an Array of Strings representing object_ids of statuses and/or photos



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/like_bomb.rb', line 160

def post_likes(obj_ids)
   hydra = Typhoeus::Hydra.new
   complete_urls = obj_ids.collect{|id| "https://graph.facebook.com/#{id}/likes?access_token=#{@key}&publish_stream"}   
   complete_urls.each do |url|
     hydra.queue Typhoeus::Request.new(url,
                                     :method => :post,
                                     :timeout => 50000,
                                     :cache_timeout => 60)
   end
   hydra.run
end