Dealing with Facebook the Right Way — Part 3

alt

In part 2 of this post, I described the first portion of a solution to my problem of wanting to participate in the Facebook community and information overload. The solution I came up with was an artificial intelligence algorithm that sorts and classifies the articles I may want to read and then posts the most interesting ones to Facebook.

The moving parts for the whole system

There are 5 pieces.

  • The news aggregator
  • The trainer
  • The classifier
  • The reader
  • The poster

In part 2 I described the first three pieces. In this post we will describe the reader and the poster.

The reader

The reader is a simple Sinatra app. It is actually just an additional endpoint on the trainer. The trainer presented me with unranked articles. I read them and ranked them. The reader simply does a query and presents me with articles that the classifier thinks I will like. I then either confirm or adjust the classification so that the training set of data can continue to build. It is pretty good at what it does. Among the articles that is presents as a 5, I usually adjust a few of them to a 4. Rarely is there one in there that is completely wrong.

The poster

The poster uses the Facebook API. In order to use the API, you need to have Facebook application. To get one, it was pretty easy. I just signed up as a developer and created an application. Facebook even deployed it to Heroku for me for free.

Now that I had an app, I needed an access token to post on behalf of my new app. I wanted to be able to fully control my Facebook account.

I used this URL to get a token:

http://facebook.com/dialog/oauth?client_id= your-client-id&redirect_uri=https://your-app-name.herokuapp.com/&scope=manage_pages,publish_stream,offline_access,publish_checkins&response_type=token

where your-client-id is your application's ID, and your-app-name is your application’s name as assigned when you published it to Heroku. Doing a GET on this URL will give you a short term token.

After you get your short term token, you need to exchange it for a long term token. This will allow you to use it with the API for 90 days.

Do a GET on this URL:

https://graph.facebook.com/oauth/access_token?client_id=your-client-id&client_secret=your-app-SECRETgrant_type=fb_exchange_token&fb_exchange_token=THE_TOKEN_GIVEN_AS_RESPONSE_TO_THE_FIRST_URL

where your-client-id is your application’s ID (same as above) and your-app-SECRET is your application secret as given on your application’s page in Facebook. Make sure you pass back the token you received from the first URL.

Now that I had all of that chicanery out of the way, I was ready to post.

A simple ruby script with using the fb_graph gem did the trick. Here is a snippet:

def post_link()

    @db = SQLite3::Database.new("articles.db")  
    @me = FbGraph::User.me(@access_token)
    read, to_post = get_random_high_ranked_article_link
    message = ""
    puts to_post[4]

    result = @me.link!(:link => to_post[11], :message => message) unless @dry_run

    #update the db so we dont repost

    query = "update articles set posted = 1 where id = #{to_post[0]}"
    puts "executing query..."
    puts query
    @db.execute(query) unless @dry_run
end

And that is all. The script pulls the highest ranked, most recent articles, picks a few random ones, favors the ones I have actually read, and posts it.

Conclusion

After posting to FB a few times I kinda got bored of it. I did get some likes on my articles and some comments. The news reader was the big win overall. It really does a excellent job of sorting out the crud. It might not get all the top articles right (some that it thinks are a five might be a four or a three), but it definitely gets all the crappy articles right and keeps them away from me.

Next steps

I would like to improve the classifier. Naive Bayesian classification works quite well. However, I would like to bring in other factors like article source, topics, length, etc. I could try/learn some other algorithms. Maybe a random forest would be neat.

Photo Credit:
flickr