Startseite • Dokumente • PyPi • Funktionen • Installation • Verwendung • Mitwirkende
pystagram ist ein Python-Client für Instagram-APIs.
Es bietet eine einfache und benutzerfreundliche Schnittstelle für den Zugriff auf Endpunkte sowohl der Graph- als auch der Basic Display-APIs.
pip install pystagram
git clone https://github.com/MatthieuThib/pystagram.git
cd pystagram
pip install .
Um die Instagram-APIs (Graph API und Basic Display API) nutzen zu können, sind einige Voraussetzungen erforderlich. Befolgen Sie die Anleitung „Erste Schritte“, um Ihr Konto einzurichten und die erforderlichen Anmeldeinformationen zu erhalten:
Dadurch erhalten Sie die folgenden Anmeldeinformationen:
Instagram-APIs verwenden Zugriffstoken, um Anfragen zu authentifizieren. Diese Token sind an bestimmte Berechtigungen gebunden und können für verschiedene Zwecke generiert werden. Stellen Sie vor dem Aufrufen eines Endpunkts sicher, dass das Zugriffstoken über die erforderlichen Berechtigungen zum Anfordern des Endpunkts verfügt.
Weitere Informationen finden Sie auf der Seite „Berechtigungen“.
Ein Medium veröffentlichen
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 )
Benutzermedien abrufen
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" )
Beide APIs verfügen über paginierte Endpunkte, was bedeutet, dass die Antwort einer Anfrage auf mehrere Seiten aufgeteilt werden kann. Die pystagram -Bibliothek handhabt dies, indem sie die Methoden der Endpunkte mit einem benutzerdefinierten Dekorator @cursor_paginated dekoriert. Beim Aufruf durchläuft die dekorierte Methode alle Seiten, bis keine weiteren Seiten mehr abzurufen sind oder die maximale Seitenanzahl erreicht ist. Standardmäßig ist die maximale Anzahl von Seiten auf „Keine“ (d. h. keine Begrenzung) eingestellt. Sie kann jedoch geändert werden, indem das Attribut „MAX_PAGES“ der Klasse auf einen anderen ganzzahligen Wert gesetzt wird.
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 ()