Beranda • Dokumen • PyPi • Fitur • Instalasi • Penggunaan • Kontributor
pystagram adalah klien python untuk API Instagram.
Ini menyediakan antarmuka yang sederhana dan mudah digunakan untuk mengakses titik akhir API Grafik dan Tampilan Dasar.
pip install pystagram
git clone https://github.com/MatthieuThib/pystagram.git
cd pystagram
pip install .
Untuk menggunakan API Instagram (Graph API dan Basic Display API), diperlukan beberapa prasyarat, ikuti panduan memulai untuk menyiapkan akun Anda dan mendapatkan kredensial yang diperlukan:
Ini akan memberi Anda kredensial berikut:
API Instagram menggunakan token akses untuk mengautentikasi permintaan. Token tersebut terikat pada izin tertentu dan dapat dibuat untuk tujuan berbeda. Sebelum memanggil titik akhir apa pun, pastikan token akses memiliki izin yang diperlukan untuk meminta titik akhir.
Lihat halaman Izin untuk informasi lebih lanjut.
Menerbitkan sebuah media
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 )
Ambil media pengguna
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" )
Kedua API ini memiliki titik akhir yang diberi nomor halaman, yang berarti respons permintaan dapat dibagi menjadi beberapa halaman. Pustaka pystagram menangani hal ini dengan mendekorasi metode titik akhir dengan dekorator khusus @cursor_paginasi. Saat dipanggil, metode yang dihias akan mengulangi seluruh halaman hingga tidak ada lagi halaman yang perlu diambil atau jumlah halaman maksimum tercapai. Secara default, jumlah halaman maksimum diatur ke Tidak Ada (yaitu tanpa batas), namun dapat diubah dengan meneruskan pengaturan atribut MAX_PAGES kelas ke nilai integer yang berbeda.
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 ()