Class: Faceoff

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#agentObject

Mechanize agent



225
226
227
# File 'lib/faceoff.rb', line 225

def agent
  @agent
end

#emailObject

User email



228
229
230
# File 'lib/faceoff.rb', line 228

def email
  @email
end

#passwordObject

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. email, password
  inst = self.new email, password
  inst.
  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
  options = {}

  opts = OptionParser.new do |opt|
    opt.program_name = File.basename $0
    opt.version = Faceoff::VERSION
    opt.release = nil

    opt.banner = <<-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| options[a] = true }
    end

    opt.on('-a', '--albums [RANGE]', 'Retrieve albums') do |range|
      options['albums'] = parse_range range
    end

    opt.on('-f', '--friends [RANGE]', 'Retrieve contacts') do |range|
      options['friends'] = parse_range range
    end

    opt.on('-n', '--notes [RANGE]', 'Retrieve notes') do |range|
      options['notes'] = parse_range range
    end

    opt.on('-p', '--photosofme [RANGE]', 'Retrieve photos of me') do |range|
      options['photos_of_me'] = parse_range range
    end

    opt.on('-P', '--profilepics [RANGE]', 'Retrieve profile pics') do |range|
      options['profile_pictures'] = parse_range range
    end

    opt.on('-V', '--videosofme [RANGE]', 'Retrieve videos of me') do |range|
      options['videos_of_me'] = parse_range range
    end

    opt.on('-d', '--directory PATH', 'Directory to save to') do |path|
      options['dir'] = path
    end

    opt.on('-z', '--zip [NAME]', 'Zip content when done') do |name|
      options['zip'] = name || true
    end
  end

  opts.parse! argv

  options['email'], options['password'] = argv

  options
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
  options = parse_args argv

  $stdin.sync
  input = HighLine.new $stdin

  faceoff = nil

  until faceoff do
    email    = options['email'] || input.ask("Facebook Email: ")
    password = options['password'] ||
      input.ask("Facebook Password: "){|i| i.echo = false}

    faceoff =  email, password
    options['password'] = nil unless faceoff
  end

  directory = options['dir'] || "./#{email}"

  ACTIONS.each do |action|
    next unless options[action]
    dir = File.join directory, action.capitalize.gsub("_", " ")

    faceoff.send(action, options[action]) do |item|
      name = item.name rescue item.fid
      puts "Saving #{action} '#{name}'"
      item.save! dir
    end
  end

  if options['zip']
    zipfile = String === options['zip'] ? options['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.

Returns:

  • (Boolean)


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.cookie_jar.cookies(url).select{|c| c.name == "c_user" }.first
end

#loginObject

Login to facebook.



291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/faceoff.rb', line 291

def 
  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