Skip to content

Instantly share code, notes, and snippets.

@johndel
Last active April 25, 2018 06:34
Show Gist options
  • Save johndel/cd01a854e8bf36d9d30b44758607cf3d to your computer and use it in GitHub Desktop.
Save johndel/cd01a854e8bf36d9d30b44758607cf3d to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# require 'koala'
# Doesn't work
# @graph = Koala::Facebook::API.new("your_token")
# friends = @graph.get_connections("me", "friends")
# puts friends.count
# friends.each do |friend|
# puts friend
# end
require 'rubygems'
require 'selenium-webdriver'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql2",
:host => "localhost",
:username => "yourusername",
:password => "yourpass",
:database => "fbfriends"
)
class Friend < ActiveRecord::Base
end
class Unfriend < ActiveRecord::Base
end
class Scan < ActiveRecord::Base
end
class Inactive < ActiveRecord::Base
end
scan_time = Time.now
fb_friends = []
old_friends_here = []
inactives = []
@driver = Selenium::WebDriver.for :firefox
@driver.get "https://facebook.com"
@driver.find_element(:id, "email").click
@driver.find_element(:id, "email").clear
@driver.find_element(:id, "email").send_keys "youremail"
sleep 1
@driver.find_element(:id, "pass").click
@driver.find_element(:id, "pass").clear
@driver.find_element(:id, "pass").send_keys "yourpassword"
@driver.find_element(:id, "loginbutton").click
sleep 1
@driver.get "https://www.facebook.com/yourusername/friends"
sleep 1
el = @driver.find_element(:id, "pageFooter")
# Count for preventing lagging, so if for six loops it still the same number, then break
@friends_num = 0
@count = 0
while @driver.find_elements(:css, ".fsl.fwb.fcb").count < 1000 do
@driver.action.move_to(el).perform
sleep 1
puts @driver.find_elements(:css, "#pagelet_timeline_medley_friends .fsl.fwb.fcb").count
if @friends_num == @driver.find_elements(:css, "#pagelet_timeline_medley_friends .fsl.fwb.fcb").count && @count == 6
break
elsif @friends_num == @driver.find_elements(:css, "#pagelet_timeline_medley_friends .fsl.fwb.fcb").count
@count = @count + 1
@friends_num = @driver.find_elements(:css, "#pagelet_timeline_medley_friends .fsl.fwb.fcb").count
else
@count = 0
@friends_num = @driver.find_elements(:css, "#pagelet_timeline_medley_friends .fsl.fwb.fcb").count
end
end
# Seed fb_friends array with fetched friends from facebook
@driver.find_elements(:css, "#pagelet_timeline_medley_friends .fsl.fwb.fcb a").each do |friend|
if friend.text != ""
if friend.attribute('ajaxify')
inactives << friend.text
elsif
fb_friends << friend.text
end
end
end
# Check if old friends are still friends
Friend.all.each do |friend|
old_friends_here << friend.name unless fb_friends.include?(friend.name)
end
puts "Inactives:"
puts inactives
puts "End inactives"
# Remove inactives
inactives.each do |friend|
if friend != ''
if Inactive.where(name: friend).first.nil?
Inactive.create(name: friend, scanned_at: scan_time)
Friend.where(name: friend).first.delete rescue ''
end
end
end
puts "Friends who deleted me:"
puts old_friends_here
puts "End of friends who deleted me"
# Remove old friends
old_friends_here.each do |friend|
if friend != ''
if Unfriend.where(name: friend).first.nil?
Unfriend.create(name: friend, scanned_at: scan_time)
Friend.where(name: friend).first.delete rescue ''
end
end
end
# Check for new friends and add them
fb_friends.each do |friend|
friend_row = Friend.where(name: friend).first
Friend.create(name: friend, scanned_at: scan_time) if friend_row.nil?
end
Scan.create(scanned_at: scan_time, friends_number: fb_friends.count)
sleep 10
@driver.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment