> For the complete documentation index, see [llms.txt](https://docs.decentraland.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.decentraland.org/contributor/contributor-es/contenido/practica/python-examples.md).

# Ejemplos de Python

Esta práctica muestra cómo escribir un programa sencillo que descarga y analiza algo de contenido usando el [instantáneas](https://github.com/decentraland/docs/tree/main/contributor/practice/snapshots/README.md) proporcionado por content servers.

{% hint style="info" %}
Puedes encontrar el [script completo](https://github.com/decentraland/documentation/blob/main/content/contributor/content/practice/snapshots_mini.py) en GitHub, junto con un [ejemplo más avanzado](https://github.com/decentraland/documentation/blob/main/content/contributor/content/practice/snapshots.py).
{% endhint %}

Usaremos el server de la Decentraland Foundation en `peer.decentraland.org`, y Python 3 como nuestro lenguaje preferido.

Esto es lo que haremos:

1. Consultar el estado del content server.
2. Selecciona y descarga una [instantánea](https://github.com/decentraland/docs/tree/main/contributor/practice/snapshots/README.md) con una lista de entities.
3. Imprime el tipo y el ID de todas las entities referenciadas.

Comencemos nuestro script con algunos preparativos. Usaremos solo módulos de la biblioteca estándar, pero en código real probablemente querrás un cliente HTTP más cómodo (como el [requests](https://github.com/psf/requests) library).

```py
# Haz una request HTTP GET, devuelve una response HTTP similar a un archivo.
def fetch(path):
    url = f"https://peer.decentraland.org/{path}"
    headers = { "User-Agent": "urllib" } # importante en algunos servers (si está vacío, 403 Forbidden)

    request = urllib.request.Request(url, headers=headers)
    response = urllib.request.urlopen(request)

    return response
```

Nuestro simple helper hace un request HTTP `GET` request, y devuelve el objeto de response similar a un archivo. Nada sofisticado. Usemoslo para llamar al `/about` endpoint y comprobar el estado del server:

```py
# Comprueba el estado del server:
about = json.load(fetch('about'))

if not about['healthy']:
    print("¡Server no saludable!")
    sys.exit(1)
```

Si superamos este punto, el server está en funcionamiento (obtuvimos una `200` response) y reporta estar operativo. Podemos solicitar el conjunto actual de snapshots (que viene en formato de array JSON):

```py
# Obtén la lista de snapshots:
all_snapshots = json.load(fetch('content/snapshots'))
```

Los archivos snapshot (especialmente para los rangos de tiempo más largos) pueden ser muy grandes. Para un experimento rápido, tomemos el más pequeño de la lista por `numberOfEntities`:

```py
# Toma la instantánea más pequeña, en términos de entities incluidas:
snapshot = min(all_snapshots, key=lambda s: s['numberOfEntities'])
```

Para descargar el contenido, necesitamos el `hash` campo de `instantánea`. Obtenemos la URL del archivo añadiéndolo a la raíz del contenido:

```py
# Solicita el archivo desde la API de contenido:
response = fetch('content/contents/' + snapshot['hash'])
```

El archivo que seleccionamos es lo suficientemente pequeño como para almacenarlo en memoria, pero fingamos que no lo sabemos y hagamos streaming de él. La primera línea es el encabezado de la instantánea, y cada línea posterior contiene un objeto JSON.

Comprobemos el encabezado, siempre es una buena idea:

```py
# Verifica el encabezado de la instantánea:
header = response.readline().decode('utf-8').strip()

if header != '### Decentraland json snapshot':
    print("Encabezado de snapshot inválido: " + header)
    sys.exit(1)
```

Ahora podemos procesar todas las entities en la instantánea, leyendo la response línea por línea. Para nuestros modestos propósitos, *procesar* significa imprimir el tipo de entity y el ID:

```py
# Lee y decodifica todos los elementos, un JSON por línea:
for line in response:
    item = json.loads(line)
    print(item['entityType'], item['entityId'])
```

Este bucle comenzará a hacer streaming, parseando e imprimiendo líneas como estas hasta que termine con la instantánea:

```
profile bafkreic36qmzyprs6whkpuxbeiif4no6kvdrr2tfpichbx2fzfz5py6eyv
scene bafkreibr5xfujqrp5q3o4s73vm2yljlcp7cucqgugssnarsuclxv4emlmy
profile bafkreid7khr5wnkialba44rsslffi633rh3lvctad5oa5vjoe6wa7s4c5a
wearable bafkreihlqcb7jgubomyidikpwpqhgzbagltk5m4rgbjdvzydxmoka7bg4i
```

¡Salud! Hemos usado el sistema de snapshots para explorar parte del contenido disponible en Decentraland.

Recuerda que puedes encontrar el [script completo](https://github.com/decentraland/documentation/blob/main/content/contributor/content/practice/snapshots_mini.py) en GitHub, junto con un [ejemplo más avanzado](https://github.com/decentraland/documentation/blob/main/content/contributor/content/practice/snapshots.py).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.decentraland.org/contributor/contributor-es/contenido/practica/python-examples.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
