Skip to content

Instantly share code, notes, and snippets.

@randomnoob
Created April 28, 2018 01:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save randomnoob/d0fda938418fc4baa894ff774a75230e to your computer and use it in GitHub Desktop.
Save randomnoob/d0fda938418fc4baa894ff774a75230e to your computer and use it in GitHub Desktop.
addon
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, TimeoutException, StaleElementReferenceException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
import csv
import requests
class FacebookBot(webdriver.Chrome):
"""Main class for browsing facebook"""
def __init__(self, pathToWebdriver='/usr/local/bin/chromedriver'):
self.pathToWebdriver = pathToWebdriver
options = webdriver.ChromeOptions()
options.add_argument('headless')
webdriver.Chrome.__init__(self, executable_path=pathToWebdriver, chrome_options=options)
def get(self, url):
"""The make the driver go to the url but reformat the url if is for facebook page"""
super().get(url)
def login(self, email, password):
"""Log to facebook using email (str) and password (str)"""
url = "https://mbasic.facebook.com"
self.get(url)
email_element = self.find_element_by_name("email")
email_element.send_keys(email)
pass_element = self.find_element_by_name("pass")
pass_element.send_keys(password)
pass_element.send_keys(Keys.ENTER)
""" Bypass facebook OneClick Login """
if self.find_element_by_class_name("bi"):
self.find_element_by_class_name("bp").click()
try:
self.find_element_by_name("xc_message")
print("Logged in")
return True
except NoSuchElementException as e:
print("Fail to login",e)
return False
def getImage(FacebookBot, postfbid, image_url):
try:
FacebookBot.get(image_url)
realurl = FacebookBot.current_url
img_data = requests.get(realurl).content
with open(postfbid + '.jpg', 'wb') as handler:
handler.write(img_data)
print ("Saved " + str(realurl))
return True
except:
return False
def getDirect(imageViewer):
base = "https://mbasic.facebook.com/photo/view_full_size/?fbid="
imagefbid = imageViewer.split("=")[1][:-3]
return (base+imagefbid)
#return base
def scanImage(oneBot, fileHandle):
for row in csv.reader(fileHandle, delimiter=","):
postfbid = row[0]
if row[5] == "" or row[5] == "Multimedia Links":
continue
elif len(eval(row[5])) == 1:
print ("Row 5 " + row[5])
photourl = getDirect(eval(row[5])[0])
image = getImage(oneBot, postfbid, photourl)
print ("Saved 1 item named " + str(postfbid))
elif len(eval(row[5])) > 1:
for i in range(len(eval(row[5]))):
row5list = eval(row[5])
print ("R5 list")
print (row5list)
photourl = getDirect(eval(row[5])[i])
if i == 0:
image = getImage(oneBot, postfbid, photourl)
image = getImage(oneBot, postfbid + "." + str(i), photourl)
print ("Saved 1 item in a sequence")
else:
print ("Missed 1 item")
bot=FacebookBot()
bot.set_page_load_timeout(20)
bot.login("user","pass")
dataFile = open("data.csv", "r")
scanImage(bot, dataFile)
#print (getImage(bot, '1', getDirect("https://mbasic.facebook.com/photo.php?fbid=149612982546044&id=100024920294893&set=gm.2098194463747012&refid=18&__tn__=EH-R")))
#print (getDirect("https://mbasic.facebook.com/photo.php?fbid=149612982546044&id=100024920294893&set=gm.2098194463747012&refid=18&__tn__=EH-R"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment