log messages to text file
This commit is contained in:
parent
30a2df6779
commit
17bd207afc
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -129,4 +129,6 @@ dmypy.json
|
|||
.pyre/
|
||||
|
||||
# login credentials
|
||||
login.json
|
||||
login.json
|
||||
# logged messages
|
||||
messages.txt
|
60
index.py
60
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()
|
||||
#* 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
|
Loading…
Reference in a new issue