ホーム · ドキュメント · PyPi · 機能 · インストール · 使用法 · 貢献者
pystagram Instagram API 用の Python クライアントです。
グラフ API と基本表示 API の両方のエンドポイントにアクセスするための、シンプルで使いやすいインターフェイスを提供します。
pip install pystagram
git clone https://github.com/MatthieuThib/pystagram.git
cd pystagram
pip install .
Instagram API (Graph API および Basic Display API) を使用するには、いくつかの前提条件が必要です。スタート ガイドに従ってアカウントを設定し、必要な認証情報を取得します。
これにより、次の認証情報が提供されます。
Instagram API はアクセス トークンを使用してリクエストを認証します。これらのトークンは特定の権限に関連付けられており、さまざまな目的で生成できます。エンドポイントを呼び出す前に、エンドポイントを要求するために必要なアクセス許可がアクセス トークンにあることを確認してください。
詳細については、「権限」ページを参照してください。
メディアを公開する
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 )
ユーザーメディアを取得する
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" )
どちらの API もページ分割されたエンドポイントを備えており、リクエストの応答を複数のページに分割できることを意味します。 pystagramライブラリは、エンドポイントのメソッドをカスタム デコレータ @cursor_paginated で装飾することでこれを処理します。呼び出されると、装飾されたメソッドは、フェッチするページがなくなるか、最大ページ数に達するまで、すべてのページを反復処理します。デフォルトでは、最大ページ数はNone (つまり制限なし) に設定されていますが、クラスの属性MAX_PAGESの設定を別の整数値に渡すことで変更できます。
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 ()