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.1'
- 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.
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/faceoff.rb', line 240 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
225 226 227 |
# File 'lib/faceoff.rb', line 225 def agent @agent end |
#email ⇒ Object
User email
228 229 230 |
# File 'lib/faceoff.rb', line 228 def email @email end |
#password ⇒ Object
User password
231 232 233 |
# File 'lib/faceoff.rb', line 231 def password @password end |
#profile_id(reload = false) ⇒ Object
Returns the facebook profile id. Fetches it from the page if not set.
380 381 382 383 |
# File 'lib/faceoff.rb', line 380 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.
185 186 187 188 189 190 191 192 193 |
# File 'lib/faceoff.rb', line 185 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 |
# 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', '--zip [NAME]', 'Zip content when done') do |name| ['zip'] = name || true end end opts.parse! argv ['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.
130 131 132 133 134 135 136 137 |
# File 'lib/faceoff.rb', line 130 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.
143 144 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 173 174 175 176 177 178 179 |
# File 'lib/faceoff.rb', line 143 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 directory = ['dir'] || "./#{email}" ACTIONS.each do |action| next unless [action] dir = File.join directory, action.capitalize.gsub("_", " ") faceoff.send(action, [action]) do |item| name = item.name rescue item.fid puts "Saving #{action} '#{name}'" item.save! dir end end if ['zip'] zipfile = String === ['zip'] ? ['zip'] : directory success = system "zip -u #{zipfile}.zip #{directory}/**/*" FileUtils.rm_rf directory if success end end |
.safe_save(filename, &block) ⇒ Object
Safely save a file; rename it if name exists.
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/faceoff.rb', line 199 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.
309 310 311 312 |
# File 'lib/faceoff.rb', line 309 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
# => {}
269 270 271 272 273 274 275 |
# File 'lib/faceoff.rb', line 269 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.
327 328 329 330 331 332 333 334 |
# File 'lib/faceoff.rb', line 327 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.
281 282 283 284 285 |
# File 'lib/faceoff.rb', line 281 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.
291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/faceoff.rb', line 291 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.
340 341 342 343 344 345 346 347 |
# File 'lib/faceoff.rb', line 340 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.
353 354 355 356 357 358 359 360 |
# File 'lib/faceoff.rb', line 353 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.
366 367 368 369 370 371 372 373 374 |
# File 'lib/faceoff.rb', line 366 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.
318 319 320 321 |
# File 'lib/faceoff.rb', line 318 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.
389 390 391 392 393 394 395 396 |
# File 'lib/faceoff.rb', line 389 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 |