Facebook의 Graph API에서 반응을 얻습니다.

API의 사양은 이쪽
/{object-id}/reactions

2018년 2월 5일부터, 액세스 토큰의 조건이 엄격해졌습니다.


취득하는 프로그램입니다.
액세스 토큰을 유효한 것으로 변경하십시오.

get_reactions.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#   get_reactions.py
#
#                   Feb/20/2018
#
# ----------------------------------------------------------------
import sys
import json
import requests
#
# ----------------------------------------------------------------
# [4]:
def get_fb_proc(id):
#
access_token="FG4EFFBgb938BAO2ouOc1235pjXC8YgsXZBYRnZB8nNvwZCP7dJDTXq7pRrZCVXDX4rmH9hvEZCUj8ZA2ihBlMKMoVWWwzaaRzI8tqA6zCAhgZB12idZCYxIxXNmC112GypCJlnsGdUgLLwAYp0PAQinfL3J123f6or2JYj65bt0QSp1umNabcfg"
#
    sys.stderr.write("*** get_fb_proc *** start ***\n")
#
    dict_aa = get_fb_base_proc(access_token,id)
#
    dict_bb = get_fb_reactions_proc(access_token,id)
#
    dict_aa['data'] = dict_bb['data']
#
    reactions_analize_proc(dict_aa)
#
    sys.stderr.write("*** get_fb_proc *** end ***\n")
#
    return  dict_aa
# ----------------------------------------------------------------
# [4-4]:
def get_fb_base_proc(access_token,id):
#
    dict_aa = {}
    sys.stderr.write("*** get_fb_base_proc *** start ***\n")
#
    url = "https://graph.facebook.com/v2.12/"
    fields = 'id,name,created_time'
    url_target = url + id
    params={
        "access_token": access_token,
        "fields": fields,
        }
#
    try:
        rr=requests.get(url_target,params=params)
        sys.stderr.write(str(rr.status_code) + '\n')
        json_str = rr.text
        dict_aa = json.loads(json_str)
    except Exception as ee:
        sys.stderr.write("*** error *** in requests.get ***\n")
        sys.stderr.write(str(ee) + "\n")
#
    sys.stderr.write("*** get_fb_base_proc *** end ***\n")
#
    return dict_aa
# ----------------------------------------------------------------
# [4-8]:
def get_fb_reactions_proc(access_token,id):
#
    sys.stderr.write("*** get_fb_reactions_proc *** start ***\n")
#
    url_target = "https://graph.facebook.com/v2.12/" + id + "/reactions"
    fields = 'name,username,link,profile_type,type'
    params={
        "access_token": access_token,
        "fields": fields,
        "limit": 1000
        }
#
    try:
        rr=requests.get(url_target,params=params)
        sys.stderr.write(str(rr.status_code) + '\n')
    except Exception as ee:
        sys.stderr.write("*** error *** in requests.get ***\n")
        sys.stderr.write(str(ee) + "\n")
#
    json_str = rr.text
#
    dict_aa = json.loads(json_str)
    sys.stderr.write("len = %d\n" % len(dict_aa['data']))
#
    sys.stderr.write("*** get_fb_reactions_proc *** end ***\n")
    return  dict_aa
# ----------------------------------------------------------------
def reactions_analize_proc(dict_aa):
#
    nn_haha = 0
    nn_like = 0
    nn_love = 0
    nn_wow = 0
    nn_others = 0
#
    for unit in dict_aa['data']:
        type = unit['type']
        if (type == "LIKE"):
            nn_like += 1
        elif (type == "LOVE"):
            nn_love += 1
        elif (type == "WOW"):
            nn_wow += 1
        elif (type == "HAHA"):
            nn_haha += 1
        else:
            nn_others += 1
#
    sys.stderr.write("\tnn_like = %d\n" % nn_like)
    sys.stderr.write("\tnn_love = %d\n" % nn_love)
    sys.stderr.write("\tnn_wow = %d\n" % nn_wow)
    sys.stderr.write("\tnn_haha = %d\n" % nn_haha)
    sys.stderr.write("\tnn_others = %d\n" % nn_others)
# ----------------------------------------------------------------
sys.stderr.write("*** get_reactions.py *** start ***\n")
#
id_in = str(sys.argv[1])
sys.stderr.write("\tid = " + id_in + "\n")
#
#
try:
    dict_aa = get_fb_proc(id_in)
    print(json.dumps(dict_aa))
except Exception as ee:
    sys.stderr.write("*** error *** in convert_proc ***\n")
    sys.stderr.write("id = " + id_in + "\n")
    sys.stderr.write(str(ee) + "\n")
#
sys.stderr.write("*** get_reactions.py *** end ***\n")
# ----------------------------------------------------------------

실행 방법
./get_reactions.py '投稿のID' | jq .

좋은 웹페이지 즐겨찾기