pystagram
1.2.0
首頁 • 文件 • PyPi • 功能 • 安裝 • 使用 • 貢獻者
pystagram是 Instagram API 的 python 用戶端。
它提供了一個簡單易用的介面,用於存取圖形和基本顯示 API 的端點。
pip install pystagram
git clone https://github.com/MatthieuThib/pystagram.git
cd pystagram
pip install .
為了使用 Instagram API(圖形 API 和基本顯示 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_paglated 裝飾端點的方法來處理此問題。呼叫時,裝飾方法將迭代所有頁面,直到沒有更多頁面可供取得或達到最大頁面數。預設情況下,最大頁數設定為「無」 (即無限制),但可以透過將類別的屬性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 ()