mcottondesign

Loving Open-Souce One Anonymous Function at a Time.

work

Our users like to share the deal they just received with their friends on Facebook. We would like to know how well those links do and what kind of traffic they receive. awe.sm is a company that does exactly that. This blog post is about showing how easy it is to integrate into python and Django.

def facebook_share_checkin(checkin, points):
    try:
        # I removed the code for getting the user token
        graph = facebook.GraphAPI(token)

        # The facebook API specifies what they want as a payload
        attachment = dict()
        attachment['picture'] = consts.LOGO
        attachment['name'] = checkin.location.name
        msg = 'I just checked in at %s using Qliq' % checkin.location.name
        attachment['link'] = consts.SOCIAL_NETWORK_DEFAULT_LINK

        # Post it the the user's wall
        graph.put_wall_post(msg, attachment)

    except facebook.GraphAPIError as e:
        logger.exception('GraphAPIError')
    except SocialNetworkCredentials.DoesNotExist:
        logger.exception('Facebook credentials error')

This is real standard and nothing exciting. The link we are sharing is the same for every user and comes from a file of constants.

attachment['link'] = consts.SOCIAL_NETWORK_DEFAULT_LINK

Here is how we would use awe.sm to create a custom link per user and see how well that link does in the real world.

Just above the previous line we will make a network request to awe.sm with 5 parameters

- v: this is ther version of the api we will be using
- url: this is our original url we were using
- key: this is our developer key
- tool: this is the id of the tool we are using
- channel: this is going to be `facebook-post`

Now we just need to make the request and using the great python requests library

      r = requests.post('http://api.awe.sm/url.txt', 
          { 'v': 3, 
            'url': 'http://mcottondesign.com', 
            'key': '5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9', 
            'tool': 'mKU7un', 
            'channel': 'facebook-post'})

          attachment['link'] = r.body

So after everything is all buttoned up, this is what our final code looks like.

def facebook_share_checkin(checkin, points):
    try:
        # I remove the code for getting the user token
        graph = facebook.GraphAPI(token)

        # The facebook API specifies what they want as a payload
        attachment = dict()
        attachment['picture'] = consts.SOCIAL_NETWORK_QLIQ_LOGO
        attachment['name'] = checkin.location.name
        msg = 'I just checked in at %s using Qliq' % checkin.location.name
        r = requests.post('http://api.awe.sm/url.txt', 
            { 'v': 3, 
              'url': 'http://mcottondesign.com', 
              'key': '5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9', 
              'tool': 'mKU7un', 
              'channel': 'facebook-post'})

        if r.headers['status'] == '200':
            attachment['link'] = r.content
        else:
            attachment['link'] = consts.SOCIAL_NETWORK_DEFAULT_LINK

        # Post it the the user's wall
        graph.put_wall_post(msg, attachment)

    except facebook.GraphAPIError as e:
        logger.exception('GraphAPIError')
    except SocialNetworkCredentials.DoesNotExist:
        logger.exception('Facebook credentials error')