Module | FacebookGroups |
In: |
facebook_bot/groups.rb
|
Does that whole group finding and joining thing. You know. You can use these two functions together to join groups. Like so:
fb.join_group(fb.find_group('puppy'))
Finds a random group based on a search term. If there is only one result, great, it grabs that group id. Otherwise, it will choose a random group out of the results and return that group id. NOTE: This searches global groups only, currently. So, like, not within your network.
fb.find_group 'sheep' # will return something like '200040300'
# File facebook_bot/groups.rb, line 11 11: def find_group term 12: login 13: doc = hpricot_get_url("/s.php?q=#{term}&n=0&k=20010&s=0") 14: # figure out the number of results 15: begin 16: num_results = doc.at("//li[@class='current']//a").inner_html 17: rescue 18: return -1 # we failed to find any matching groups 19: end 20: if num_results =~ /500\+/ 21: max = 500 22: else 23: max = num_results.split(' ')[0].to_i 24: end 25: 26: #if there's more than one result, we'll want to find one random group 27: #out of the set 28: #TODO: what about 2-10 results? 29: if max > 1 30: #grab a random page of results from 0 to max 31: random_page = rand(max) 32: #re-search! 33: doc = hpricot_get_url("/s.php?q=#{term}&n=0&k=20010&s=#{random_page}") 34: end 35: 36: #pick a random group that we can join (ones that we can join have onclicks) 37: group = doc.search("//ul[@class='actionspro']/li/a[@onclick]").random 38: 39: #figure out its group id 40: group.attributes['onclick'] =~ /var dialog_(\d+) /i 41: $1 42: end
Joins a group given a group_id.
fb.join_group '3400030020' fb.join_group fb.find_group('cats')
# File facebook_bot/groups.rb, line 47 47: def join_group group_id 48: login 49: 50: #find a post_form_id, any old one will do. 51: post_form_id = get_ids_from_url("/home.php", ['post_form_id'])['post_form_id'] 52: 53: req = @http.post2('/ajax/group_actions_ajax.php',"gid=#{group_id}&join=1&post_form_id=#{post_form_id}",@opts[:headers]) 54: 55: #we get a 0 back on success, for whatever reason. 56: if req.body.include?('0') 57: puts "Successfully joined group ##{group_id}." 58: else 59: puts "Failed to join group ##{group_id}!" 60: log req 61: end 62: end