Action code fetched file

Using the action code below either fails or I’m missing something.
I couldn’t find any help, readme, or other reference so I’m not sure how to get image.png to the AI. It’s not shown to the user and it seems the AI doesn’t have access to the file, as it’s making things up.

The fetch works (verified outside of actions code).

import requests

def fetch_url_screenshot(url: str):
    """
    Fetches an image using the link embedded in the provided

    Args:
        url (string): The URL address that will return the image URL
    """

    # Insert your code below. You can access environment variables using os.environ[].
    # # Currently, only the requests library is supported, but more libraries will be available soon.
    # Use print statements or return values to display results to the user.
    # If you save a PNG image in the root directory, it will be automatically displayed to the user.
    trimmed_url = url.strip()
    headers = {"X-Return-Format": "pageshot"}
    
    # Initial request to get the image URL
    response = requests.get(trimmed_url, headers=headers)
    
    # Assuming the response contains a new URL for the image
    new_url = response.text.strip()
    
    # Fetching the content of the new URL (image)
    img_response = requests.get(new_url)
    
    # Saving the image content to a PNG file
    # Tried ("/image.png", "wb") as well
    with open("image.png", "wb") as f:
        f.write(img_response.content)

Hi @kaerez,

I ran into an issue with your code, where the error message says, “No connection adapters were found”. This typically happens when the URL format is incorrect, or the requests library gets blocked by some websites.

Some websites block requests made by Python libraries like requests. In that case, consider using an API service specifically for taking website screenshots, and call that with the request library.

I didn’t realize you could retrieve stuff on the actions side.

As assuming, you could almost out any python code in the action and it would run - correct ?

That is correct. We currently only support the requests module, but we will support more modules soon. Pretty much any python code will work.

Thanks Nathaniel

Appreciate it

Hi Nathaniel

Quick question.

Can you only have one script on a page, meaning that you cannot call additional scripts that are part of the package, or could you if there full URL was used.

As the top part of the script is built with the variables etc and cannot be changes, how do you add in additional items that should be part of the top section or does it matter ?

I am not a python coder - more of a hack, so am just trying to see how to use existing code.

For example, would this work?

import requests
from bs4 import BeautifulSoup
import csv
res = requests.get('http://www.stroeder.com/')
soup = BeautifulSoup(res.text, 'html.parser')
h1 = soup.h1.text
def write_csv(filename, data):
    with open(filename, 'w') as f:
        writer = csv.writer(f)
        writer.writerows(data)
        f.close()

write_csv('test.csv',[[h1]])

Iain

We don’t currently support any modules that have to be installed other than requests. So no bs4 or csv for now.

You can add multiple actions. But within a given action, they reflect the implementation of a single function.