หน้าแรก • เอกสาร • PyPi • คุณสมบัติ • การติดตั้ง • การใช้งาน • ผู้ร่วมให้ข้อมูล
pystagram เป็นไคลเอนต์หลามสำหรับ Instagram API
โดยมีอินเทอร์เฟซที่เรียบง่ายและใช้งานง่ายสำหรับการเข้าถึงจุดสิ้นสุดของทั้ง Graph และ Basic Display 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 เมื่อเรียก เมธอดการตกแต่งจะวนซ้ำทุกเพจจนกว่าจะไม่มีเพจให้ดึงข้อมูลอีกต่อไป หรือถึงจำนวนเพจสูงสุด ตามค่าเริ่มต้น จำนวนหน้าสูงสุดจะถูกตั้งค่าเป็น ไม่มี (เช่น ไม่จำกัด) แต่สามารถเปลี่ยนแปลงได้โดยการตั้งค่าแอตทริบิวต์ 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 ()