-
-
Save ismailmechbal/7762c3bfd2c168a151b1856de36933f7 to your computer and use it in GitHub Desktop.
Grab likes et al for members of a Facebook group.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This is a really simple script: | |
##Grab the list of members of a Facebook group (no paging as yet...) | |
#Lets you do things like http://blog.ouseful.info/2012/12/05/emergent-social-interest-mapping-red-bull-racing-facebook-group/ | |
###For each member, try to grab their Likes | |
#USAGE | |
#> python fbLinksGrabber.py -group GROUPID -typ THING -FBTOKEN ETC | |
#THING is one of groups, likes, books etc | |
import urllib,json,csv,argparse | |
#Grab a copy of a current token from an example Facebook API call. eg from clicking a keyed link on: | |
#https://developers.facebook.com/docs/reference/api/examples/ | |
#Something a bit like this: | |
#AAAAAAITEghMBAOMYrWLBTYpf9ciZBLXaw56uOt2huS7C4cCiOiegEZBeiZB1N4ZCqHgQZDZD | |
parser = argparse.ArgumentParser(description='Generate social positioning map around a Facebook group') | |
parser.add_argument('-gid',default='2311573955',help='Facebook group ID') | |
#gid='2311573955' | |
parser.add_argument('-FBTOKEN',help='Facebook API token') | |
parser.add_argument('-typ',default='likes',help='Facebook group ID') | |
args=parser.parse_args() | |
if args.gid!=None: gid=args.gid | |
if args.FBTOKEN!=None: FBTOKEN=args.FBTOKEN | |
if args.typ!=None: typ=args.typ | |
import datetime | |
def getTimeStampedProjDirName(path,stub): | |
now = datetime.datetime.now() | |
ts = now.strftime("_%Y-%m-%d-%H-%M-%S") | |
return path+'/'+stub+ts | |
#Quick test - output file is simple 2 column CSV that we can render in Gephi | |
#fn='fbgroupliketest_'+str(gid)+'.csv' | |
fn=getTimeStampedProjDirName('.','fbgroup_'+typ+'_test_'+str(gid) ) | |
writer=csv.writer(open(fn+'.csv','wb+'),quoting=csv.QUOTE_ALL) | |
uids=[] | |
def getGroupMembers(gid): | |
gurl='https://graph.facebook.com/'+str(gid)+'/members?limit=5000&access_token='+FBTOKEN | |
data=json.load(urllib.urlopen(gurl)) | |
if "error" in data: | |
print "Something seems to be going wrong - check OAUTH key?" | |
print data['error']['message'],data['error']['code'],data['error']['type'] | |
exit(-1) | |
else: | |
print data | |
return data | |
def getX(X,uid,gid): | |
#X: likes | |
print "Get",X,"for",str(uid) | |
#Should probably implement at least a simple cache here | |
lurl="https://graph.facebook.com/"+str(uid)+"/"+X+"?access_token="+FBTOKEN | |
ldata=json.load(urllib.urlopen(lurl)) | |
print ldata | |
if len(ldata['data'])>0: | |
for i in ldata['data']: | |
o=[] | |
if 'name' in i: | |
#writer.writerow([str(uid),i['name'].encode('ascii','ignore')]) | |
o.append( str(uid) ) | |
o.append( i['name'].encode('ascii','ignore') ) | |
print str(uid),i['name'], | |
#We could colour nodes based on category, etc, though would require richer output format. | |
#In the past, I have used the networkx library to construct "native" graph based representations of interest networks. | |
if 'category' in i: | |
print i['category'], | |
o.append( i['category'] ) | |
if 'id' in i: | |
print i['id'], | |
o.append( i['id'] ) | |
writer.writerow( o ) | |
print '' | |
#For each user in the group membership list, get their likes | |
def parseGroupMembers(groupData,gid): | |
for user in groupData['data']: | |
uid=user['id'] | |
#writer.writerow([str(uid),str(gid)]) | |
#x is just a fudge used in progress reporting | |
x=0 | |
#Prevent duplicate fetches | |
if uid not in uids: | |
#getLikes(user['id'],gid) | |
getX(typ,user['id'],gid) | |
uids.append(uid) | |
#Really crude progress reporting | |
print x | |
x=x+1 | |
#need to handle paging? | |
#parse next page URL and recall this function | |
groupdata=getGroupMembers(gid) | |
parseGroupMembers(groupdata,gid) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment