Mass Auto Follow Strava

2024.09.18 00:00

Blog

This script does the following:

After processing each URL, it writes the remaining unprocessed URLs to a temporary file (temp_urls.txt).
It then replaces the original file (link.txt) with the temporary file, effectively removing the processed URL from the list.
This process repeats for each URL, continuously updating the file to contain only unprocessed URLs.
At the end of the script, it attempts to delete the temporary file if it still exists.

This approach ensures that even if the script is interrupted, the link.txt file will always contain only the unprocessed URLs. When you run the script again, it will continue from where it left off.

Remember to handle the file operations carefully, as this script modifies the input file. It’s always a good idea to keep a backup of your original URL list before running this script.

Login function :

A new login_to_strava(email, password) function has been added. This function navigates to the Strava login page, enters the provided email and password, and clicks the login button.

The login function is called before processing the URLs. You need to replace "your_email@example.com" and "your_password" with your actual Strava credentials.

The login function includes error handling. If there's an error during login, the script will print an error message, close the browser, and exit.

After successful login, the script waits for the feed to load before proceeding with the URL processing.

Script Selenium

from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
import time
import os

# Set up Firefox options
firefox_options = Options()
firefox_options.add_argument("--start-maximized")

# Set up the Firefox driver
driver = webdriver.Firefox(service=Service(GeckoDriverManager().install()), options=firefox_options)

# Function to log in to Strava
def login_to_strava(email, password):
    try:
        # Navigate to the Strava login page
        driver.get("https://www.strava.com/login")
        
        # Wait for the email input field to be present
        email_field = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, "email"))
        )
        
        # Enter email
        email_field.send_keys(email)
        
        # Find and enter password
        password_field = driver.find_element(By.ID, "password")
        password_field.send_keys(password)
        
        # Click the login button
        login_button = driver.find_element(By.ID, "login-button")
        login_button.click()
        
        # Wait for the login to complete (adjust the timeout as needed)
        WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.CLASS_NAME, "feed-ui"))
        )
        
        print("Successfully logged in to Strava")
    except Exception as e:
        print(f"An error occurred during login: {str(e)}")
        driver.quit()
        exit(1)

# Function to click all "follow" buttons
def click_follow_buttons():
    script = """
    let tmbl = document.querySelectorAll('.follow');
    tmbl.forEach(kTmbl => kTmbl.click());
    return tmbl.length;  // Return the number of buttons clicked
    """
    return driver.execute_script(script)

# Login to Strava (replace with your credentials)
login_to_strava("your_email@example.com", "your_password")

# Open the file containing URLs
file_path = 'link.txt'
with open(file_path, 'r') as file:
    urls = file.readlines()

# Create a temporary file to store unprocessed URLs
temp_file_path = 'temp_urls.txt'

# Iterate through each URL
for i, url in enumerate(urls):
    url = url.strip()
    
    try:
        # Open the URL in the Firefox browser
        driver.get(url)
        
        # Wait for the page to load (adjust the timeout as needed)
        WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.TAG_NAME, 'body')))
        
        # Click the "follow" buttons
        buttons_clicked = click_follow_buttons()
        print(f"Clicked {buttons_clicked} 'follow' buttons on {url}")
        
        # Wait for 3 seconds before moving to the next URL
        time.sleep(3)
    
    except TimeoutException:
        print(f"Timeout occurred while loading {url}")
    except Exception as e:
        print(f"An error occurred with {url}: {str(e)}")
    
    # Write unprocessed URLs to the temporary file
    with open(temp_file_path, 'w') as temp_file:
        temp_file.writelines(urls[i+1:])

    # Replace the original file with the temporary file
    os.replace(temp_file_path, file_path)

print("All URLs have been processed.")

# Close the browser
driver.quit()

# Delete the temporary file if it exists
if os.path.exists(temp_file_path):
    os.remove(temp_file_path)

Assalamualaykum!

Referensi

https://poe.com/s/HVX847nSnbiay105x5TV