|
| 1 | +# get current date in required format |
| 2 | +import datetime |
| 3 | + |
| 4 | +# store the birthdates of your contacts |
| 5 | +import json |
| 6 | + |
| 7 | +from selenium import webdriver |
| 8 | + |
| 9 | +# add a delay so that all elements of |
| 10 | +# the webpage are loaded before proceeding |
| 11 | +import time |
| 12 | + |
| 13 | +# Global variable Do not use elsewhere |
| 14 | +eleNM = None |
| 15 | + |
| 16 | +# This function is just to return a |
| 17 | +# string of the message required |
| 18 | +def wish_birth(name): |
| 19 | + return "Happy Birthday " + name.split(" ")[0] + "!!" |
| 20 | + |
| 21 | +# This function returns a list of values of some |
| 22 | +# attribute based on conditions on two attributes from the JSON file. |
| 23 | +# use to return names of contacts having their birthday on current date. |
| 24 | +def getJsonData(file, attr_ret, attr1, attr2, attr_val1, attr_val2): |
| 25 | + |
| 26 | + # Load the file's data in 'data' variable |
| 27 | + data = json.load(file) |
| 28 | + retv =[] |
| 29 | + |
| 30 | + # If the attributes' value conditions are satisfied, |
| 31 | + # append the name into the list to be returned. |
| 32 | + for i in data: |
| 33 | + if(i[attr1]== attr_val1 and i[attr2]== attr_val2): |
| 34 | + retv.append(i[attr_ret]) |
| 35 | + return retv |
| 36 | + |
| 37 | +# Opening the JSON file (birthdays.json) in read only mode. |
| 38 | +data_file = open("birthdays.json", "r") |
| 39 | +namev =[] |
| 40 | +print("Script Running") |
| 41 | + |
| 42 | +# This will keep rerunning the part of |
| 43 | +# the code from 'while True' to 'break'. |
| 44 | +# use to keep waiting for the JSON function |
| 45 | +# to return a non empty list. |
| 46 | +# In practice, this function will keep rerunning at |
| 47 | +# 11:59pm a day before the birthday and break out at 12:00am. |
| 48 | +while True: |
| 49 | + try: |
| 50 | + # to get current date |
| 51 | + datt = datetime.datetime.now() |
| 52 | + namev = getJsonData(data_file, "name", "birth_month", "birth_date", |
| 53 | + str(datt.month), str(datt.day)) |
| 54 | + |
| 55 | + except json.decoder.JSONDecodeError: |
| 56 | + continue |
| 57 | + if(namev !=[]): |
| 58 | + break |
| 59 | + |
| 60 | +# ChromeOptions allows us use the userdata of chrome |
| 61 | +# so that you don't have to sign in manually everytime. |
| 62 | +chropt = webdriver.ChromeOptions() |
| 63 | + |
| 64 | +# adding userdata argument to ChromeOptions object |
| 65 | +chropt.add_argument("user-data-<LOCATION TO YOUR CHROME USER DATA>") |
| 66 | + |
| 67 | +# Creating a Chrome webdriver object |
| 68 | +driver = webdriver.Chrome(executable_path ="<LOCATION TO CHROME WEBDRIVER>", |
| 69 | + options = chropt) |
| 70 | +driver.get("https://web.whatsapp.com/") |
| 71 | + |
| 72 | +# delay added to give time for all elements to load |
| 73 | +time.sleep(10) |
| 74 | + |
| 75 | +print(namev) |
| 76 | + |
| 77 | +# Finds the chat of your contacts (as in the namev list) |
| 78 | +for inp in namev: |
| 79 | + try: |
| 80 | + eleNM = driver.find_element_by_xpath('//span[@title ="{}"]'.format(inp)) |
| 81 | + except Exception as ex: |
| 82 | + print(ex) |
| 83 | + continue |
| 84 | + # Simulates a mouse click on the element |
| 85 | + eleNM.click() |
| 86 | + |
| 87 | + while(True): |
| 88 | + # Finds the chat box element |
| 89 | + eleTF = driver.find_element_by_class_name("_13mgZ") |
| 90 | + # Writes the message(function call to wish_birth()) |
| 91 | + eleTF.send_keys(wish_birth(inp)) |
| 92 | + # Finds the Send button |
| 93 | + eleSND = driver.find_element_by_class_name("_3M-N-") |
| 94 | + # Simulates a click on it |
| 95 | + eleSND.click() |
| 96 | + break |
0 commit comments