Last active
March 19, 2018 20:38
-
-
Save kylekyle/9b3bf4fc192a7878af5ce2403302e334 to your computer and use it in GitHub Desktop.
Pull all of a friends Facebook posts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
require 'watir' | |
require 'seconds' | |
if ARGS.empty? | |
warn 'Please provide the Facebook user number and query string that will appear in every post (first name usually works)' | |
exit | |
end | |
b = Watir::Browser.new | |
b.goto 'https://www.facebook.com/' | |
b.driver.execute_script( "alert('Please log in')" ) rescue nil | |
Watir::Wait.until { b.title['Log In'].nil? } | |
FileUtils.mkdir ARGS.last | |
(0..Float::INFINITY).each do |n| | |
month = (Time.now - n.months).strftime '%Y-%m' | |
warn "Pulling #{month} ..." | |
query = { | |
q: ARGS.last, | |
filters_rp_author: { | |
name: "author", | |
args: ARGS.first | |
}.to_json, | |
filters_rp_creation_time: { | |
name: "creation_time", | |
args: { | |
start_month: month, | |
end_month: month | |
}.to_json | |
}.to_json | |
} | |
b.goto "https://www.facebook.com/search/top/?#{URI.encode_www_form(query)}" | |
break if b.div(text: /We couldn't find anything for/).exists? | |
until b.div(text: 'End of Results').exists? | |
b.divs(text: 'See more').select(&:visible?).map(&:click) rescue nil | |
b.driver.execute_script( "window.scrollBy(0,document.body.scrollHeight)" ) rescue nil | |
end | |
File.open(File.join(ARGS.last, month), 'w') do |file| | |
b.divs("data-testid" => "results").each do |div| | |
file.puts div.text | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment