Class: Faceoff
- Inherits:
-
Object
- Object
- Faceoff
- Defined in:
- lib/faceoff.rb,
lib/faceoff/note.rb,
lib/faceoff/user.rb,
lib/faceoff/album.rb,
lib/faceoff/photo.rb,
lib/faceoff/video.rb,
lib/faceoff/pagelet.rb
Defined Under Namespace
Classes: Album, Note, Pagelet, Photo, User, Video
Constant Summary collapse
- VERSION =
Version of the gem.
'1.0.0'
- ACTIONS =
Command line actions available.
%w{albums friends notes photos_of_me profile_pictures videos_of_me}
- DEFAULT_DIR =
Default directory to save data to.
"faceoff-#{Time.now.to_i}"
Instance Attribute Summary collapse
-
#agent ⇒ Object
Mechanize agent.
-
#email ⇒ Object
User email.
-
#password ⇒ Object
User password.
-
#profile_id(reload = false) ⇒ Object
Returns the facebook profile id.
Class Method Summary collapse
-
.download(url) ⇒ Object
Download a photo from a url.
-
.login(email, password) ⇒ Object
Create a new Faceoff instance and login.
-
.parse_args(argv) ⇒ Object
Parse ARGVs.
-
.parse_range(str) ⇒ Object
Takes a range or number as a string and returns a range or integer.
-
.run(argv) ⇒ Object
Run from the command line.
-
.safe_save(filename, &block) ⇒ Object
Safely save a file; rename it if name exists.
Instance Method Summary collapse
-
#albums(reload = false, &block) ⇒ Object
Returns an array of photo albums.
-
#bracket_for(obj) ⇒ Object
Returns an options hash based on the type of input: bracket_for 5 # => => 5.
-
#friends(reload = false, &block) ⇒ Object
Returns an array of friends.
-
#initialize(email, password) ⇒ Faceoff
constructor
Instantiate with user information.
-
#logged_in? ⇒ Boolean
Check if we’re logged into facebook.
-
#login ⇒ Object
Login to facebook.
-
#notes(reload = false, &block) ⇒ Object
Returns an array of notes.
-
#photos_of_me(reload = false, &block) ⇒ Object
Returns an array of photos of me.
-
#profile_pictures(reload = false, &block) ⇒ Object
Returns an array of profile pictures photos.
-
#user(reload = false) ⇒ Object
Returns the logged in user’s User object.
-
#videos_of_me(reload = false, &block) ⇒ Object
Returns an array of videos of me.
Constructor Details
#initialize(email, password) ⇒ Faceoff
Instantiate with user information.
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/faceoff.rb', line 233 def initialize email, password @email = email @password = password @profile_id = nil @agent = Mechanize.new @agent.user_agent_alias = 'Mac Safari' @agent.redirect_ok = true @albums = nil @friends = nil @notes = nil @photos_of_me = nil @profile_pics = nil @videos_of_me = nil end |
Instance Attribute Details
#agent ⇒ Object
Mechanize agent
218 219 220 |
# File 'lib/faceoff.rb', line 218 def agent @agent end |
#email ⇒ Object
User email
221 222 223 |
# File 'lib/faceoff.rb', line 221 def email @email end |
#password ⇒ Object
User password
224 225 226 |
# File 'lib/faceoff.rb', line 224 def password @password end |
#profile_id(reload = false) ⇒ Object
Returns the facebook profile id. Fetches it from the page if not set.
373 374 375 376 |
# File 'lib/faceoff.rb', line 373 def profile_id reload=false return @profile_id if @profile_id && !reload @profile_id = $1 if @agent.current_page.body =~ /\\"user\\":(\d+)/m end |
Class Method Details
.download(url) ⇒ Object
Download a photo from a url. Pass a block to catch the data.
178 179 180 181 182 183 184 185 186 |
# File 'lib/faceoff.rb', line 178 def self.download url uri = URI.parse url resp = Net::HTTP.start(uri.host) do |http| http.get uri.path end resp.body end |
.login(email, password) ⇒ Object
Create a new Faceoff instance and login.
46 47 48 49 50 |
# File 'lib/faceoff.rb', line 46 def self.login email, password inst = self.new email, password inst.login inst if inst.logged_in? end |
.parse_args(argv) ⇒ Object
Parse ARGVs
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/faceoff.rb', line 56 def self.parse_args argv = {} opts = OptionParser.new do |opt| opt.program_name = File.basename $0 opt.version = Faceoff::VERSION opt.release = nil opt. = <<-STR Faceoff is a facebook scraper that allows you to download and backup your content in a reusable format. Usage: #{opt.program_name} -h/--help #{opt.program_name} -v/--version #{opt.program_name} [facebook_email [password]] [options...] Examples: #{opt.program_name} [email protected] --all #{opt.program_name} [email protected] --albums '2..3' #{opt.program_name} [email protected] --friends --profilepics 0 Options: STR opt.on('-A', '--all', 'Retrieve all facebook data') do ACTIONS.each{|a| [a] = true } end opt.on('-a', '--albums [RANGE]', 'Retrieve albums') do |range| ['albums'] = parse_range range end opt.on('-f', '--friends [RANGE]', 'Retrieve contacts') do |range| ['friends'] = parse_range range end opt.on('-n', '--notes [RANGE]', 'Retrieve notes') do |range| ['notes'] = parse_range range end opt.on('-p', '--photosofme [RANGE]', 'Retrieve photos of me') do |range| ['photos_of_me'] = parse_range range end opt.on('-P', '--profilepics [RANGE]', 'Retrieve profile pics') do |range| ['profile_pictures'] = parse_range range end opt.on('-V', '--videosofme [RANGE]', 'Retrieve videos of me') do |range| ['videos_of_me'] = parse_range range end opt.on('-d', '--directory PATH', 'Directory to save to') do |path| ['dir'] = path end opt.on('-z', '--gzip [NAME]', 'Zip content when done') do |name| ['gzip'] = name || true end end opts.parse! argv ['dir'] ||= "." ['email'], ['password'] = argv end |
.parse_range(str) ⇒ Object
Takes a range or number as a string and returns a range or integer. Returns true if no range or int value is found.
132 133 134 135 136 137 138 139 |
# File 'lib/faceoff.rb', line 132 def self.parse_range str str = str.to_s.strip return Range.new($1.to_i, $2.to_i) if str =~ /^(\d+)\.\.(\d+)$/ return str.to_i if str == str.to_i.to_s true end |
.run(argv) ⇒ Object
Run from the command line.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/faceoff.rb', line 145 def self.run argv = parse_args argv $stdin.sync input = HighLine.new $stdin faceoff = nil until faceoff do email = ['email'] || input.ask("Facebook Email: ") password = ['password'] || input.ask("Facebook Password: "){|i| i.echo = false} faceoff = login email, password ['password'] = nil unless faceoff end ACTIONS.each do |action| next unless [action] dir = File.join ['dir'], action.capitalize.gsub("_", " ") faceoff.send(action, [action]) do |item| name = item.name rescue item.fid puts "Saving #{action} '#{name}'" item.save! dir end end end |
.safe_save(filename, &block) ⇒ Object
Safely save a file; rename it if name exists.
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/faceoff.rb', line 192 def self.safe_save filename, &block dir = File.dirname(filename) FileUtils.mkdir_p dir raise "Invalid directory #{dir}" unless File.directory? dir test_filename = filename i = 0 while File.file?(test_filename) i = i.next ext = File.extname filename test_filename = File.join File.dirname(filename), "#{File.basename(filename, ext)} (#{i})#{ext}" end filename = test_filename File.open(filename, "w+") do |f| block.call(f) if block_given? end end |
Instance Method Details
#albums(reload = false, &block) ⇒ Object
Returns an array of photo albums.
302 303 304 305 |
# File 'lib/faceoff.rb', line 302 def albums reload=false, &block return @albums if @albums && !reload @albums = Album.retrieve_all self, bracket_for(reload), &block end |
#bracket_for(obj) ⇒ Object
Returns an options hash based on the type of input:
bracket_for 5
# => {:limit => 5}
bracket_for 3..5
# => {:limit => 3, :start => 3}
bracket_for true
# => {}
262 263 264 265 266 267 268 |
# File 'lib/faceoff.rb', line 262 def bracket_for obj case obj when Fixnum then {:limit => obj} when Range then {:limit => obj.entries.length, :start => obj.first} else {} end end |
#friends(reload = false, &block) ⇒ Object
Returns an array of friends.
320 321 322 323 324 325 326 327 |
# File 'lib/faceoff.rb', line 320 def friends reload=false, &block if @friends && !reload @friends.each &block if block_given? return @friends end @friends = User.retrieve_all self, bracket_for(reload), &block end |
#logged_in? ⇒ Boolean
Check if we’re logged into facebook.
274 275 276 277 278 |
# File 'lib/faceoff.rb', line 274 def logged_in? url = URI.parse("http://www.facebook.com") #puts @agent.cookie_jar.cookies(url).inspect @agent..(url).select{|c| c.name == "c_user" }.first end |
#login ⇒ Object
Login to facebook.
284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/faceoff.rb', line 284 def login page = @agent.get("http://www.facebook.com") form = page.form_with \ :action => 'https://login.facebook.com/login.php?login_attempt=1' return unless form form.email = @email form.pass = @password page = form.submit logged_in? end |
#notes(reload = false, &block) ⇒ Object
Returns an array of notes.
333 334 335 336 337 338 339 340 |
# File 'lib/faceoff.rb', line 333 def notes reload=false, &block if @notes && !reload @notes.each &block if block_given? return @notes end @notes = Note.retrieve_all self, bracket_for(reload), &block end |
#photos_of_me(reload = false, &block) ⇒ Object
Returns an array of photos of me.
346 347 348 349 350 351 352 353 |
# File 'lib/faceoff.rb', line 346 def photos_of_me reload=false, &block if @photos_of_me && !reload @photos_of_me.each &block if block_given? return @photos_of_me end @photos_of_me = Photo.photos_of_me self, bracket_for(reload), &block end |
#profile_pictures(reload = false, &block) ⇒ Object
Returns an array of profile pictures photos.
359 360 361 362 363 364 365 366 367 |
# File 'lib/faceoff.rb', line 359 def profile_pictures reload=false, &block if @profile_pics && !reload @profile_pics.each &block if block_given? return @profile_pics end @profile_pics = Photo.photos_of_album self, :profile, bracket_for(reload), &block end |
#user(reload = false) ⇒ Object
Returns the logged in user’s User object.
311 312 313 314 |
# File 'lib/faceoff.rb', line 311 def user reload=false return @user if @user && !reload @user = User.retrieve self, profile_id end |
#videos_of_me(reload = false, &block) ⇒ Object
Returns an array of videos of me.
382 383 384 385 386 387 388 389 |
# File 'lib/faceoff.rb', line 382 def videos_of_me reload=false, &block if @videos_of_me && !reload @videos_of_me.each &block if block_given? return @videos_of_me end @videos_of_me = Video.videos_of_me self, bracket_for(reload), &block end |