Twitter Bot Python

print

While we are in a carantine and Saturday nights are fullfilled with entertaiments like song contests, I had to be Entertained.

This is bot is written in Python. It’s main focus is to Tweet repeatedly different tweets in order to win Price price for lucky draw in the competition.

The bot initially login to twitter account then, if it gets an error it is handled, and then reads the latest tweet from a specific thread and then tweets it at the same thread with some added tags. The process of reading and tweeting is repeated in definitely unless we get an error and the process is stopped.

Tweets that are sent are stored at the local file. Login credentials are stored at the local that is being read at boot.

This code can be tweaked in order to handle many accounts.

The purpose of this project is only for experimentation, it is not intended for usage and I don’t take responsibility for any bad or illegal activities.

I was lucky and one of my tweets were shown on tv.

You can see the bot in action bellow:

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
# from selenium.webdriver.keys import Keys
from selenium.webdriver.chrome.options import Options;
import time
from datetime import datetime

browser = webdriver.Chrome(ChromeDriverManager().install())

def account_info():
    with open('files/account_info.txt','r') as f:
        info = f.read().split()
        email = info[0]
        password = info[1]
    return email, password

def get_tweet():
    path = 'https://twitter.com/search?q=%23j2us%20&src=typed_query&f=live'
    first_tweet_xpath = '//*[@id="react-root"]/div/div/div[2]/main/div/div/div/div/div/div[2]/div/div/section/div/div/div[1]/div/div/article/div/div/div/div[2]/div[2]/div[2]/div[1]/div/span[1]'
    browser.get(path)
    time.sleep(4)
    tweet_element = browser.find_element_by_xpath(first_tweet_xpath)
    time.sleep(4)
    return tweet_element.text

def login(email,password):
    browser.get('https://twitter.com/login')
    username_field = '//*[@id="react-root"]/div/div/div[2]/main/div/div/div[1]/form/div/div[1]/label/div/div[2]/div/input'
    password_field = '//*[@id="react-root"]/div/div/div[2]/main/div/div/div[1]/form/div/div[2]/label/div/div[2]/div/input'
    login_btn = '//*[@id="react-root"]/div/div/div[2]/main/div/div/div[1]/form/div/div[3]/div/div'
    time.sleep(1)
    browser.find_element_by_xpath(username_field).send_keys(email)
    time.sleep(0.6)
    browser.find_element_by_xpath(password_field).send_keys(password)
    time.sleep(0.6)
    browser.find_element_by_xpath(login_btn).click()
    time.sleep(4)
    check_adreess()

def check_adreess():
    print(browser.current_url);
    if (browser.current_url == "https://twitter.com/login?email_disabled=true&redirect_after_login=%2F"):
        phone_xpath = '//*[@id="react-root"]/div/div/div[2]/main/div/div/div[2]/form/div/div[1]/label/div/div[2]/div/input'
        password_xpath = '//*[@id="react-root"]/div/div/div[2]/main/div/div/div[2]/form/div/div[2]/label/div/div[2]/div/input'
        login_btn_xpath = '//*[@id="react-root"]/div/div/div[2]/main/div/div/div[2]/form/div/div[3]/div/div/span/span'

        browser.find_element_by_xpath(phone_xpath).send_keys('6977368455')
        time.sleep(0.5)
        browser.find_element_by_xpath(password_xpath).send_keys('nagasbot1')
        time.sleep(0.5)
        browser.find_element_by_xpath(login_btn_xpath).click()
        time.sleep(4)


def send_tweet(message):
    tweet_xpath = '//*[@id="react-root"]/div/div/div[2]/header/div/div/div/div[1]/div[3]/a/div'
    message_xpath = '//*[@id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div[3]/div/div/div/div[1]/div/div/div/div/div[2]/div[1]/div/div/div/div/div/div/div/div/div/div[1]/div/div/div/div[2]/div/div/div/div'
    tweet_btn_xpth = '//*[@id="layers"]/div[2]/div/div/div/div/div/div[2]/div[2]/div/div[3]/div/div/div/div[1]/div/div/div/div/div[2]/div[4]/div/div/div[2]/div[4]/div/span/span'

    browser.find_element_by_xpath(tweet_xpath).click()
    time.sleep(0.5)
    browser.find_element_by_xpath(message_xpath).send_keys(message)
    time.sleep(5)
    
    try:
        browser.find_element_by_xpath(tweet_btn_xpth).click()
    except:
        print("An exception occurred")
        blocking_view_xpath = '/html/body/div/div/div/div[1]/div[3]/div/div/div'
        browser.find_element_by_xpath(blocking_view_xpath).click()
        time.sleep(0.5)
        browser.find_element_by_xpath(tweet_btn_xpth).click()
    time.sleep(8)


def log_tweet(message):
    import datetime
    now = datetime.datetime.now()
    message_to_log = "\r"+str(now)+" "+message
    print(message_to_log)
    f = open("logs.txt", "a")
    f.write(message_to_log)
    f.close()




# log_tweet("nikos")

email, password = account_info()
login(email, password)
# print(get_tweet())
while True:
    message = get_tweet()
    message_with_hash = message + " #j2us #J2us_greece #j2usgr"
    send_tweet(message_with_hash)
    log_tweet(message_with_hash)



Example of the files/account_info.txt

Below is an example of the logs.txt file that is produced containing tweets sent.

 
2020-12-05 20:01:49.629632 Ξεκινάει η προετοιμασία....Μπαίνω για μπάνιο, βάζω τις καλές μου πυτζάμες, στρώνω τραπέζι (ποπ κορν, πατατάκια, τυράκια/αλλαντικά - αυτά για αρχή-) και βάζω μπουκάλια άσπρο κρασί στο ψυγείο να κρυώσουν. Σε μια ώρα αρχίζει το πάρτυ!!!  #j2us #J2us_greece #j2usgr
2020-12-05 21:00:17.013425 ΣΗΚΩ ΒΑΝΔΑΡΑ ΝΑ ΠΕΙΤΕ ΤΟ ΑΧΑΡΙΣΤΗ ΚΙ ΑΛΗΤΙΣΣΑ ΣΗΚΩ  #j2us #J2us_greece #j2usgr
2020-12-05 21:00:37.351002 Βασίλη πες το τι αισθάνεσαι για μένα να κανόε λίγο χαμό  #j2us #J2us_greece #j2usgr
2020-12-05 21:00:57.361193 #j2us #j2us #J2us_greece #j2usgr
2020-12-05 21:01:17.489683 Τιτανομεγιστoς Βασίλης Καρράς  #j2us #J2us_greece #j2usgr
2020-12-05 21:01:37.860473  παμεεεε μαρακι δεσποιναρα κουκλες οπως πανταααα  #j2us #J2us_greece #j2usgr
2020-12-05 21:01:57.944563  Βικάκι είσαι γλύκα  #j2us #J2us_greece #j2usgr
2020-12-05 21:02:18.148959 Υπέροχο το λεοπάρ πουκάμισο Νίκο μου  #j2us #J2us_greece #j2usgr
2020-12-05 21:02:38.195874 Ώπα ώπα ώπα  #j2us #J2us_greece #j2usgr
2020-12-05 21:02:58.264732 Γίγαντα Καρρα #j2us #J2us_greece #j2usgr
2020-12-05 21:03:18.278701 Δωωωωωσε   #j2us #J2us_greece #j2usgr
2020-12-05 21:03:38.564067 Εγώ είναι σα να κέρδισα με τον λεοπάρ κοκλο   #j2us #J2us_greece #j2usgr
2020-12-05 21:03:58.896718 Κοκλωνη το λεοπαρ σουτιέν που φοράς είναι με ενίσχυση;  #j2us #J2us_greece #j2usgr
2020-12-05 21:04:19.028675 #j2us_greece #j2us #J2us_greece #j2usgr
2020-12-05 21:04:39.197416 Τα σπάμε σήμερα για εσένα Βασιλάρα Καρρά  #j2us #J2us_greece #j2usgr
2020-12-05 21:04:59.671250 Ο Γαβαλάς λέει ότι το animal print θέλει δαντέλα... ακούς Κοκλώ #j2us #J2us_greece #j2usgr
2020-12-05 21:05:19.729506 #j2us #j2us #J2us_greece #j2usgr
2020-12-05 21:05:39.803190 Papaboi6969  #j2us #J2us_greece #j2usgr
2020-12-05 21:05:59.930710  Νίκο είσαι θεόςς!  #j2us #J2us_greece #j2usgr
2020-12-05 21:06:20.127061 Σε πόσα σκαλιά ανέβηκε η Βανδή ;  #j2us #J2us_greece #j2usgr
2020-12-05 21:06:40.209224 Νότα δεν πατάει  #j2us #J2us_greece #j2usgr

One thought on “Twitter Bot Python”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.