Class: FacebookScrapper::Scrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/facebook_scrapper/scrapper.rb

Instance Method Summary collapse

Constructor Details

#initializeScrapper

Returns a new instance of Scrapper.



5
6
7
8
# File 'lib/facebook_scrapper/scrapper.rb', line 5

def initialize
  @driver = Selenium::WebDriver.for :chrome
  @logged_in = false
end

Instance Method Details

#get_posts_from_group_url(url, keywords = []) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/facebook_scrapper/scrapper.rb', line 59

def get_posts_from_group_url(url, keywords = [])
  get(url)
  posts = []
  all_posts = @driver.find_element(id: "m_group_stories_container").find_elements(css: "div[role='article']")
  all_posts.each do |raw_post|
    new_post = get_post_object(raw_post, keywords)
    posts.push(new_post) if new_post
  end
  puts "Found #{posts.length} posts"
  return posts
end

#get_posts_from_home(keywords = []) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/facebook_scrapper/scrapper.rb', line 71

def get_posts_from_home(keywords = [])
  get("https://mbasic.facebook.com")
  posts = []
  all_posts = @driver.find_elements(css: "div[role='article']")
  all_posts.each do |raw_post|
    new_post = get_post_object(raw_post, keywords)
    posts.push(new_post) if new_post
  end
  puts "Found #{posts.length} posts"
  return posts
end

#is_logged_in?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/facebook_scrapper/scrapper.rb', line 10

def is_logged_in?
  @logged_in
end

#login(email, password) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/facebook_scrapper/scrapper.rb', line 14

def (email, password)
  url = "https://mbasic.facebook.com"
  get(url)
  email_box = @driver.find_element(name: "email")
  email_box.send_keys(email)
  password_box = @driver.find_element(name: "pass")
  password_box.send_keys(password)
  password_box.submit
  # Bypass facebook OneClick Login
  if @driver.find_element(class: "bi")
    @driver.find_element(class: "bp").click()
  end
  begin
    @driver.find_element(name: "xc_message")
    puts "Logged in"
    @logged_in = true
    return true
  rescue Selenium::WebDriver::Error::NoSuchElementError => e
    body = @driver.find_element(tag_name: "body").text
    if (body.include?("Enter login code to continue"))
      puts "You 2 factor is turned on. Authenticate it and try again"
    else
      puts "Failed to login"
      @driver.save_screenshot("login_failed.png")
    end
    return false
  end
end

#logoutObject



43
44
# File 'lib/facebook_scrapper/scrapper.rb', line 43

def logout
end

#post_in_group(group_url, text) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/facebook_scrapper/scrapper.rb', line 83

def post_in_group(group_url, text)
  get(group_url)
  begin
    text_box = @driver.find_element(name: "xc_message")
  rescue Selenium::WebDriver::Error::NoSuchElementError
    @driver.save_screenshot("no_group_found.png")
    puts "Group url dosnt exist"
    return false
  end
  text_box.send_keys(text)
  text_box.submit
  return true
end

#write_post_to_url(url, text) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/facebook_scrapper/scrapper.rb', line 46

def write_post_to_url(url, text)
  begin
    get(url)
    textBox = @driver.find_element(name: "xc_message")
    textBox.send_keys(text)
    textbox.submit
    return true
  rescue => e
    puts "Failed to post in #{url} for error of #{e}"
    return false
  end
end