Created
November 28, 2022 20:07
-
-
Save hoqqanen/c1c11d1c6a7f99b86cc8d9ce88f2d9fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# SETUP BEFORE RUNNING: | |
# - download and install python 3, https://www.python.org/downloads/ | |
# - In a terminal: | |
# - pip install pytest-playwright | |
# - playwright install | |
# To run: in a terminal type python3, a space, then drag and drop this file. | |
# Press return and the program will start! | |
# ----------------------------------- | |
# Load the bot specification | |
# ----------------------------------- | |
import json | |
BOT_FILE = input("Drag and drop bot file here: ").strip() | |
BOT_SPEC = json.load(open(BOT_FILE)) | |
# ----------------------------------- | |
# Get credentials to log in | |
# ----------------------------------- | |
USERNAME = input("Twitter username: ") | |
print("PSA: Don't trust programs you haven't audited with passwords to accounts you wouldn't be okay with having hacked.") | |
import getpass | |
PASSWORD = getpass.getpass("Twitter password: ") | |
# ----------------------------------- | |
# Start up the automated browser | |
# ----------------------------------- | |
from playwright.sync_api import sync_playwright | |
import time | |
playwright = sync_playwright().start() | |
browser = playwright.chromium.launch(headless=False, slow_mo=100) | |
page = browser.new_page() | |
page.goto("https://twitter.com/login") | |
time.sleep(2.0) | |
page.locator("input").click() | |
page.locator("input").click() | |
page.locator("input").type(USERNAME) | |
page.locator("span", has_text="Next").first.click() | |
time.sleep(0.5) | |
page.locator("[type=password]").click() | |
page.locator("[type=password]").type(PASSWORD) | |
page.locator("span", has_text="Log in").first.click() | |
# ----------------------------------- | |
# Define functions to search and tweet | |
# ----------------------------------- | |
def search_twitter(word): | |
page.locator("[aria-label='Search query']").type(word) | |
page.keyboard.press("Enter") | |
time.sleep(3.0) | |
import random | |
def generate_bot_message(): | |
sample = {} | |
for k, v in BOT_SPEC['data'].items(): | |
sample[k] = random.sample(v, 1)[0] | |
return BOT_SPEC['template'].format(**sample) | |
def tweet_several(): | |
for i in range(page.locator('data-testid=reply').count()): | |
page.locator('data-testid=reply').nth(i).scroll_into_view_if_needed() | |
page.locator('data-testid=reply').nth(i).click() | |
page.locator("[aria-label='Tweet text']").click() | |
page.locator("[aria-label='Tweet text']").fill(generate_bot_message()) | |
page.locator('data-testid=tweetButton').first.click() | |
time.sleep(3.0) | |
# ----------------------------------- | |
# Get search terms for targeting | |
# ----------------------------------- | |
terms = input("List of search words separated by spaces: ").strip().split(' ') | |
# ----------------------------------- | |
# Tweet tweet tweet tweet tweet! | |
# ----------------------------------- | |
for term in terms: | |
search_twitter(term) | |
tweet_several() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment