Facebook の Graph API で リアクションを取得

API の仕様はこちら
[/{object-id}/reactions](https://developers.facebook.com/docs/graph-api/reference/v2.12/object/reactions)

２０１８年２月５日から、アクセス　トークンの条件が厳しくなりました。
![facebook_feb2001.png](https://qiita-image-store.s3.amazonaws.com/0/179446/655e472f-e82f-d91e-6662-2521d78a1386.png)

取得するプログラムです。
アクセス トークンは有効なものに変更して下さい。

```py3: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")
# ----------------------------------------------------------------
```

実行方法

```bash
./get_reactions.py '投稿のID' | jq .
```