diff --git a/.gitignore b/.gitignore index 5b7b8c4..7c9bd2d 100644 --- a/.gitignore +++ b/.gitignore @@ -129,4 +129,6 @@ dmypy.json .pyre/ # login credentials -login.json \ No newline at end of file +login.json +# logged messages +messages.txt \ No newline at end of file diff --git a/index.py b/index.py index 0ac6ab3..ec55655 100644 --- a/index.py +++ b/index.py @@ -2,52 +2,70 @@ from time import sleep from selenium import webdriver from selenium.common.exceptions import NoSuchElementException import json +from os.path import exists +# add needed libraries with open('login.json', 'r') as file: json_object = json.load(file) username = json_object['username'] password = json_object['password'] +# get account credentials from json file browser = webdriver.Firefox() browser.implicitly_wait(5) browser.get('https://www.instagram.com/') sleep(2) +# start browser and go to site browser.find_element_by_css_selector("input[name='username']").send_keys(username) browser.find_element_by_css_selector("input[name='password']").send_keys(password) - browser.find_element_by_xpath("//button[@type='submit']").click() -print('login successful') sleep(5) +# login browser.get('https://www.instagram.com/direct/inbox/') sleep(2) +# go to dms browser.find_element_by_css_selector("button.aOOlW:nth-child(2)") .click() -print("click on `not now` for notifications") sleep(2) +# close notification pop up -browser.find_element_by_xpath("/html/body/div[1]/section/div/div[2]/div/div/div[1]/div[2]/div/div/div/div/div[3]/a").click() -print("open one of the dms") +browser.find_element_by_xpath("/html/body/div[1]/section/div/div[2]/div/div/div[1]/div[2]/div/div/div/div/div[1]/a").click() sleep(2) +# open first direct message chat + +if exists('messages.txt'): + pass +else: + open('messages.txt', "a+").writelines("##### LOG FILE #####") +# if log file does not exist create it while True: - increment_message = "/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[1]/div/div/div[last()]/div[2]/div" - try: - message = browser.find_element_by_xpath(increment_message).text - print(message) - except NoSuchElementException as exception: - print("no message to display") - #? error handling if a specifc element does not exist + last_message = str(browser.find_element_by_xpath("/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[1]/div/div/div[last()]/div[2]/div").text) + # get last message from site + last_line = '' + with open('messages.txt', "r") as f: + for line in f: + pass + last_line = line + print(last_line) + f.close() + #get last entry from log file -# textfield = browser.find_element_by_xpath("/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[2]/textarea") -# textfield.send_keys("this message is sent through a selenium bot by Array in a Matrix™") -#? find textbox and types a message + if last_message == last_line: + pass + else: + with open('messages.txt', "a+") as f: + f.writelines('\n') + f.writelines(last_message) + f.close() + # if last dm is already logged, do nothing, else log it -# browser.find_element_by_xpath("/html/body/div[1]/section/div/div[2]/div/div/div[2]/div[2]/div/div[2]/div/div/div[3]/button").click() -#? sends whatever message was in text field - -# sleep(10) - -# browser.close() \ No newline at end of file +#* try: +#* message = browser.find_element_by_xpath(xpath_here).text +#* print(message) +#* except NoSuchElementException as exception: +#* print("no message to display") +#*? error handling if a specifc element does not exist, might need this later \ No newline at end of file