简体   繁体   中英

Getting Reactions and Post type from Facebook Graph API

For a university project I am trying to extract information present on a public page present on Facebook. I know that a permission is required, but that is not the focus of my question.

I am writing the code needed for the verification phase and I am testing it on my own page. Currently I can get all the necessary data except:

  • The number/type of the various reactions (Like, Love, Hug, etc.) of each post.

  • The type of post (with photo, video, text only, link, etc.).

Reading the documentation, I can't figure out how to extract that informations. I can only see the reactions selected by me, but I can see the total reactions for each post (for example, I can see that a post has 7 reactions, but they are not divided into the various types).

Finally, I really have no idea how I can get the type of post analyzed.

Below is the code I wrote. I know it's not very pretty, I'm still learning. I'm still testing a few things, eventually all the data will be put into a dataframe.

Thank you in advance for your attention and any help you can give me.

token=""
def export_post_booster(token):

page_id=""
graph = facebook.GraphAPI(token)
posts = graph.request(page_id+'/posts')
count=1
while "paging" in posts: 
    for post in posts["data"]:

        shares = graph.request(post["id"]+"?fields=shares")
        reacts = graph.request(post["id"]+"/likes?summary=True")
        coms = graph.request(post["id"]+"?fields=comments.summary(true)")
        none = graph.request(post["id"]+"?fields=reactions.type(NONE).summary(true)")
        like = graph.request(post["id"]+"?fields=reactions.type(LIKE).summary(true)")
        love = graph.request(post["id"]+"?fields=reactions.type(LOVE).summary(true)")
        wow = graph.request(post["id"]+"?fields=reactions.type(WOW).summary(true)")
        haha = graph.request(post["id"]+"?fields=reactions.type(HAHA).summary(true)")
        sad = graph.request(post["id"]+"?fields=reactions.type(SAD).summary(true)")
        angry = graph.request(post["id"]+"?fields=reactions.type(ANGRY).summary(true)")
        coms = graph.request(post["id"]+"?fields=comments.summary(true)")

        print("----------------",count,"----------------")
        print("time :  ",post["created_time"])
        print("id   :",post["id"],"\n")

        if "message" in post:
            print("Text Post : ",post["message"])
        else:
            print("Text Post : NULL")

        try:
          print("shares :",shares["shares"]["count"])
        except:
          print("shares : 0")

        try:
          print("likes : ",reacts["summary"]["total_count"])
        except:
          print("likes : 0")

        try:
          print("none : ",none["summary"]["total_count"])
        except:
          print("none : 0")

        try:
          print("love : ",love["summary"]["total_count"])
        except:
          print("love : 0")

        try:
          print("wow : ",wow["summary"]["total_count"])
        except:
          print("wow : 0")

        try:
          print("sad : ",sad["summary"]["total_count"])
        except:
          print("sad : 0")

        try:
          print("love : ",love["summary"]["total_count"])
        except:
          print("love : 0")

        try:
          print("angry : ",angry["summary"]["total_count"])
        except:
          print("angry : 0")

        for i in range(0, len(coms["comments"]["data"])):
          print("><><><")              
          print("Comment Text: ",i)
          
          print(
                        {
                            "id_post":  coms["id"],
                            "data":     coms["comments"]["data"][i]["created_time"],
                            "message":  coms["comments"]["data"][i]["message"]
                        }
                )

        count=count+1
    try:
      posts=requests.get(posts["paging"]["next"]).json()
    except:
      print("end of posts")
      break

I got total reaction type with: https://graph.facebook.com/{postid}?fields=reactions.type({reactiontype}).limit(0).summary(total_count)&access_token={accesstoken} Reaction types available are: ['LIKE', 'LOVE', 'WOW', 'HAHA', 'ANGRY', 'SAD'] There might be some more, if you input a wrong reaction type the result JSON will contain the list of existing reaction types.

For post type I used: https://graph.facebook.com/{postid}?fields=status_type&access_token={accesstoken}

I'm a noob so there might be a better way to do this, maybe with less requests, but this get the job done for now

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM