ability to log both gc or dms

This commit is contained in:
array-in-a-matrix 2021-10-09 23:16:08 -04:00
parent 6293dd8a5d
commit b7b696a104

View file

@ -11,8 +11,15 @@ with open('login.json', 'r') as file:
username = json_object['username']
password = json_object['password']
private = json_object['private']
# get account credentials from json file
# are we loging a private chat or a group chat?
if private == "Yes":
private = True
elif private == "No":
private = False
browser = webdriver.Firefox()
browser.implicitly_wait(5)
browser.get('https://www.instagram.com/')
@ -37,7 +44,7 @@ sleep(2)
# sleep(2)
# # open first direct message chat
browser.find_element_by_xpath("//*[contains(text(), 'array.in.a.matrix')]").click()
browser.find_element_by_xpath("//*[contains(text(), 'test')]").click()
sleep(2)
# open dms with a specific user
@ -47,11 +54,12 @@ else:
open('messages.txt', "a+").writelines("##### LOG FILE " + datetime.now().strftime("%d/%m/%Y %H:%M:%S") + " #####")
# if log file does not exist create it
while True:
while private:
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
try:
l_message_author = 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[1]/div/a").get_attribute("href"))[26:-1]
l_message_author = 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[1]/div/a").get_attribute("href"))[26:-1]
except NoSuchElementException as exception:
l_message_author = username
# get last message's author if not found its probably you who sent it
@ -72,4 +80,32 @@ while True:
f.writelines(last_message)
f.close()
# if last dm is already logged, do nothing, else log it
while not private:
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/div/div/div/div/div/div/div/span").text)
# get last message from site
# try:
# l_message_author = 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[1]/div/a").get_attribute("href"))[26:-1]
# except NoSuchElementException as exception:
# l_message_author = username
# except StaleElementReferenceException:
# sleep(0.5)
# l_message_author = 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[1]/div/a").get_attribute("href"))[26:-1]
#TODO: idk how to get message author reliably
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
if last_message == last_line:
pass
else:
with open('messages.txt', "a+") as f:
f.writelines(datetime.now().strftime("\n%d/%m/%Y %H:%M:%S - ") + ':\n')
f.writelines(last_message)
f.close()
# if last dm is already logged, do nothing, else log it
#? probably better to log to a json file or a db