properly records time of message & fixed emplty file bug
This commit is contained in:
parent
ee028ca1be
commit
6293dd8a5d
|
@ -1,6 +1,4 @@
|
|||
# insta-logger
|
||||
|
||||
This is a selenium based bot that logs instagram direct messages.
|
||||
currently the bot is very limited (some things are hard coded) because I am still figuring things out.
|
||||
|
||||
|
||||
currently the bot is very limited (some things are hard coded) because I am still figuring things out.
|
24
index.py
24
index.py
|
@ -1,7 +1,8 @@
|
|||
from time import sleep
|
||||
from selenium import webdriver
|
||||
from selenium.common.exceptions import NoSuchElementException
|
||||
import json
|
||||
import json, os
|
||||
from datetime import datetime
|
||||
from os.path import exists
|
||||
# add needed libraries
|
||||
|
||||
|
@ -28,28 +29,33 @@ 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()
|
||||
browser.find_element_by_css_selector("button.aOOlW:nth-child(2)").click()
|
||||
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[1]/a").click()
|
||||
sleep(2)
|
||||
# open first direct message chat
|
||||
# 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'):
|
||||
browser.find_element_by_xpath("//*[contains(text(), 'array.in.a.matrix')]").click()
|
||||
sleep(2)
|
||||
# open dms with a specific user
|
||||
|
||||
if exists('messages.txt') and (os.path.getsize('messages.txt') > 0):
|
||||
pass
|
||||
else:
|
||||
open('messages.txt', "a+").writelines("##### LOG FILE #####")
|
||||
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:
|
||||
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)
|
||||
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]
|
||||
except NoSuchElementException as exception:
|
||||
l_message_author = username
|
||||
# get last message's author if not found its probably you who sent it
|
||||
#! way slower than messages with an auther need fix
|
||||
last_line = ''
|
||||
with open('messages.txt', "r") as f:
|
||||
for line in f:
|
||||
|
@ -62,7 +68,7 @@ while True:
|
|||
pass
|
||||
else:
|
||||
with open('messages.txt', "a+") as f:
|
||||
f.writelines('\n' + l_message_author + ':\n')
|
||||
f.writelines(datetime.now().strftime("\n%d/%m/%Y %H:%M:%S - ") + l_message_author + ':\n')
|
||||
f.writelines(last_message)
|
||||
f.close()
|
||||
# if last dm is already logged, do nothing, else log it
|
||||
|
|
Loading…
Reference in a new issue