-
-
Save pragnakalpdev21/80de40cdc1c849180b57bfe10ed9f087 to your computer and use it in GitHub Desktop.
This file contains 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
import time | |
import csv | |
from bs4 import BeautifulSoup | |
from datetime import date | |
from exceptionMail import sendMail | |
from selenium import webdriver | |
from selenium.webdriver.chrome.options import Options | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.common.keys import Keys | |
import pandas as pd | |
from selenium.webdriver.common.action_chains import ActionChains | |
import os | |
usr = os.getenv("EMAIL") | |
pwd = os.getenv("PASSWORD") | |
header = ['name', 'Link', 'image', 'Bithday'] | |
data = [] | |
index = [] | |
message = '' | |
connList = {} | |
birthdayList = {} | |
last_page = 0 | |
def runScript(): | |
driver = webdriver.Chrome() | |
driver.get('https://www.linkedin.com/login') | |
print("Opened Linkedin") | |
username_box = driver.find_element_by_id('username') | |
username_box.send_keys(usr) | |
print("Email Id entered") | |
time.sleep(1) | |
password_box = driver.find_element_by_id('password') | |
password_box.send_keys(pwd) | |
print("Password entered") | |
login_box = driver.find_element_by_xpath( | |
'//*[@id="organic-div"]/form/div[3]/button') | |
login_box.click() | |
time.sleep(10) | |
print('logged in') | |
time.sleep(3) | |
driver.get( | |
"https://www.linkedin.com/search/results/people/?network=%5B%22F%22%5D&origin=FACETED_SEARCH&sid=RUx") | |
time.sleep(4) | |
# Scrap the list of profiles | |
def get_profile_of_a_page(): | |
position = 0 | |
birthdayslist = driver.find_elements_by_class_name( | |
'entity-result__item') | |
for b in birthdayslist: | |
profileLink = b.find_element_by_tag_name("a") | |
name = profileLink.text | |
linkk = profileLink.get_attribute("href") | |
try: | |
imageTagFinder = b.find_element_by_tag_name("img") | |
image = imageTagFinder.get_attribute("src") | |
except: | |
image = 'https://www.pngarea.com/pngm/90/6980003_profile-icon-png-facebook-default-profile-picture-girl.png' | |
connList[position] = {'link': linkk, 'name': name, 'image': image} | |
position = position + 1 | |
# Scrolling a full-page | |
def scroll_till_end(): | |
try: | |
html = driver.find_element_by_tag_name('html') | |
html.send_keys(Keys.END) | |
except Exception as e: | |
print(str(e)) | |
# moving to next page | |
def next_page(): | |
try: | |
next_button = driver.find_element_by_class_name( | |
'artdeco-pagination__button.artdeco-pagination__button--next.artdeco-button.artdeco-button--muted.artdeco-button--icon-right.artdeco-button--1.artdeco-button--tertiary.ember-view') | |
driver.execute_script("arguments[0].click();", next_button) | |
time.sleep(4) | |
# break | |
except Exception as e: | |
print(e) | |
# Add all connection details into the CSV file | |
def addConnectionToCsv(): | |
for user in connList: | |
driver.get(connList[user]['link']) | |
time.sleep(3) | |
driver.find_element_by_class_name( | |
'ember-view.link-without-visited-state.cursor-pointer.text-heading-small.inline-block.break-words').click() | |
time.sleep(2) | |
try: | |
birthdate = driver.find_element_by_class_name( | |
'pv-contact-info__contact-item.t-14.t-black.t-normal').text | |
time.sleep(4) | |
data.append([connList[user]['name'], connList[user] | |
['link'], connList[user]['image'], birthdate]) | |
loopVar = loopVar + 1 | |
# write multiple rows | |
except Exception as e: | |
print(e) | |
with open('linkedinProfiles.csv', 'w', encoding='UTF8', newline='') as f: | |
writer = csv.writer(f) | |
# write the header | |
writer.writerow(header) | |
writer.writerows(data) | |
# checks if today is somone's birthday or not | |
def isbirthday(): | |
today = date.today() | |
# Textual month, day and year | |
d2 = today.strftime("%B %#d %Y") | |
currentDate = d2.split() | |
currentDate.pop() | |
dataOfCsv = pd.read_csv("linkedinProfiles.csv") | |
# converting column data to list | |
connectionBirthdates = dataOfCsv['Bithday'].tolist() | |
for birthday in range(len(connectionBirthdates)): | |
if connectionBirthdates[birthday].split() == currentDate: | |
index.append(birthday) | |
# Count data in CSV | |
def dataInCSV(): | |
try: | |
dataInCsv = pd.read_csv("linkedinProfiles.csv") | |
except Exception as e: | |
print(e) | |
return 0 | |
listOfData = dataInCsv['Bithday'].tolist() | |
print("totalCSVdata : ", len(listOfData)) | |
return len(listOfData) | |
# Fetch the total connection | |
def totalConnection(): | |
totConnection = driver.find_element_by_class_name( | |
'pb2.t-black--light.t-14').text | |
conn = totConnection.split() | |
conn.pop() | |
print("totalConnectiondata : ", int(conn[0])) | |
return int(conn[0]) | |
# Get the list whose birthday is today | |
def getBirthdayList(): | |
listCount = 0 | |
with open('linkedinProfiles.csv') as csv_file: | |
csv_reader = csv.reader(csv_file) | |
next(csv_reader) | |
row = list(csv_reader) | |
# fhandle = open('linkedinProfiles.csv') | |
for ind in index: | |
rowneeded = row[ind] | |
birthdayList[listCount] = { | |
'name': rowneeded[0], 'link': rowneeded[1], 'image': rowneeded[2], 'birthday': rowneeded[3]} | |
listCount = listCount + 1 | |
def pageToScrape(): | |
try: | |
response = driver.page_source | |
soup = BeautifulSoup(response, 'html.parser') | |
all_pages = soup.find_all(class_="artdeco-pagination__indicator artdeco-pagination__indicator--number ember-view") | |
global last_page | |
if len(all_pages)>0: | |
print("total pages:",len(all_pages)) | |
last_page = all_pages[-1].text | |
print("last_page",all_pages[-1].text) | |
else: | |
print("No data") | |
last_page = 1 | |
except: | |
print("Can't find the element") | |
# Check the count of connections in CSV and the actual connection count | |
if dataInCSV() == totalConnection(): | |
print("Finding the bithday of connections") | |
isbirthday() | |
getBirthdayList() | |
else: | |
print("Scraping the connections details\n") | |
pageToScrape() | |
for i in range(int(last_page)): | |
scroll_till_end() | |
get_profile_of_a_page() # profile scrapping function! | |
scroll_till_end() | |
next_page() | |
time.sleep(4) | |
addConnectionToCsv() | |
print("Finding the bithday of connections") | |
isbirthday() | |
getBirthdayList() | |
print(index) | |
# Sends the message | |
def sendMessage(): | |
global message | |
global index | |
global connList | |
if index != []: | |
message = "Birthday wished" | |
connListFromCSV = pd.read_csv("linkedinProfiles.csv") | |
BirthdayConnLinks = connListFromCSV['Link'].tolist() | |
for indexNumber in index: | |
link = BirthdayConnLinks[indexNumber] | |
# Logic to send Message | |
driver.get(link) | |
time.sleep(4) | |
msg = driver.find_element_by_link_text('Message').click() | |
time.sleep(3) | |
inbox = driver.find_element_by_class_name( | |
'msg-form__contenteditable.t-14.t-black--light.t-normal.flex-grow-1.full-height.notranslate') | |
inbox.send_keys('Happy Birthday') | |
time.sleep(3) | |
send = driver.find_element_by_class_name( | |
'msg-form__send-button.artdeco-button.artdeco-button--1') | |
ActionChains(driver).move_to_element( | |
send).click(send).perform() | |
time.sleep(3) | |
try: | |
close = driver.find_elements_by_class_name( | |
'msg-overlay-bubble-header__control.artdeco-button.artdeco-button--circle.artdeco-button--muted.artdeco-button--1.artdeco-button--tertiary.ember-view') | |
for closebut in close: | |
closebut.click() | |
except: | |
print("No close button found") | |
exp = "There is some problem sending the message, try again or contact the developer." | |
sendMail(receiver_email=usr, bodyMessage=exp) | |
time.sleep(3) | |
print('Message send') | |
else: | |
message = "No more birthday for today" | |
sendMessage() | |
return message, birthdayList | |
driver.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment