Liquid 9

Neckbeards, Unite

Saturday, Kansas City Python coders finally came out of their basements to meet and discuss the first ever broad-scale Python user group in Kansas City.

uploads/blog/interactive/highres_31084281.jpg

After months of brewing interest in a Python meetup, curiosity about the existence and occupation of local Python developers, and discussions with the Python Software Foundation about hosting a future PyCon in Kansas City, the Python KC user group was born.

The meeting was well-attended, and we were honored and humbled to be able to host the first discussion at Liquid 9. In attendance were some brilliant minds, from the founder of the zero-knowledge data encryption, backup, sync, and storage service SpiderOak, to a developer of the mobile media platform MobiTV, which serves up hundreds of shows for television giants NBC, Fox News, ESPN, The Disney Channel, and Comedy Central.

The diversity of ways developers are using Python in Kansas City really gave credence to the power and flexibility of the language, and made it clear that a user group such as this would be beneficial to us all, and to the Kansas City programming community at large. It was great also to see a handful of attendees who had never touched Python code at all, but just had curiosity about the merits of the language and wanted to learn a bit more.

Some of the key goals discussed in the meeting:

  • Raise awareness and interest in the Python programming language
  • Find "sell-able" ways to demonstrate the business value of the language, even on an enterprise level, to broaden the acceptance of the language in corporate environments
  • Learn new ideas and concepts about the language we love

It will be a fun road to see how this group evolves over time.

def init_user_group():
    meet_and_discuss()
    for today in forever:
        users = [user.name for user in local_developers if 'Python' in
                 user.interests]
        group = Group(members=','.join(users), activities=lambda day: [
                      user.brings_to_the_table(day) for user in users])
        group.perform_activities(today)
Comments

Batch Geocoding Addresses Using Google Spreadsheets

After facing countless frustrations trying to find a way to geocode 300+ addresses to latitude and longitude coordinates for an upcoming project, I decided to investigate scripting Google Spreadsheets to handle the task.

The ImportData() solution that has been spread around is clever, but, unfortunately, Google limits spreadsheets to 50 or fewer ImportData() formulas per sheet. For situations where many more geocodes are needed, this won't work.

This is where I turned to Google Apps Script for the first time, and I've published a script in the script gallery called "Batch Geocode."

Here's how to use it.

First, open your Google Spreadsheet and select Tools -> Script Gallery. Search for "Batch Geocode," and click "Install."

After installation, you will need to close and re-open your spreadsheet. Upon re-opening, you will find a "Geocoder" menu added up top.

Prepare your data for geocoding. For my spreadsheet, I had to concatenate several fields first to get a valid location field.

uploads/blog/interactive/location_formula.jpg

After copying the formula down the spreadsheet, I had complete addresses ready for geocoding.

uploads/blog/interactive/filled_locations.jpg

Next, hightlight the addresses you want geocoded, and select "Geocode addresses" from the "Geocoder" menu.

uploads/blog/interactive/geocode_menu.jpg

You will be prompted for which columns you would like to overwrite with the latitude and longitude values, respectively. These should be two column letters, separated by a comma. If you would like to insert new columns for the data, leave this field blank. Press "OK."

uploads/blog/interactive/column_prompt.jpg

That's all there is to it. The script throttles the process to a maximum of 5 geocoding requests per second, to ease the burden on Google servers and avoid rate limits, so you may have to wait a bit.

uploads/blog/interactive/geocode_loading.jpg

However, you will be notified once the script has completed, and your latitude and longitude values will appear in the specified columns!

uploads/blog/interactive/geocode_complete.jpg

Until the script has been approved by Google for addition to the script gallery, the raw script can be downloaded here, and installed manually through your spreadsheet's script editor.

Comments

Obtaining a Permanent Facebook OAuth Access Token

In the same vein as my last post on obtaining a permanent Google session token, we also found a need for a permanent Facebook OAuth access token. In our case, we wanted to grab data from Facebook Insights for several pages on a scheduled cron without requiring the user to authenticate each time.

Here's how to pull it off.

First, create a new Facebook application if you haven't already, and construct the OAuth url. Determine which permissions your access token will need, and fill in the application ID. Ours looks like this:

https://graph.facebook.com/oauth/authorize?client_id=...&redirect_uri=http://liquid9.tv/&scope=read_insights,offline_access

There are two things to note about the above url. First, notice we specified redirect_uri as http://liquid9.tv/. The redirect_uri is the path to which the browser will be redirected after authentication. This can be almost anything, and we are only using it in our case so we can grab the code parameter from its querystring after redirection occurs. There is, however, one requirement for this path -- its domain must match the one specified in the "Site URL" and "Site Domain" values of your application's "Web Site" settings.

The second thing to note is the offline_access permission we appended to the scope. This is the key to a permanent Facebook access token. The offline_access permission removes the short expiration time from your access token and allows your application to make requests on behalf of the user at any time.

Copy the authentication url into your browser and authorize your application with Facebook. You will be redirected to a url that looks similar to this:

http://liquid9.tv/?code=...

The code parameter will be a very lengthy string of random characters. Copy this value and hang on to it for the construction of a new url. This url will turn the generated code into a valid access token for your application.

Fill in your application ID, application secret, redirect uri, and the code we just copied. Again, ours looks like this:

https://graph.facebook.com/oauth/access_token?client_id=...&client_secret=...&redirect_uri=http://liquid9.tv/&code=...

Open the url in your browser, and you should be presented a string with an access token parameter in it. The value after access_token= is the bit you will want to store for future requests, and it can be used indefinitely to authenticate your application with this user's session!

Once we have the token, how can it be used to make requests? In Python, we use Facebook's own python-sdk. Assuming we stored our access token in a setting called FB_ACCESS_TOKEN, we can use it in our application like so:

import facebook
from settings import FB_ACCESS_TOKEN

graph = facebook.GraphAPI(FB_ACCESS_TOKEN)
page = graph.get_object(...)
...

Read more about Facebook OAuth authentication here: http://developers.facebook.com/docs/authentication/

Comments