الصفحة الرئيسية • المستندات • PyPi • الميزات • التثبيت • الاستخدام • المساهمين
pystagram هو عميل بايثون لواجهات برمجة تطبيقات Instagram.
فهو يوفر واجهة بسيطة وسهلة الاستخدام للوصول إلى نقاط النهاية لكل من Graph وBasic Display APIs.
pip install pystagram
git clone https://github.com/MatthieuThib/pystagram.git
cd pystagram
pip install .
من أجل استخدام واجهات برمجة تطبيقات Instagram (واجهة برمجة تطبيقات الرسم البياني وواجهة برمجة تطبيقات العرض الأساسية)، يلزم توفر بعض المتطلبات الأساسية، اتبع دليل البدء لإعداد حسابك والحصول على بيانات الاعتماد اللازمة:
سيوفر لك هذا بيانات الاعتماد التالية:
تستخدم واجهات برمجة تطبيقات Instagram رموز الوصول لمصادقة الطلبات. ترتبط هذه الرموز المميزة بأذونات محددة ويمكن إنشاؤها لأغراض مختلفة. قبل الاتصال بأي نقطة نهاية، تأكد من أن رمز الوصول لديه الأذونات اللازمة لطلب نقطة النهاية.
راجع صفحة الأذونات لمزيد من المعلومات.
نشر وسيلة إعلامية
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" )
تتميز كلا واجهتي برمجة التطبيقات بنقاط نهاية مرقّمة، مما يعني أنه يمكن تقسيم استجابة الطلب إلى صفحات متعددة. تتعامل مكتبة 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 ()