Question

I am creating a new website. I want to promote it using another my topic-related web service. I want to send some gifts to people which popularized my first website and fanpage. How to filter out lets say 20 users which likes/shares/comments most of my posts?

Any suitable programming language will be good.

[EDIT]

Ok... to be honest I looking a way to parse a fanpage that is not mine. I want to send gifts to the most active users of fanpage of my competition, to simply bribe them a little :)

Was it helpful?

Solution

There are a number of ways, I'll start with the easiest...

  1. Say there's a brand name or #hashtag involved then you could user the search API as such: https://graph.facebook.com/search?q=watermelon&type=post&limit=1000 and then iterate over the data, say the latest 1000 (the limit param) to find out mode user (the one that comes up the most) out of all the statuses.

  2. Say it's just a page, then you can access the /<page>/posts end point (eg: https://developers.facebook.com/tools/explorer?method=GET&path=cocacola%2Fposts) as that'll give you a list of the latest posts (they're paginated so you can iterate over the results) and this'll include a list of the people who like the posts and who comment on them; you can then find out the mode user and so on.

In terms of the code you can use anything, you can even run this locally on your machine using a simple web server (such as MAMP or WAMP, etc...) or CLI. The response is all JSON and modern languages are able to handle this. Here's a quick example I knocked up for the first method in Python:

import json
import urllib2
from collections import Counter

def search():
  req = urllib2.urlopen('https://graph.facebook.com/search?q=watermelon&type=post')
  res = json.loads(req.read())
  users = []

  for status in res['data']:
    users.append(status['from']['name'])

  count = Counter(users)

  print count.most_common()

if __name__ == '__main__':
  search()

I've stuck it up on github if you want to refer to it later: https://github.com/ahmednuaman/python-facebook-search-mode-user/blob/master/search.py

When you run the code it'll return an ordered list of the mode users within that search, eg the ones who've posted the most comments with the specific search tag. This can be easily adapted for the second method should you wish to use it.

OTHER TIPS

Based on Ahmed Nuaman answer (please also give them +1), I have prepared this piece of code:

example of usage:

To analyze most active facebook users of http://www.facebook.com/cern

$ python FacebookFanAnalyzer.py cern likes

$ python FacebookFanAnalyzer.py cern comments

$ python FacebookFanAnalyzer.py cern likes comments

notes: shares and inner comments are not supported

file: FacebookFanAnalyzer.py

# -*- coding: utf-8 -*-
import json
import urllib2
import sys
from collections import Counter
reload(sys)
sys.setdefaultencoding('utf8')
###############################################################
###############################################################
#### PLEASE PASTE HERE YOUR TOKEN, YOU CAN GENERETE IT ON:
####    https://developers.facebook.com/tools/explorer
#### GENERETE AND PASTE NEW ONE, WHEN THIS WILL STOP WORKING

token = 'AjZCBe5yhAq2zFtyNS4tdPyhAq2zFtyNS4tdPw9sMkSUgBzF4tdPw9sMkSUgBzFZCDcd6asBpPndjhAq2zFtyNS4tsBphqfZBJNzx'

attrib_limit = 100
post_limit = 100
###############################################################
###############################################################


class FacebookFanAnalyzer(object):

    def __init__(self, fanpage_name, post_limit, attribs, attrib_limit):
        self.fanpage_name = fanpage_name
        self.post_limit = post_limit
        self.attribs = attribs
        self.attrib_limit = attrib_limit
        self.data={}

    def make_request(self, attrib):
        global token
        url = 'https://graph.facebook.com/' + self.fanpage_name + '/posts?limit=' + str(self.post_limit) + '&fields=' + attrib + '.limit('+str(self.attrib_limit)+')&access_token=' + token
        print "Requesting '" + attrib + "' data: " + url
        req = urllib2.urlopen(url)
        res = json.loads(req.read())

        if res.get('error'):
            print res['error']
            exit()

        return res

    def grep_data(self, attrib):
        res=self.make_request(attrib)
        lst=[]
        for status in res['data']:
            if status.get(attrib):
                for person in status[attrib]['data']:
                    if attrib == 'likes':
                        lst.append(person['name'])
                    elif attrib == 'comments':
                        lst.append(person['from']['name'])
        return lst


    def save_as_html(self, attribs):
        filename = self.fanpage_name + '.html'
        f = open(filename, 'w') 

        f.write(u'<html><head></head><body>')
        f.write(u'<table border="0"><tr>')
        for attrib in attribs:
            f.write(u'<td>'+attrib+'</td>')
        f.write(u'</tr>')

        for attrib in attribs:
            f.write(u'<td valign="top"><table border="1">')

            for d in self.data[attrib]:
                f.write(u'<tr><td>' + unicode(d[0]) + u'</td><td>' +unicode(d[1]) + u'</td></tr>')

            f.write(u'</table></td>')

        f.write(u'</tr></table>')
        f.write(u'</body>')
        f.close()
        print "Saved to " + filename

    def fetch_data(self, attribs):
        for attrib in attribs:
            self.data[attrib]=Counter(self.grep_data(attrib)).most_common()

def main():
    global post_limit
    global attrib_limit

    fanpage_name = sys.argv[1] 
    attribs = sys.argv[2:] 

    f = FacebookFanAnalyzer(fanpage_name, post_limit, attribs, attrib_limit)
    f.fetch_data(attribs)
    f.save_as_html(attribs)

if __name__ == '__main__':
    main()

Output:

Requesting 'comments' data: https://graph.facebook.com/cern/posts?limit=50&fields=comments.limit(50)&access_token=AjZCBe5yhAq2zFtyNS4tdPyhAq2zFtyNS4tdPw9sMkSUgBzF4tdPw9sMkSUgBzFZCDcd6asBpPndjhAq2zFtyNS4tsBphqfZBJNzx
Requesting 'likes' data: https://graph.facebook.com/cern/posts?limit=50&fields=likes.limit(50)&access_token=AjZCBe5yhAq2zFtyNS4tdPyhAq2zFtyNS4tdPw9sMkSUgBzF4tdPw9sMkSUgBzFZCDcd6asBpPndjhAq2zFtyNS4tsBphqfZBJNzx
Saved to cern.html

enter image description here

Read the list of posts on the page at the page's /feed connection and track the user IDs of those users who posted and commented on each post, building a list of who does it the most often.

Then store those somewhere and use the stored list in the part of your system which decides who to send the bonuses to.

e.g.

http://graph.facebook.com/cocacola/feed returns all the recent posts on the cocacola page, and you could track the IDs of the posters, commenters, likers, to determine who are the most active users

write a php or jquery script which is executed when user clicks like or share on your website just before actually sharing/like to fb and record the user info and the post he/she shared/liked. Now you can track who shared your post the most.

PHP/Jquery script will act as middle man, so dont use facebook share/like script directly. I will try to find the code I have written for this method. I have used PHP & Mysql. Try to use JQuery this will give a better result in terms of hidding the process (I mean data will be recorded without reloading the page).

Your question is nice, but it is quite hard.. (Actually, in the beginning, there's a thing that came from my mind that this is impossible. So, I build a quite different solution...) One of the best ways is to create a network where your viewers can register in the form that required the official URLs of their social networking pages, and also, they could choose that they doesn't have this kind of network:

“Do you want to share some of our page? Please register here first..”

So, they can get a specific URL that they've wanted to share when they're in your website, but they doesn't know the they're in tracing when they visited that specific URL.. (Every time a specific URL get visited, an I.P. get tracked and the number of visits get ++1 in a database.) Give them a dynamic URL on the top of your website that's in text area of every pages to track them. Or use scripting to automated the adding of a tracing query string on the URLs of your site.

I think there's a free software to build an affiliate network to make this easy! If your viewers really love your website, they'll registered to be an affiliate. But this thing is different, affiliate network is quite different from a network that mentioned in the paragraphs above..

But I think, you can also use Google Analytics to fully trace some referrals that didn't came from the URLs with dynamic QUERY STRING like Digital Point, but not from the other social network like Facebook 'cause you wouldn't get the exact Referral Paths with that kind of social network because of the query path. However you can use it to track the other networks. Also, AddThis Analytics is good for non-query string URLs.

The two kinds of referrals on Google Analytics are under of “Traffic Sources” menu of STANDARD REPORTS..

  • Traffic Sources
    • Sources
      • Referrals
    • Social
      • Network Referrals

This answer is pretty messy, but sometimes, quite useful.. Other than that? Please check these links below:

  1. Publishing with an App Access Token - Facebook Developers
  2. Facebook for Websites - Facebook Developers
  3. Like - Facebook Developers
  4. Open Graph Overview - Facebook Developers
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top