Description
These are some helper methods to get you along in life. Use wisely, young one.
Public Instance methods
connect (host=nil)
Connect somewhere to Facebook! Will either make a new @http or try to change the Host header to portray where we were trying to go.
fb.connect 'emerson.facebook.com'
# File facebook_bot/helper.rb, line 78 78: def connect host=nil 79: #should we try to connect to our default_host? 80: #changes on a network to network basis. 81: #www if you're on a regional network. 82: if host.nil? && @default_host 83: host = @default_host 84: elsif host.nil? 85: host = 'www.facebook.com' 86: end 87: #if we havent connected yet, or we're trying to connect to upload. 88: #upload requires its own connection, it wont accept the Host trick that's 89: #happenin' below. 90: if @http.nil? || host=='upload.facebook.com' 91: host = 'www.facebook.com' unless host == 'upload.facebook.com' 92: @http = Net::HTTP.new(host) 93: puts "Created new connection to #{host}." 94: #looks like for some reason we're not connected to www, and we're not 95: #trying to upload, so we might as well reset to www and trick the Host. 96: elsif @http.address != 'www.facebook.com' && host != 'upload.facebook.com' 97: @http = Net::HTTP.new('www.facebook.com') 98: @opts[:headers]['Host'] = host 99: puts "Reset connection to #{host}." 100: #just change the Host header. This is so we don't have to make unnecessary 101: #connections to northeastern.facebook.com, bu.facebook.com, etc. etc. 102: #www.facebook.com should be able to handle all of these connections. 103: #Hopefully. 104: elsif @opts[:headers]['Host'] != host 105: @opts[:headers]['Host'] = host 106: puts "Changed 'Host' to #{host}." 107: end 108: end
do_http () {|| ...}
All HTTP calls should be made through here, since this method checks whether the cookie is still valid. If it‘s not, it logs back in and tries again. Example:
req = do_http { @http.get2(url, @opts[:headers]) }
# File facebook_bot/helper.rb, line 115 115: def do_http 116: req = yield 117: if req.code.to_i == 302 118: location = URI.parse(req.response['location']).path 119: if location == "" || location == "/" || location == "/login.php" 120: cookie_expired 121: retry 122: end 123: elsif req.code.to_i == 200 && req.body.include?("Your session has timed out.") 124: cookie_expired 125: retry 126: end 127: req 128: end
get_ids_from_url (url, ids)
Gets the contents of any IDs it finds on a given page. Takes an array of IDs. Returns a hash of ID information, keys being the IDs provided. Useful for getting basic information from a page, such as the verification post_form_id‘s, and any other information you so desire. Will try to extract this information from the ID, or give up: (in order) ID value, ID src, ID innerHTML.
id_info = fb.get_ids_from_url '/home.php', ['post_form_id']
# File facebook_bot/helper.rb, line 23 23: def get_ids_from_url url, ids 24: elements = {} 25: # go to the url 26: doc = hpricot_get_url url 27: 28: ids.each do |id| 29: tries = 0 30: while elements[id].nil? 31: ele = doc.at("##{id}") 32: if ele.nil? 33: puts "cannot get id #{id}" 34: elements[id] = 'unknown' 35: elsif ele.attributes['value'] 36: elements[id] = ele.attributes['value'] 37: elsif ele.attributes['src'] 38: elements[id] = ele.attributes['src'] 39: elsif ele.inner_html 40: elements[id] = ele.inner_html 41: end 42: end 43: end 44: elements 45: end
hpricot_get_url (url)
Will get a URL and parse it with the (lovely) hpricot library. Returns The parsed Hpricot object, to which you can do what you please.
doc = fb.hpricot_get_url '/home.php'
# File facebook_bot/helper.rb, line 6 6: def hpricot_get_url url 7: req = try_try_again { @http.get2(url, @opts[:headers]) } 8: 9: if req.code.to_i != 200 10: log(req) and return 11: end 12: 13: Hpricot(req.body) 14: end
log (req)
Logs a bad Net::HTTP request. Appends to a file(log.txt) and prints to the console.
# File facebook_bot/helper.rb, line 66 66: def log req 67: msg = "ERROR: #{req.code}: #{req.message}\nbody: #{req.body}\nheaders: #{req.response.to_hash.inspect}" 68: File.open('log.txt','a') do |f| 69: f.puts msg 70: end 71: #puts msg 72: end
try_try_again () {|| ...}
If at first you don‘t succeed… try try again. This function takes a block and will try(and try and try and try) that block until it finally doesn‘t raise an error. This is useful if you are getting timeouts or other such errors while grabbing some information. I use it liberally just in case I get disconnected temporarily (which is often).
# File facebook_bot/helper.rb, line 52 52: def try_try_again 53: begin 54: do_http { yield } 55: rescue Timeout::Error => err 56: puts "Timeout Error: #{err}! Retrying.." 57: retry 58: rescue Exception => exception 59: puts "Exception: #{exception.message}! Retrying.." 60: retry 61: end 62: end