2

I am trying to write a code that periodically uploads an image(scraped as URL from somewhere else) to my twitter account. It will be in the AWS Lambda so I don't have an option(or I don't know the way yet) to download it first and then upload it to twitter, it should be all conducted in the browser environment. And I don't have Twitter developer account so I can't use Twitter API(I am rejected). Is this possible at all? I searched for many methods including base64, request, selenium but couldn't figure it out. I must say I am beginner at coding, so I need you guys help.

Below is my code which successfully scrapes an image from the pinterest(I deleted those parts as they had nothing to do with this question) and successfully tweets texts. But not images. My code looks like mess I know, after I find the way I will simplify it.

Say that I scraped the url using my selenium codes which is not written here.

import json, time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import TimeoutException



def lambda_handler(event, context):
    options = Options()
    options.binary_location = '/opt/headless-chromium'
    options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    options.add_argument('--single-process')
    options.add_argument('--disable-dev-shm-usage')

    driver = webdriver.Chrome('/opt/chromedriver',chrome_options=options)

    url = "https://i.pinimg.com/564x/db/f0/c0/dbf0c0e7a506c5b532df174a0a2fcdb7.jpg"

    driver.get(url)
    time.sleep(3)
    imagepage = driver.find_element_by_tag_name('img')
    actions = ActionChains(driver)
    actions.move_to_element(imagepage)
    actions.key_down(Keys.CONTROL)
    actions.send_keys("c")
    actions.key_up(Keys.CONTROL)
    actions.perform()


    driver.get("https://twitter.com/login")
    time.sleep(3)

    username_field=driver.find_element_by_xpath("//[@id='reactroot']/div/div/div[2]/main/div/div/div[1]/form/div/div[1]/label/div/div[2]/div/input")
    password_field = driver.find_element_by_xpath("//*[@id='react-root']/div/div/div[2]/main/div/div/div[1]/form/div/div[2]/label/div/div[2]/div/input")

    username_field.send_keys("*****")
    driver.implicitly_wait(2)
    
    password_field.send_keys("*****")
    driver.implicitly_wait(2)
    
    password_field.submit()
    #time.sleep(10)

    driver.get("https://twitter.com/home")
    time.sleep(10) 

    
    autotw1 = WebDriverWait(driver, 15).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='css-1dbjc4n r-xoduu5 r-1sp51qo r-mk0yit r-13qz1uu']")))
    ActionChains(driver).move_to_element(autotw1).click(autotw1).send_keys(Keys.CONTROL, "v").perform()


    tweet = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='css-901oao css-16my406 css-bfa6kz r-1qd0xha r-ad9z0x r-bcqeeo r-qvutc0']//span[@class='css-901oao css-16my406 r-1qd0xha r-ad9z0x r-bcqeeo r-qvutc0'][contains(text(),'Tweet')]")))
    tweet.click()




        
    afw = "successful"
   
    body = f"Headless Chrome Initialized, Page title: {afw}"

    driver.close()
    driver.quit()

    return {
        "statusCode": 200,
        "body": body
    }

As you can see I am lastly tried ctrl+c and ctrl+v method which is failed too. When I run the code with $sam local invoke, I see no error but see no image post too.

I am waiting for any support, thanks.

0 Answers0