How to launch CookieRobot using Python
CookieRobot makes your profiles look more legit by automatically collecting cookies from your chosen websites – no manual crawling needed! In this guide, we’ll walk you through how to use it to streamline the process.
Already familiar with automation or completed some steps? Use the table of contents on the right to jump straight to what you need. Let’s go!
Python code application
In this example, settings are configured in the payload
object, inside the run_script()
function:
- CookieRobot will be running through the file
cookie_robot.py
- The profile IDs worked are set to
ced16576-a67b-4ae5-8459-c07991d50f27
andec0cf95f-b199-4b1c-b394-af1e01ac9c09
- Both profiles will run in non-headless mode
- The websites are Yahoo.com and Amazon.com, and they will be visited in a random order
- Since
fractionMode
is set to 1, 100% of the websites listed will be visited - CookieRobot will automatically accept cookie consent notices if they appear
import logging
import requests
import json
# In project root you need a file called token with your auth token in there
def setup_logging():
"""Set up logging configuration."""
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s",
handlers=[
logging.FileHandler("script_runner.log"),
logging.StreamHandler()
]
)
def get_token_from_file():
"""Read the token from a file called 'token' in the project root."""
try:
with open("token", "r") as file:
token = file.read().strip()
return token
except FileNotFoundError:
logging.error("Token file not found in the project root.")
raise
except Exception as e:
logging.error("An error occurred while reading the token file: %s", str(e))
raise
def run_script():
url = "https://launcher.mlx.yt:45001/api/v1/run_script"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {get_token_from_file()}"
}
payload = {
"script_file": "cookie_robot.py",
"profile_ids": [
{
"profile_id": "ced16576-a67b-4ae5-8459-c07991d50f27",
"is_headless": False
},
# {
# "profile_id": "ec0cf95f-b199-4b1c-b394-af1e01ac9c09",
# "is_headless": False
# },
],
"script_params": [
{
"name": "websites",
"value": ["yahoo.com", "amazon.com"]
},
{
"name": "randomOrder",
"value": True
},
{
"name": "fractionMode",
"value": 1
},
{
"name": "processCookieConsent",
"value": True
}
]
}
try:
logging.info("Payload to send: %s", json.dumps(payload, indent=4))
logging.info("Sending request to the script runner endpoint...")
response = requests.post(url, headers=headers, data=json.dumps(payload), timeout=30)
if response.status_code == 200:
logging.info("Script executed successfully.")
logging.info("Response: %s", response.json())
else:
logging.error("Failed to execute script. Status code: %d", response.status_code)
logging.error("Response: %s", response.text)
except requests.exceptions.RequestException as e:
logging.error("An error occurred while making the request: %s", str(e))
if __name__ == "__main__":
setup_logging()
run_script()