10
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

🌟Raspberry PiでSNSフォロワーカウンターシステムを作る【第7回:Instagram&Facebook APIとの連携編】

Last updated at Posted at 2025-08-18

📝 はじめに

こんにちは、piyovateです!

このシリーズでは、Raspberry PiとPythonを使って「SNSフォロワー数をリアルタイムで見える化する壁掛けディスプレイ」の構築を目指しています。

前回【第6回】では、X(旧Twitter)のAPI連携を行い、フォロワー数をリアルタイム取得・表示できるようになりました。
今回はその続きとして、InstagramとFacebookのフォロワー数を取得して表示できるようにしていきます!

実はこの作業、業務が忙しくて約1か月ぶりの再開です。
久しぶりで思い出しながらですが、やっぱりこの手の作業は楽しいですね!
完成までもう少し。ここからは微調整を重ねていきます!

GitHubにもプロジェクトを公開しているので、セットアップやコード全体は以下を参照ください👇
🔗 GitHub - SystermDevelopment/follower-counter


🎯 今回の概要

  • Instagram APIを使ってビジネスアカウントのフォロワー数を取得
  • Facebook APIを使ってページのフォロワー数を取得
  • Xと同様に、数値として返す設計(int型で返却)
  • UI側にラベル追加(数値 or エラーで表示切り替え)

📌 Instagram APIの連携

📄 .env設定

IG_USER_ID=your_instagram_business_account_id  
IG_TOKEN=your_instagram_token

🐍 instagramAPI.py

import os
import requests
from dotenv import load_dotenv

load_dotenv()

IG_USER_ID = os.getenv("IG_USER_ID")
IG_TOKEN = os.getenv("IG_TOKEN")

def get_instagram_follower_count():
    url = f"https://graph.facebook.com/v19.0/{IG_USER_ID}?fields=followers_count&access_token={IG_TOKEN}"
    try:
        res = requests.get(url)
        if res.status_code == 200:
            data = res.json()
            return int(data.get("followers_count", -1))
        else:
            print(f"[InstagramAPI] Error: Status {res.status_code}")
            return -1
    except Exception as e:
        print(f"[InstagramAPI] Exception: {str(e)}")
        return -1

📌 Facebook APIの連携

📄 .env設定

FB_PAGE_ID=your_facebook_page_id  
FB_TOKEN=your_facebook_token

🐍 facebookAPI.py

import os
import requests
from dotenv import load_dotenv

load_dotenv()

FB_PAGE_ID = os.getenv("FB_PAGE_ID")
FB_TOKEN = os.getenv("FB_TOKEN")

def get_facebook_follower_count():
    url = f"https://graph.facebook.com/v19.0/{FB_PAGE_ID}?fields=followers_count&access_token={FB_TOKEN}"
    try:
        res = requests.get(url)
        if res.status_code == 200:
            data = res.json()
            return int(data.get("followers_count", -1))
        else:
            print(f"[FacebookAPI] Error: Status {res.status_code}")
            return -1
    except Exception as e:
        print(f"[FacebookAPI] Exception: {str(e)}")
        return -1

🖼️ UIへの反映(例)

from API import instagramAPI, facebookAPI

fb_count = facebookAPI.get_facebook_follower_count()
ig_count = instagramAPI.get_instagram_follower_count()

if fb_count == -1:
    print("Facebook フォロワー数:取得失敗")
else:
    print(f"Facebook フォロワー数:{fb_count}")

if ig_count == -1:
    print("Instagram フォロワー数:取得失敗")
else:
    print(f"Instagram フォロワー数:{ig_count}")

📦 動作結果

Facebook フォロワー数:103人
Instagram フォロワー数:204人

または、失敗時は:

Facebook フォロワー数:取得失敗
Instagram フォロワー数:取得失敗

🔜 次回の予定

更新処理などを実装する予定です

完成まで、あと少し!
次回もぜひお楽しみに✨


🔗 シリーズリンク

📚 全記事まとめはこちら → シリーズまとめリンク

10
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?