Just recently I found myself trying to explore some of the new features released along with version 2.0 of Facebook’s Graph Api and checking out the new documentation. Unfortunately, I found nothing of interest with version 2.0, but I did have my mind blown to find a very useful but little known existing feature of the Graph API.
Without even procuring an access token, you can make calls to the Facebook API and pass in a website for the ‘id’ parameters, and you can get some results about the site according to Facebook:
Note that as v1.0 is deprecated, v2.0 will require an access token along with the request, but that’s a fairly trivial hurdle. Otherwise, by making this request, Facebook will return JSON data that will look something like this:
Note that since this is my own website, I may or may not have modified the response for this blog to make myself look more hip and cool. You can also make calls with multiple sites:
And get back moar data:
This very basic yet powerful functionality let me to want to answer a fairly straightforward question that a computer would be useful for: Given a base domain, what is the most popular page on that site according to Facebook? I subsequently made a simple website crawler, and all of the code can be found here on Github.
I used Beautiful Soup to quickly and easily scrape and parse HTML to extract links, and I used GEvent to make asynchronous network calls. Combined with the Facebook API examples above, I could crawl a domain and find the share count from every single page on that domain and find the most popular thing ever posted on that site by ranking the results from most to least popular.
We can step through all of the code below:
Starting with the beginning of the script, we first invoke the totally unobtrusive power of GEvent and monkey patch core network functionality to make network calls non-blocking. Without calling patch_socket() and patch_ssl(), network calls will remain serialized.
The rest of this portion of the script is fairly straightforward: force the end user to pass in a URL as the second parameter on the command line and then instantiate an instance of a FacebookContentFinder class defined below:
There are a few things we need to keep track of throughout the process. The base domain establishes the starting point of the crawl. We parse each link of every page, so it’s quite possible to create infinite loops, so we can track visited pages by using a python set. Min_str_length and max_str_length are beyond the scope of basic functionality and are used as helpers to ignore non-html files. See full file for additional context.
Pool and greenlets reference gevent-specific logic. By using a gevent pool, we can restrict the total number of greenlets (or “emulated threads”, if you will (and I will)) to a certain size. We maintain a list of the greenlets that we spawn so that we can eventually block at a certain point to ensure completion of all of these “threads” (if you will).
Now we can kick off the crawl for the object:
find_links_in_url is the method defined below, and base_domain represents the single argument we pass as a parameter to that method. This particular method is recursive in nature and will re-call itself for every single link in every single page. The logic is defined here:
Note that this method has a few helper functions. You can see the original file for full context, but hopefully the methods defined in pure english are relatively self-explanatory. For additional context for what these do, please see a dictionary.
The important part is that we get all links in each page, and recursively call this method again to do the same thing on the next page. In calling the method, we spawn another greenlet which will be added to the gevent pool that will be executed asynchronously.
Finally, we can block until the entire site has been crawled. If your eyes haven’t glazed over, you remember that we maintained a set of visited URL’s that we’ve crawled. We can then pass those URL’s to Facebook to find out which ones were popular, and we can find our proverbial prom queen:
Here, I’m simply batching all of the URL’s into chunks of 50 items and then making a call to the Facebook graph api. I’m taking the data returned from Facebook and adding it to a dictionary of the URL to the number of shares it has on Facebook. Finally, I can return that data as an OrderedDict which will return the keys in sorted order so that the most popular site is listed first.
Again, please see the entire python file to see the entire thing in action and feel free to give it a whirl.