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 ()