log messages to text file

This commit is contained in:
array-in-a-matrix 2021-09-28 10:52:09 -04:00
parent 30a2df6779
commit 17bd207afc
2 changed files with 42 additions and 22 deletions

4
.gitignore vendored
View file

@ -129,4 +129,6 @@ dmypy.json
.pyre/
# login credentials
login.json
login.json
# logged messages
messages.txt

View file

@ -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()
#* 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