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/
|
.pyre/
|
||||||
|
|
||||||
# login credentials
|
# 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 import webdriver
|
||||||
from selenium.common.exceptions import NoSuchElementException
|
from selenium.common.exceptions import NoSuchElementException
|
||||||
import json
|
import json
|
||||||
|
from os.path import exists
|
||||||
|
# add needed libraries
|
||||||
|
|
||||||
with open('login.json', 'r') as file:
|
with open('login.json', 'r') as file:
|
||||||
json_object = json.load(file)
|
json_object = json.load(file)
|
||||||
|
|
||||||
username = json_object['username']
|
username = json_object['username']
|
||||||
password = json_object['password']
|
password = json_object['password']
|
||||||
|
# get account credentials from json file
|
||||||
|
|
||||||
browser = webdriver.Firefox()
|
browser = webdriver.Firefox()
|
||||||
browser.implicitly_wait(5)
|
browser.implicitly_wait(5)
|
||||||
browser.get('https://www.instagram.com/')
|
browser.get('https://www.instagram.com/')
|
||||||
sleep(2)
|
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='username']").send_keys(username)
|
||||||
browser.find_element_by_css_selector("input[name='password']").send_keys(password)
|
browser.find_element_by_css_selector("input[name='password']").send_keys(password)
|
||||||
|
|
||||||
browser.find_element_by_xpath("//button[@type='submit']").click()
|
browser.find_element_by_xpath("//button[@type='submit']").click()
|
||||||
print('login successful')
|
|
||||||
sleep(5)
|
sleep(5)
|
||||||
|
# login
|
||||||
|
|
||||||
browser.get('https://www.instagram.com/direct/inbox/')
|
browser.get('https://www.instagram.com/direct/inbox/')
|
||||||
sleep(2)
|
sleep(2)
|
||||||
|
# go to dms
|
||||||
|
|
||||||
browser.find_element_by_css_selector("button.aOOlW:nth-child(2)") .click()
|
browser.find_element_by_css_selector("button.aOOlW:nth-child(2)") .click()
|
||||||
print("click on `not now` for notifications")
|
|
||||||
sleep(2)
|
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()
|
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()
|
||||||
print("open one of the dms")
|
|
||||||
sleep(2)
|
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:
|
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"
|
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)
|
||||||
try:
|
# get last message from site
|
||||||
message = browser.find_element_by_xpath(increment_message).text
|
last_line = ''
|
||||||
print(message)
|
with open('messages.txt', "r") as f:
|
||||||
except NoSuchElementException as exception:
|
for line in f:
|
||||||
print("no message to display")
|
pass
|
||||||
#? error handling if a specifc element does not exist
|
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")
|
if last_message == last_line:
|
||||||
# textfield.send_keys("this message is sent through a selenium bot by Array in a Matrix™")
|
pass
|
||||||
#? find textbox and types a message
|
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()
|
#* try:
|
||||||
#? sends whatever message was in text field
|
#* message = browser.find_element_by_xpath(xpath_here).text
|
||||||
|
#* print(message)
|
||||||
# sleep(10)
|
#* except NoSuchElementException as exception:
|
||||||
|
#* print("no message to display")
|
||||||
# browser.close()
|
#*? error handling if a specifc element does not exist, might need this later
|
Loading…
Reference in a new issue