Posts tagged with “webdev”


Fri 17 Sep

Using the Facebook API to retrieve mutual friends

A few nights ago I had a (bad) idea for a tool that leveraged the Facebook API. I'll spare you the details, but my tool needed to retrieve the list of mutual friends for each of the logged in user's friends. This proved to be a bit trickier to figure out than I had hoped for as a developer new to the Facebook API, so here's a quick little PHP script that shows how I went about solving this problem.

I wound up using the REST API's friends.getMutualFriends query. This code sample uses the new Graph API to retrieve a list of your friends, then displays the profile picture of any who have more than ten mutual friends. Note this would take a while to run on people with a large number of friends.

$facebook = new Facebook(array(
    'appId'  => '<your app id>', 
    'secret' => '<your secret id>',
    'cookie' => true, 
));
$session = $facebook->getSession();

$my_friends = $facebook->api('/me/friends');  // Graph API call, retrieves own friendlist
foreach ($my_friends['data'] as $person) {
    $friend_uid = $person['id'];

    // Old REST API call. Gets the mutual friends (source must be logged in user).
    $param = array('method' => 'friends.getMutualFriends', 
                                'source_uid' => $me['id'], 
                                'target_uid' => $friend_uid, 
                                'callback' => '' );
    $res = $facebook->api($param);
    if(count($res) > 10) {
            echo "<a href=\"http://www.facebook.com/profile.php?id=".$friend_uid."\">"
                ."<img src=\"https://graph.facebook.com/".$friend_uid."/picture\"></a>: "
                .count($res)." friends in common<br>";
    }
}

Unfortunately, this does not provide a way to retrieve the full friend list of an arbitrary friend of a logged in friend. As far as I can tell, this is not possible using any of the Facebook API's. If you know of a way, certainly leave a note in the comments!


Tue 14 Sep

One-liner to extract a list of link addresses from an HTML file

I'm moving my research group's website to a new server and making some updates at the same time. One of the main things I need to do is make sure links are going to work after the transition. Here is a little one-line shell "script" (if you can call it that) that will extract link addresses from an HTML web page:

wget -q -O - http://www.google.com | tr " " "\n" | grep "href" | cut -f2 -d"\""

wget fetches the file and outputs its content to stdout. tr replaces all spaces with newlines, grep filters out every line that doesn't contain an "href", and finally cut displays everything between the first pair of double-quotes.

If you want to use a file you have on your local machine, you can use this variant instead:

tr " " "\n" < [file_name.html]| grep "href" | cut -f2 -d"\""

Obligatory disclaimer: HTML is NOT a regular language and in general cannot be parsed with regex's as is done here. This is not guaranteed to work.

· Tags: , ,

Tue 29 Jun

python-mysqldb: execute() first

While working on implementing a schema-free, MySQL-backed data store (thanks, Bret Taylor!), I ran into a problem with using MySQLdb to access the database. I'll eventually post the code I wrote up on this site so others can see my example, but for now the following will suffice. When performing a SELECT, I would get the following error upon attempting to fetch my results.

Incorrect Python:
q = "SELECT body FROM entities WHERE id='%s'" % (entity_id)
self.conn.cursor().execute(q)
entity = self.conn.cursor().fetchone()

Error:
File "datastore.py", line 93, in get
entity = self.conn.cursor().fetchone()
File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 340, in fetchone
self._check_executed()
File "/usr/lib/pymodules/python2.6/MySQLdb/cursors.py", line 70, in _check_executed
self.errorhandler(self, ProgrammingError, "execute() first")
File "/usr/lib/pymodules/python2.6/MySQLdb/connections.py", line 35, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: execute() first

The issue was using two separate cursor objects. Here's the corrected code:
c = self.conn.cursor()
q = "SELECT body FROM entities WHERE id='%s'" % (entity_id)
c.execute(q)
entity = c.fetchone()


Sat 22 Aug

Kohana Documentation Offline Snapshot

This is a snapshot of the Kohana documentation as of Aug 22, 2009 that can be used offline. It's not pretty but it is nice if, say, you're about to get on a plane and want to be able to reference the Kohana documentation.