1
\$\begingroup\$

Given below is code which I wrote to provide an easy access to Facebook graph API.

import simplejson as json
import urllib

class Facebook ():

def __init__ (self, access_token):

    self.access_token = access_token
    self.id = None
    self.name = None

def user_information (self, fbuser = "me"):

    """ 
    returns User Information ; fbuser can be the facebook id of any user ;
    for the current user it is 'me'

    """
    profile = json.load(urllib.urlopen("https://graph.facebook.com/" + fbuser + "?" + urllib.urlencode(dict(access_token=self.access_token))))
    self.id = str(profile["id"])
    self.name = str(profile["name"])
    user = dict(key_name=str(profile["id"]), id=str(profile["id"]),  name=profile["name"], access_token=self.access_token, profile_url=profile["link"])
    return user

def get_friends (self, maxPage = 4, fbuser = "me"):

    """
    Gets the list of friends , maxPage : Pages need to be accessed ; 4 can cober most of the friendlist ; will ensure that data is returned within 60 secs
    """
    friends = json.load(urllib.urlopen("https://graph.facebook.com/" + fbuser + "/friends?" + urllib.urlencode(dict(access_token=self.access_token))))
    Friends = []
    while "next" in friends["paging"]:
        if maxPage == 0:
            break
        maxPage = maxPage - 1
        Friends = Friends + friends["data"]
        friends = json.load(urllib.urlopen(friends["paging"]["next"]))
    return Friends

def get_user_wall (self, max_pages = 2, fbuser = "me"):
    """
    gets User Wall 
    """

    user_feeds = json.load(urllib.urlopen("https://graph.facebook.com/" + fbuser + "/feed?" + urllib.urlencode(dict(access_token=self.access_token))))
    Data = []
    while True :
        try:
            Data = Data + user_feeds["data"]
            user_feeds = json.load(urllib.urlopen(user_feeds["paging"]["next"]))
            max_pages = max_pages-1
            if max_pages < 1 :
                break
        except:
            break
    return Data

def get_user_comments_story (self, fbuser = "me"):
    """
    gets User Comments 
    """

    UserWall = self.get_user_wall(fbuser = fbuser)
    returnData = []

    for feed in UserWall :
        if "story" in feed:
            returnData.append(feed["story"])

    return returnData 

def get_user_comments_picture(self, fbuser = "me"):

    UserWall = self.get_user_wall(fbuser = fbuser)
    returnData = []

    for feed in UserWall :
        if "picture" in feed:
            returnData.append(feed["picture"])

    return returnData 

def get_user_comments_from(self, fbuser = "me"):

    UserWall = self.get_user_wall(fbuser = fbuser)
    returnData = []

    for feed in UserWall :
        if "from" in feed:
            returnData.append(feed["from"])

    return returnData 

def get_user_comments_name(self, fbuser = "me"):

    UserWall = self.get_user_wall(fbuser = fbuser)
    returnData = []

    for feed in UserWall :
        if "name" in feed:
            returnData.append(feed["name"])

    return returnData 

def get_user_comments_caption(self, fbuser = "me"):

    UserWall = self.get_user_wall(fbuser = fbuser)
    returnData = []

    for feed in UserWall :
        if "caption" in feed:
            returnData.append(feed["caption"])

    return returnData 

def get_user_comments_description(self, fbuser = "me"):

    UserWall = self.get_user_wall(fbuser = fbuser)
    returnData = []

    for feed in UserWall :
        if "description" in feed:
            returnData.append(feed["description"])

    return returnData 

def get_user_comments_comments(self, fbuser = "me"):

    UserWall = self.get_user_wall(fbuser = fbuser)
    returnData = []

    for feed in UserWall :
        if "comments" in feed:
            returnData.append(feed["comments"])

    return returnData 

def get_user_comments_updated_time(self, fbuser = "me"):

    UserWall = self.get_user_wall(fbuser = fbuser)
    returnData = []

    for feed in UserWall :
        if "updated_time" in feed:
            returnData.append(feed["updated_time"])

    return returnData 

def get_user_comments_type(self, fbuser = "me"):

    UserWall = self.get_user_wall(fbuser = fbuser)
    returnData = []

    for feed in UserWall :
        if "type" in feed:
            returnData.append(feed["type"])

    return returnData 

def get_user_comments_id(self, fbuser = "me"):

    UserWall = self.get_user_wall(fbuser = fbuser)
    returnData = []

    for feed in UserWall :
        if "id" in feed:
            returnData.append(feed["id"])

    return returnData 

def get_user_comments_likes(self, fbuser = "me"):

    UserWall = self.get_user_wall(fbuser = fbuser)
    returnData = []

    for feed in UserWall :
        if "likes" in feed:
            returnData.append(feed["likes"])

    return returnData 

def get_user_likes (self, fbuser = "me"):

    user_likes = json.load(urllib.urlopen("https://graph.facebook.com/" + fbuser + "/likes?" + urllib.urlencode(dict(access_token=self.access_token))))
    returnData = []

    while True:
        try:
            returnData = returnData + [x["name"] for x in user_likes["data"]]
            user_likes = json.load(urllib.urlopen(user_likes["paging"]["next"]))
        except:
            break

    return returnData

def get_user_movies (self, fbuser = "me"):

    user_movies = json.load(urllib.urlopen("https://graph.facebook.com/" + fbuser + "/movies?" + urllib.urlencode(dict(access_token=self.access_token))))
    returnData = []

    while True:

        try:
            returnData = returnData + [ x["name"] for x in user_movies["data"]]
            user_movies = json.load(urllib.urlopen(user_movies["paging"]["next"]))
        except:
            break


    return returnData

def get_user_music (self, fbuser = "me"):

     user_music = json.load(urllib.urlopen("https://graph.facebook.com/" + fbuser + "/music?" + urllib.urlencode(dict(access_token=self.access_token))))
     returnData = []

     while True:

         try:
             returnData = returnData + [ x["name"] for x in user_music["data"]]
             user_music = json.load(urllib.urlopen(user_music["paging"]["next"]))
         except:
            break

     return returnData

def get_user_books (self, fbuser = "me"):

     user_book = json.load(urllib.urlopen("https://graph.facebook.com/" + fbuser + "/books?" + urllib.urlencode(dict(access_token=self.access_token))))
     returnData = []

     while True:

         try:
             returnData = returnData + [ x["name"] for x in user_book["data"]]
             user_book = json.load(urllib.urlopen(user_book["paging"]["next"]))
         except:
            break

     return returnData

def get_user_notes (self, fbuser = "me"):

     user_notes = json.load(urllib.urlopen("https://graph.facebook.com/" + fbuser + "/notes?" + urllib.urlencode(dict(access_token=self.access_token))))
     returnData = []

     while True:

         try:
             returnData = returnData + [ x["name"] for x in user_notes["data"]]
             user_notes = json.load(urllib.urlopen(user_notes["paging"]["next"]))
         except:
            break

     return returnData

def get_user_photos (self, fbuser = "me"):

     user_photos = json.load(urllib.urlopen("https://graph.facebook.com/" + fbuser + "/photos?" + urllib.urlencode(dict(access_token=self.access_token))))
     returnData = []

     while True:

         try:
             returnData = returnData + [ x["name"] for x in user_photos["data"]]
             user_photos = json.load(urllib.urlopen(user_photos["paging"]["next"]))
         except:
            break

     return returnData

def get_user_events(self, fbuser = "me"):

     user_notes = json.load(urllib.urlopen("https://graph.facebook.com/" + fbuser + "/events?" + urllib.urlencode(dict(access_token=self.access_token))))
     returnData = []

     while True:

         try:
             returnData = returnData + [ x for x in user_notes["data"]]
             user_notes = json.load(urllib.urlopen(user_notes["paging"]["next"]))
         except:
            break

     return returnData

def get_user_groups (self, fbuser = "me"):

     user_notes = json.load(urllib.urlopen("https://graph.facebook.com/" + fbuser + "/groups?" + urllib.urlencode(dict(access_token=self.access_token))))
     returnData = []

     while True:

         try:
             returnData = returnData + [ x["name"] for x in user_notes["data"]]
             user_notes = json.load(urllib.urlopen(user_notes["paging"]["next"]))
         except:
            break

     return returnData

def get_user_places (self, fbuser = "me"):

     user_places = json.load(urllib.urlopen("https://graph.facebook.com/" + fbuser + "/locations?" + urllib.urlencode(dict(access_token=self.access_token))))
     returnData = {}

     while True:

         try:
             tempData = [ x["place"]["name"] for x in user_places["data"]]
             for place in tempData:
                 if place in returnData:
                     returnData[place] = returnData[place]+1
                 else:
                     returnData[place] = 0
             user_places = json.load(urllib.urlopen(user_places["paging"]["next"]))
         except:
            break

     return returnData

def all_user_data (self):

    String = ""

    String = String + "\n USer Information \n"
    String = String + "________________________________________\n\n\n"
    String = String + str(self.user_information('502291155'))
    """
    String = String + "_____________________________________________________________________________________\n\n\n"
    String = String + "\n Friends \n"
    String = String + "________________________________________\n\n\n"
    String = String + str(self.get_friends())


    String = String + "_____________________________________________________________________________________\n\n\n"
    String = String + "\n user_books \n"
    String = String + "________________________________________\n\n\n"
    String = String + str(self.get_user_books())
    """

    return String
    """
    String = String + "_____________________________________________________________________________________\n\n\n"
    String = String + "\n user_comments_caption \n"
    String = String + "________________________________________\n\n\n"
    String = String + str(self.get_user_comments_caption())

    return String
    String = String + "_____________________________________________________________________________________\n\n\n"
    String = String + "\n user_comments_comments \n"
    String = String + "________________________________________\n\n\n"
    String = String + str(self.get_user_comments_comments())

    String = String + "_____________________________________________________________________________________\n\n\n"
    String = String + "\n user_comments_description \n"
    String = String + "________________________________________\n\n\n"
    String = String + str(self.get_user_comments_description())

    String = String + "_____________________________________________________________________________________\n\n\n"
    String = String + "\n user_comments_from \n"
    String = String + "________________________________________\n\n\n"
    String = String + str(self.get_user_comments_from())

    String = String + "_____________________________________________________________________________________\n\n\n"
    String = String + "\n user_comments_id \n"
    String = String + "________________________________________\n\n\n"
    String = String + str(self.get_user_comments_id())

    String = String + "_____________________________________________________________________________________\n\n\n"
    String = String + "\n user_comments_likes \n"
    String = String + "________________________________________\n\n\n"
    String = String + str(self.get_user_comments_likes())

    return String """

def all_user_data1 (self):

    String = ""

    String = String + "_____________________________________________________________________________________\n\n\n"
    String = String + "\n user_comments_name \n"
    String = String + "________________________________________\n\n\n"
    String = String + str(self.get_user_comments_name())

    String = String + "_____________________________________________________________________________________\n\n\n"
    String = String + "\n user_comments_picture \n"
    String = String + "________________________________________\n\n\n"
    String = String + str(self.get_user_comments_picture())

    String = String + "_____________________________________________________________________________________\n\n\n"
    String = String + "\n user_comments_story \n"
    String = String + "________________________________________\n\n\n"
    String = String + str(self.get_user_comments_story())

    String = String + "_____________________________________________________________________________________\n\n\n"
    String = String + "\n user_comments_type \n"
    String = String + "________________________________________\n\n\n"
    String = String + str(self.get_user_comments_type())



    String = String + "_____________________________________________________________________________________\n\n\n"
    String = String + "\n user_comments_updated_time \n"
    String = String + "________________________________________\n\n\n"
    String = String + str(self.get_user_comments_updated_time())

    String = String + "_____________________________________________________________________________________\n\n\n"
    String = String + "\n user_events \n"
    String = String + "________________________________________\n\n\n"
    String = String + str(self.get_user_events())

    String = String + "_____________________________________________________________________________________\n\n\n"
    String = String + "\n user_groups \n"
    String = String + "________________________________________\n\n\n"
    String = String + str(self.get_user_groups())

    String = String + "_____________________________________________________________________________________\n\n\n"
    String = String + "\n user_likes \n"
    String = String + "________________________________________\n\n\n"
    String = String + str(self.get_user_likes())

    String = String + "_____________________________________________________________________________________\n\n\n"
    String = String + "\n user_movies \n"
    String = String + "________________________________________\n\n\n"
    String = String + str(self.get_user_movies())

    String = String + "_____________________________________________________________________________________\n\n\n"
    String = String + "\n user_music \n"
    String = String + "________________________________________\n\n\n"
    String = String + str(self.get_user_music())

    String = String + "_____________________________________________________________________________________\n\n\n"
    String = String + "\n user_notes \n"
    String = String + "________________________________________\n\n\n"
    String = String + str(self.get_user_notes())

    String = String + "_____________________________________________________________________________________\n\n\n"
    String = String + "\n user_photos \n"
    String = String + "________________________________________\n\n\n"
    String = String + str(self.get_user_photos())



    String = String + "_____________________________________________________________________________________\n\n\n"
    String = String + "\n user_places \n"
    String = String + "________________________________________\n\n\n"
    String = String + str(self.get_user_places())

    String = String + "_____________________________________________________________________________________\n\n\n"
    String = String + "\n user_wall \n"
    String = String + "________________________________________\n\n\n"
    String = String + str(self.get_user_wall())

    return String

I also have similar code for Twitter access:

import tweepy
import TwitterConfig as config_twitter

class Twitter :

def __init__(self):
    self.CONSUMER_KEY = config_twitter.get_consumer_key() 
    self.CONSUMER_SECRET = config_twitter.get_consumer_secret()
    self.oauth_token = None
    self.oauth_verifier = None
    self.api = None

def get_auth_url(self):
    self.auth = tweepy.OAuthHandler(self.CONSUMER_KEY, self.CONSUMER_SECRET)
    return self.auth.get_authorization_url()

def get_request_token(self):
    return (self.auth.request_token.key,self.auth.request_token.secret)

def set_token(self,token):
    self.oauth_token = token

def set_verifier(self,verifier):
    self.oauth_verifier = verifier

def set_request_token (self,ReqToken):
    self.request_token = ReqToken

def get_access_token (self):
    self.auth = tweepy.OAuthHandler(self.CONSUMER_KEY, self.CONSUMER_SECRET)
    token = self.request_token
    #session.delete('request_token')
    self.auth.set_request_token(token[0],token[1])
    self.auth.get_access_token(self.oauth_verifier)

def authorize (self):
    key = self.auth.access_token.key
    secret = self.auth.access_token.secret
    self.auth = tweepy.OAuthHandler(self.CONSUMER_KEY, self.CONSUMER_SECRET)
    self.auth.set_access_token(key, secret)
    self.api = tweepy.API(self.auth)

def update_status(self,status):
    if self.oauth_token == None or self.oauth_verifier == None or self.api == None :
        return "Verification Problem"
    self.api.update_status (status)
    return "Done"

def user_information (self):
    # returns information of the authenticate user
    if self.oauth_token == None or self.oauth_verifier == None or self.api == None :
        return "Verification Problem"
    return self.api.me()

def get_friends (self):
    if self.oauth_token == None or self.oauth_verifier == None or self.api == None :
        return ["Verification Problem"]
    return self.api.GetFriends(self.user_information().name)

def get_followers (self):
    if self.oauth_token == None or self.oauth_verifier == None or self.api == None :
        return ["Verification Problem"]
    return self.api.GetFollowers()

def get_followers_id (self):
    if self.oauth_token == None or self.oauth_verifier == None or self.api == None :
        return "Verification Problem"
    return self.api.followers_ids()

def get_friends_ids (self):
    # returns ids of the friends
    if self.oauth_token == None or self.oauth_verifier == None or self.api == None :
        return "Verification Problem"
    return self.api.friends_ids()

def get_rate_limit_status (self):
    #returns the rate limit status of the authenticated user
    return self.api.rate_limit_status()

def get_tweets(self):
    if self.oauth_token == None or self.oauth_verifier == None or self.api == None :
        return ["Verification Problem"]
    me = self.user_information()
    statuses = self.api.GetUseerTimeline(me.name)
    return statuses

def get_messages(self):
    if self.oauth_token == None or self.oauth_verifier == None or self.api == None :
        return ["Verification Problem"]
    return self.api.GetDirectMessages()

The complete code is available here. I'd appreciate any and all comments-- correctness, style, best practices, logging, error handling.

\$\endgroup\$

2 Answers 2

5
\$\begingroup\$
import urllib

You should take a look at Requests, a much simpler, cleaner module which is becoming the defacto standard for doing this sort of stuff.

self.id = str(profile["id"])
self.name = str(profile["name"])

If facebook returns data which doesn't have an ID or Name attribute, this will raise an unhandled exception. You should either wrap these clauses with Try/except KeyError clauses, or provide a default with profile.get(id, None).

user = dict(key_name=str(profile["id"]), id=str(profile["id"]),  name=profile["name"], access_token=self.access_token, profile_url=profile["link"])

This is a rather long line, and probably violates PEP-8's line length recommendations. Not everybody/every project follow PEP8 on line length -- Django, for example doesn't always, but it's something to be aware of. If pyflakes or other linters will catch this for you.

Friends = []

generally uppercase vars are reserved for classes or class factories. In general, having friends and a Friends variables just seems like asking for trouble -- way to much potential confusion there. I'd rename friends to something like raw_friend_data and Friends to friends or friend_list.

while "next" in friends["paging"]:
    if maxPage == 0:

More pythonic is:

if not max_page:

In general, use snake_case rather than CamelCase.

    try:
        Data = Data + user_feeds["data"]
        user_feeds = json.load(urllib.urlopen(user_feeds["paging"]["next"]))
        max_pages = max_pages-1
        if max_pages < 1 :
            break
    except:
        break

Why have you wrapped this chunk of code in a Try/Except but not other similar chunks? It's not a bad idea, but I'd refactor to:

url = "https://graph.facebook.com/%s/feed?" % fbuser
try:
    response = requests.get(url,params=dict(access_token=self.access_token)
    user_feeds = json.load(response)
except ConnectionError:
    print("There was a problem connecting to FacePalm.")
    Return False

user_wall_data = user_wall_data + user_feeds["data"]         
max_pages = max_pages-1
if not max_pages:
    break
return user_wall_data

Key points about the above:

It's generally a Bad Idea to have a generic except clause, because it will hide every error the code inside Try raises, making it a major pain to debug. try/except should be used for very specific conditions, such as: I'm getting a bunch of data from FacePalm, it maybe foobar'd, in which case my code could raise a FooBarException, which I can then handle by Quuxing the Bars.

def all_user_data (self):

    String = ""
    String = String + "\n USer Information \n"

This type of data should be segregated from your code into a separate data file which is then read into the main script. This is very easy to do: you can create and read from a file object with file = open('/path/to/data/', 'r+') and then file.read() to get the data. You may want to take a look a the python docs for file objects to familiarize yourself with the reading/writing modes in which you can open a file, as well as opening a file with a context manager, which is a nice pattern that allows you to do stuff like:

with open('/path/to/data/', 'r+')` as file: 
    foo_data = file.read()

One of the advantages of this is that the context manager will close the file for you when its block of code exits.

\$\endgroup\$
2
\$\begingroup\$

A very important point that was not mentioned is too much duplication in your code. You could generalize many methods, for example get_user_comments_comments and get_user_comments_updated_time, like this:

def get_user_data(self, fbuser, field):
    wall = self.get_user_wall(fbuser=fbuser)
    return [feed[field] for feed in wall if field in feed]

def get_user_comments_comments(self, fbuser = "me"):
    self.get_user_data(fbuser, 'comments')

def get_user_comments_updated_time(self, fbuser = "me"):
    self.get_user_data(fbuser, 'updated_time')

Also notice the simplified code using list comprehensions.

\$\endgroup\$
1
  • \$\begingroup\$ I also noticed it after posting my question here. I also posted a changed version of my code here. But unfortunately it was deleted by admin. So I have posted it as a follow up question here:codereview.stackexchange.com/questions/49419/… \$\endgroup\$
    – Pranav Raj
    May 10, 2014 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.