Inicio • Documentos • PyPi • Características • Instalación • Uso • Colaboradores
pystagram es un cliente de Python para las API de Instagram.
Proporciona una interfaz simple y fácil de usar para acceder a los puntos finales de las API Graph y Basic Display.
pip install pystagram
git clone https://github.com/MatthieuThib/pystagram.git
cd pystagram
pip install .
Para utilizar las API de Instagram (Graph API y Basic Display API), se requieren algunos requisitos previos, siga la guía de introducción para configurar su cuenta y obtener las credenciales necesarias:
Esto le proporcionará las siguientes credenciales:
Las API de Instagram utilizan tokens de acceso para autenticar solicitudes. Esos tokens están vinculados a permisos específicos y pueden generarse para diferentes propósitos. Antes de llamar a cualquier punto final, asegúrese de que el token de acceso tenga los permisos necesarios para solicitar el punto final.
Consulte la página Permisos para obtener más información.
Publicar un medio
import os
# Importing the necessary modules
from pystagram import pystagram GraphApi
from pystagram . components . containers import ImageContainer
# Initializing the pystagram GraphApi with the necessary credentials
graph_api = pystagram GraphApi (
app_id = int ( os . getenv ( "APP_ID" )), # The App ID from the environment variables
app_secret = os . getenv ( "APP_SECRET" ), # The App Secret from the environment variables
access_token = os . getenv ( "ACCESS_TOKEN" ), # The Access Token from the environment variables
)
# Creating an ImageContainer with the image URL and caption
container = ImageContainer (
image_url = "https://www.example.com/image.jpg" , # The URL of the image
caption = "your caption #hashtag" , # The caption for the image
# Additional parameters can be added here
)
# Creating a media object with the ImageContainer
response = graph_api . user . media . create ( container )
# Extracting the ID of the created media object
container_id = response . data . get ( "id" )
# Publishing the created media object
graph_api . user . media_publish . create ( container_id = container_id )
Obtener medios de usuario
import os
from pystagram import pystagram BasicDisplayApi
from pystagram . components . fields import MediaFields
# Instantiate the pystagram BasicDisplayApi class with the necessary credentials
basic_display_api = pystagram BasicDisplayApi (
app_id = int ( os . getenv ( "APP_ID" )), # The App ID from the environment variables
app_secret = os . getenv ( "APP_SECRET" ), # The App Secret from the environment variables
access_token = os . getenv ( "ACCESS_TOKEN" ), # The Access Token from the environment variables
)
# Fetch the user's media from the API
# The get() method sends a GET request to the API and returns the response
response = basic_display_api . user . user_media . get ()
# Extract the user's media data from the response
user_media = response . data . get ( "data" )
Ambas API cuentan con puntos finales paginados, lo que significa que la respuesta de una solicitud se puede dividir en varias páginas. La biblioteca pystagram maneja esto decorando los métodos de los puntos finales con un decorador personalizado @cursor_paginated. Cuando se llama, el método decorado iterará sobre todas las páginas hasta que no queden más páginas para recuperar o se alcance el número máximo de páginas. De forma predeterminada, el número máximo de páginas se establece en Ninguno (es decir, sin límite), pero se puede cambiar estableciendo el atributo MAX_PAGES de la clase en un valor entero diferente.
from pystagram import pystagram GraphApi
# Initializing the pystagram GraphApi with the necessary credentials
graph_api = pystagram GraphApi ( ... )
# Set the maximum number of pages to fetch from the API
graph_api . MAX_PAGES = 5
# Request a cursor paginated endpoint
response = graph_api . user . media . get ()