Created
December 11, 2012 10:39
-
-
Save psychemedia/4257646 to your computer and use it in GitHub Desktop.
Facebook likes grabber
This file contains 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...) | |
###For each member, try to grab their Likes | |
import urllib,csv,argparse | |
##INSTRUCTIONS: | |
#1) Download this file, eg to ~/Downloads/fbGrab.py | |
#2) Get a Facebook group ID and paste into gid below | |
#3) Get a Facebook auth token (click link from Facebook API page) and paste in FBTOKEN below | |
#4) Save the file | |
#5) in the terminal, cd to where your file is (eg cd ~/Downloads/fbGroup.py) | |
#6) run the script with: python fbGrab.py | |
#7) the results are dropped into a CSV file in the same directory | |
#---- | |
####CONFIGURE THESE BITS | |
#A group ID | |
gid='YOURGROUPIDHERE' | |
#A current access token | |
FBTOKEN='YOURTOKENHERE' | |
#How many group members | |
glim=1000 | |
#------ | |
try: import simplejson as json | |
except ImportError: import json | |
#Grab a copy of a current token from an example Facebook API call. | |
#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') | |
#Quick test - output file is simple 2 column CSV that we can render in Gephi | |
fn='fbgroupliketest_'+str(gid)+'.csv' | |
writer=csv.writer(open(fn,'wb+'),quoting=csv.QUOTE_ALL) | |
uids=[] | |
def getGroupMembers(gid): | |
gurl='https://graph.facebook.com/'+str(gid)+'/members?limit='+str(glim)+'&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: | |
return data | |
#Grab the likes for a particular Facebook user by Facebook User ID | |
def getLikes(uid,gid): | |
#Should probably implement at least a simple cache here | |
lurl="https://graph.facebook.com/"+str(uid)+"/likes?access_token="+FBTOKEN | |
ldata=json.load(urllib.urlopen(lurl)) | |
print ldata | |
if len(ldata['data'])>0: | |
for i in ldata['data']: | |
if 'name' in i: | |
writer.writerow([str(uid),i['name'].encode('ascii','ignore')]) | |
#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 str(uid),i['name'],i['category'] | |
#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) | |
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) |
can you make for grabbing user id only?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
can i get to make in webbase...
pm me..