<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Marc Hibbins</title>
	<atom:link href="http://blog.marchibbins.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.marchibbins.com</link>
	<description>Freelance Web developer, blogger</description>
	<lastBuildDate>Wed, 21 Sep 2011 11:42:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>One Note</title>
		<link>http://blog.marchibbins.com/2011/09/21/one-note/</link>
		<comments>http://blog.marchibbins.com/2011/09/21/one-note/#comments</comments>
		<pubDate>Wed, 21 Sep 2011 11:41:48 +0000</pubDate>
		<dc:creator>Marc Hibbins</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.marchibbins.com/?p=1795</guid>
		<description><![CDATA[Here’s a note on using the PyDev plug-in for Eclipse with our virtualenv set-up, configuring your Python Path within PyDev and running custom commands from the console — particularly for Django development.]]></description>
			<content:encoded><![CDATA[<p>In addition to <a title="Here’s to Taking It Easy" href="http://blog.marchibbins.com/2011/09/15/here%e2%80%99s-to-taking-it-easy/">my last post</a> on Python development environments, here’s a note on using the <a title="PyDev" href="http://pydev.org/" target="_blank">PyDev plug-in for Eclipse</a> with our virtualenv set-up.</p>
<p>Assuming everything went swimmingly — virutalenv running, Django installed with a project folder (let’s call it ‘<strong>myproject</strong>’) having done something like this:</p>
<blockquote><p><code>$ mkdir dev<br />
$ cd dev<br />
$ virtualenv --no-site-packages env<br />
$ source env/bin/activate<br />
(env) $ pip install django<br />
(env) $ django-admin.py startproject myproject</code></p></blockquote>
<p>First install PyDev in Eclipse under “Install New Software” with the URL: <a href="http://pydev.org/updates" target="_blank">http://pydev.org/updates</a></p>
<p>Once that’s done, configure the Python interpreter. In your Preferences, under <strong>PyDev</strong> and <strong>Interpreter &#8211; Python</strong>, hit the <strong>Auto Config</strong> option to find all your libraries and populate the Python Path.</p>
<p>Otherwise, manually add Python and locate your interpreter, mine is under <code>/usr/bin/python2.7</code>.</p>
<p>For now, this only includes your globally-installed system-wide libraries, not those you’ve installed within the virtualenv environment.</p>
<p>To create a new &#8220;PyDev Django project&#8221; however, you’ll need to have Django installed globally (or otherwise configured in the above Python Path settings) so PyDev can see it. Right now ours isn’t, so we have an extra step.</p>
<p>Instead, we’ll create a &#8220;New PyDev project&#8221; (non-Django), add our virtualenv location containing the libraries in our local site-packages directory, then <em>convert</em> that to a Django Project once PyDev is satisfied we have the goods.</p>
<p>This method means we don’t have to install Django globally just for the sake of using this IDE.</p>
<p>To do this, from the File menu and <strong>New PyDev Project</strong>, I un-tick ‘Create src folder and add it to the PYTHONPATH’, instead selecting ‘Don’t configure PYTHONPATH (to be done manually later on)’.</p>
<p>Right-click the project folder, go to <strong>Properties</strong> and <strong>PyDev &#8211; PYTHONPATH</strong> and add a Source Folder pointing to your virtualenv site-packages. In this instance:</p>
<blockquote><p><code>dev/env/lib/python2.7/site-packages</code></p></blockquote>
<p>Having found Django, PyDev now let’s us convert this to a Django project. Right-click again and under the <strong>PyDev</strong> menu select <strong>Set as Django Project</strong>.</p>
<p>Now everything can be performed within Eclipse, rather than by the command line.</p>
<p>For example, to run the server we’ll add a Custom Command. Under the <strong>Django</strong> menu select <strong>Custom Command</strong> and add the following:</p>
<blockquote><p><code>runserver --noreload</code></p></blockquote>
<p>You may be asked to select which <code>manage.py</code> to run from, choose the one within your project, i.e. <code>myproject/manage.py</code>.</p>
<p>Hit Run and test <a href="http://localhost:8000/" target="_blank">http://localhost:8000/</a> within your browser.</p>
<p>Note the <code>--noreload</code> option allows Eclipse to maintain control over the process, rather than spawning a new thread. This function usually allows the server to reload automatically when changes are made to your code, at your convenience.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.marchibbins.com/2011/09/21/one-note/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Here’s to Taking It Easy</title>
		<link>http://blog.marchibbins.com/2011/09/15/here%e2%80%99s-to-taking-it-easy/</link>
		<comments>http://blog.marchibbins.com/2011/09/15/here%e2%80%99s-to-taking-it-easy/#comments</comments>
		<pubDate>Thu, 15 Sep 2011 16:43:43 +0000</pubDate>
		<dc:creator>Marc Hibbins</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.marchibbins.com/?p=1763</guid>
		<description><![CDATA[How to set up multiple Python environments on Linux, using Virtualenv to handle isolated project and pip for package management.]]></description>
			<content:encoded><![CDATA[<p>I mentioned in my previous post that I borked my system meddling with Python. Having reset my workspace, I’ve now set up a solid system that makes handling projects and multiple development environments super simple.</p>
<p>The new set up easily handles multiple Python projects, without compatibility or version conflicts. The installation is equally straightforward.</p>
<p>Before switching to a desktop Linux, I used to sing the praises of VMware and developing with virtual machines when dealing with unique environments. By “unique”, I rather mean any odd project out of the ordinary LAMP set-up I usually work with, or something that requires a specific version of a piece of software.</p>
<p>Since then however, I’ve found no need. So long as you think before you leap.</p>
<p>Virtual boxes (as closed, single-piece software) are good and all, you can be as venturous as you wish without risk of damaging your native system. Plus, if you screw one of these you can restore a saved state in a few clicks. However, the VM safety net allows you to proceed without caution, perhaps recklessly, at the expense of fully comprehending the commands you’re executing and tasks you’re running.</p>
<p>In that sense, they’re great for beginners uncertain of how (or if) they should install software, e.g. Apache, PHP, Python etc — appliances and virtual stacks are helpful.</p>
<p>Otherwise they can convolute your workspace — and more often than not, won&#8217;t be configured exactly how you want or need them. Running software natively is simple and as intended, it also allows you to configure your entire environment without any assumptions made by distributors.</p>
<h3><a class="anchor" name="Virtualenv">Virtualenv</a></h3>
<p><a title="virtualenv 1.6.4 : Python Package Index" href="http://pypi.python.org/pypi/virtualenv" target="_blank">Virtualenv</a> is quite the revelation. It facilitates multiple isolated Python environments on a single system, dynamically handling your <a title="6. Modules — Python v2.7.2 documentation" href="http://docs.python.org/tutorial/modules.html#the-module-search-path" target="_blank">Python Path</a> so packages are install within an enclosed local directory, rather than in amongst your top-level system packages.</p>
<p>This means you can create project-by-project virtual environments avoiding compatibility and version conflicts. When an environment is created (and activated) libraries are thereafter installed within discreet directories that aren&#8217;t shared with other virtualenv environments.</p>
<p>This means nothing is installed “system-wide”, so libraries don’t accrue over time, there’s no balancing of versions. It also means you can work with different version of Python simultaneously.</p>
<p>Python packages should be installed with a package manager. The latest of which is <a title="pip 1.0.2 : Python Package Index" href="http://pypi.python.org/pypi/pip" target="_blank">pip</a>.</p>
<p>Prior to this, easy_install was the manager <em>du jour</em> (part of Setuptools, both now out-dated), but we’ll only be using that to install pip:</p>
<blockquote><p><code>$ sudo easy_install pip</code></p></blockquote>
<p>Pip is a direct replacement for easy_install, improving on a few things (a <a title="pip — pip v1.0.2 documentation" href="http://www.pip-installer.org/en/latest/index.html#pip-compared-to-easy-install" target="_blank">comparison can be found on the installer site</a>). Packages that are available with easy_install should be pip-installable and the installation method is the same — the following installs virtualenv:</p>
<blockquote><p><code>$ sudo pip install virtualenv</code></p></blockquote>
<p>With virtualenv installed we can create an environment within your workspace, all it needs is the environment directory name, here ‘<strong>env</strong>’:</p>
<blockquote><p><code>$ virtualenv env</code></p></blockquote>
<p>There are a few options you have with this command. In the following example, the <code>--no-site-packages</code> flag means that the new environment will <em>not </em>inherit any system-wide global site packages. The <code>--distribute</code> flag will install <a title="distribute 0.6.21 : Python Package Index" href="http://pypi.python.org/pypi/distribute" target="_blank">Distribute</a> rather than Setuptools:</p>
<blockquote><p><code>$ virtualenv --no-site-packages --distribute env</code></p></blockquote>
<p>Distribute is to setuptools as Pip is to easy_install. Distribute and pip are <a href="http://s3.pixane.com/pip_distribute.png" target="_blank">the new hotness</a>, Setuptools and easy_install are old and busted — <a title="Introduction to Packaging — The Hitchhiker's Guide to Packaging v1.0 documentation" href="http://guide.python-distribute.org/introduction.html#current-state-of-packaging" target="_blank">for now</a>.</p>
<p>Anyway, activate your environment:</p>
<blockquote><p><code>$ source env/bin/activate</code></p></blockquote>
<p>You’ll see from your shell prompt that the environment is activated, with the name prepended.</p>
<p>Then we’ll install something with pip. <a title="yolk 0.4.1 : Python Package Index" href="http://pypi.python.org/pypi/yolk" target="_blank">Yolk</a> is a tool for querying the packages currently installed on your system, so we’ll install that and grab a list:</p>
<blockquote><p><code>(env) $ pip install yolk<br />
(env) $ yolk -l</code></p></blockquote>
<p>Then you’ll see everything the environment can see in the output (this will depend on your global site packages and how you created the environment, as above).</p>
<p>Note that you don&#8217;t need to sudo whilst in the activated environment.</p>
<p>As a test, we’ll <code>deactivate</code> the environment and run the same command, which gets the following error (unless you have yolk installed globally):</p>
<blockquote><p><code>(env) $ deactivate<br />
$ yolk -l<br />
yolk: command not found</code></p></blockquote>
<p>If installed within an environment, a package is only available whilst it is activated. This is the means to install whatever you wish, without worrying about cross-project conflicts.</p>
<h3><a class="anchor" name="More_pip">More pip</a></h3>
<p>Another good feature of pip is to generate a list of requirements for your working set of packages. The command is called freeze and generates a text file as follows:</p>
<blockquote><p><code>(env) $ pip freeze &gt; requirements.txt</code></p></blockquote>
<p>This will create a list of all installed packages with specific versions for each library. This is in a custom syntax and looks something like this:</p>
<blockquote><p><code>distribute==0.6.19<br />
wsgiref==0.1.2<br />
yolk==0.4.1</code></p></blockquote>
<p>This list can then be distributed (e.g. to a team of developers) and used to install those packages on other systems, like so:</p>
<blockquote><p><code>(env) $ pip install -r requirements.txt</code></p></blockquote>
<p>Note, this isn’t couple with virtualenv, which actually has it’s own method of bootstrapping — <a title="virtualenv 1.6.4 : Python Package Index" href="http://pypi.python.org/pypi/virtualenv" target="_blank">see &#8220;Creating Your Own Bootstrap Scripts&#8221;</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.marchibbins.com/2011/09/15/here%e2%80%99s-to-taking-it-easy/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Home Security</title>
		<link>http://blog.marchibbins.com/2011/09/08/home-security/</link>
		<comments>http://blog.marchibbins.com/2011/09/08/home-security/#comments</comments>
		<pubDate>Thu, 08 Sep 2011 15:43:46 +0000</pubDate>
		<dc:creator>Marc Hibbins</dc:creator>
				<category><![CDATA[development]]></category>

		<guid isPermaLink="false">http://blog.marchibbins.com/?p=1742</guid>
		<description><![CDATA[Lessons in user permissions and groups, enabling Apache VirtualHosts to server HTTP from within an encrypted home directory on Ubuntu.]]></description>
			<content:encoded><![CDATA[<p>Since <a title="» Better Half by Marc Hibbins" href="http://blog.marchibbins.com/2011/03/15/better-half/">deciding to work exclusively in a Linux environment</a> at the beginning of the year, I’ve been more than pleasantly surprised not to have found myself needing to reset my system as a result of the frequent changes of set-up and numerous installations and removals of software that I’ve needed to perform in order to work on various projects.</p>
<p>The inevitable day, however, came a couple of weeks ago when I royally screwed my system messing around with Python (solution in another blog post. <em>Update</em>: <a title="» Here’s to Taking It Easy by Marc Hibbins" href="http://blog.marchibbins.com/2011/09/15/here%e2%80%99s-to-taking-it-easy/">here it is</a>).</p>
<p>Once Ubuntu was reinstalled, I encountered a problem attempting to recreate my workspace having opted to encrypt my home directory during user setup.</p>
<p>Running the normal LAMP-server setup, Apache is unable to access files within the <a title="EncryptedHome - Community Ubuntu Documentation" href="https://help.ubuntu.com/community/EncryptedHome" target="_blank">encrypted home</a>.</p>
<p>I was tying to duplicate my previous configuration, using individual VirtualHosts locating directories within my user home, for example:</p>
<blockquote><p>/home/marc/sites/dev/</p></blockquote>
<p>I’m pretty sure my home directory was encrypted last time too, but this problem was new for me — perhaps something from an update in between?</p>
<p>The permissions problem occurs as only my user, <strong>marc</strong>, has access to the home and Apache’s user, <strong>www-data</strong>, does not. This results in a HTTP 403 Forbidden when attempting to serve files.</p>
<p>Having a look around, I found <a title="Ubuntu Forums - View Single Post - [SOLVED] apache 403 on encrypted home dir in 10.10 maverick meerkat" href="http://ubuntuforums.org/showpost.php?s=26a3c259806ff43ed7e18ca3c00f15be&amp;p=9868230" target="_blank">a convoluted method using symlinks and Apache’s UserDir</a> then a far simpler solution, on <a title="permissions - Allowing apache access to a subdirectory in a home directory without access to the home directory - Ask Ubuntu - Stack Exchange" href="http://askubuntu.com/questions/36533/" target="_blank">AskUbuntu</a>, as follows.</p>
<p>It’s unsafe to change your home ownership (to <strong>www-data</strong>, for example) but Apache needs execute permissions there. So selectively <strong>chmod</strong> the directory:</p>
<blockquote><p>sudo chmod 751 /home</p></blockquote>
<p>This grants execute access to others, who can only read files with correct knowledge of names and locations. It also removes your user’s read access to /home, so you’ll have to <strong>sudo</strong> for that.</p>
<p>Another precaution benefiting those on development-only machines, is to restrict IP listening within Apache’s ports.conf, so only local connections get any attention:</p>
<blockquote><p>Listen 127.0.0.1:80</p></blockquote>
<h3><a name="Alternatives" class="anchor">Alternatives</a></h3>
<p>As for alternatives, you could encrypt your whole drive rather than just the home directory. You shouldn’t see any problems then.</p>
<p>Or you could just ignore encryption all together.</p>
<p>You could, of course, just work out of the traditional <strong>/var/www/</strong> location, which is the Apache default. Simply create a directory there and <strong>chown</strong> to your user so you don’t have to always <strong>sudo</strong> changes.</p>
<blockquote><p>sudo mkdir /var/www/dev/<br />
sudo chown marc /var/www/dev/</p></blockquote>
<p>If you’re directories are elsewhere on your system, for example in SVN repositories such as <strong>/srv/svn/</strong> or <strong>/usr/local/svn/</strong> then you’ll need to <strong>chown</strong> those to <strong>www-data</strong> so they’re readable, similar to our method of reading from within /home above.</p>
<p>The <a title="Subversion - Community Ubuntu Documentation" href="https://help.ubuntu.com/community/Subversion#Server_Configuration" target="_blank">Ubuntu docs on Subversion</a> offer the best solution for handling user permissions for SVN over HTTP.</p>
<p>Create a new user group, <strong>subversion</strong>, add the users <strong>marc</strong> and <strong>www-data</strong> to it and <strong>chown</strong> the repo to <strong>www-data:subversion</strong>, giving read/write access to the group (granting privileges to <strong>marc</strong>). Finally <strong>chmod</strong> with <strong>-s</strong> so that new files inherit that group ID, like so:</p>
<blockquote><p>cd /srv/svn/<br />
sudo chown -R www-data:subversion dev/<br />
sudo chmod -R g+rws dev/</p></blockquote>
<p>The <strong>-s</strong> flag means that all files created inside that directory will inherit the <em>group of the directory</em>, otherwise files takes on the primary <em>group of the user</em>. New subdirectories will also inherit this.</p>
<p>The <strong>-R</strong> option applies the changes recursively (i.e. existing subdirectories).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.marchibbins.com/2011/09/08/home-security/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>As Long as You Follow</title>
		<link>http://blog.marchibbins.com/2011/05/18/as-long-as-you-follow/</link>
		<comments>http://blog.marchibbins.com/2011/05/18/as-long-as-you-follow/#comments</comments>
		<pubDate>Wed, 18 May 2011 22:48:19 +0000</pubDate>
		<dc:creator>Marc Hibbins</dc:creator>
				<category><![CDATA[beatspermile]]></category>
		<category><![CDATA[iheartplay]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://blog.marchibbins.com/?p=1707</guid>
		<description><![CDATA[Integrating Twitter into Beats Per Mile to send automatic progress updates, authenticating with OAuth and using the TwitterOAuth PHP library.]]></description>
			<content:encoded><![CDATA[<p>The final part of <a title="Beats Per Mile" href="http://coffeeandpie.net/beatspermile/" target="_blank">Beats Per Mile</a> worth mentioning is the Twitter integration.</p>
<p>The application was designed to send automated tweets on Gemma&#8217;s behalf <a title="Gemma Bardsley (gemb1) on Twitter" href="http://twitter.com/gemb1" target="_blank">from her personal Twitter account</a> giving updates of her progress to those following.</p>
<p>Mainly the tweets would report her current location for the purposes of spectators waiting ahead, others would be sent at intervals of a mile or so to report pace and ongoing form.</p>
<p>Similar to the <a title="Moving Pictures by Marc Hibbins" href="http://blog.marchibbins.com/2011/05/16/moving-pictures/" target="_blank">mechanism for fetching Instagram images</a> at predetermined locations on the course, the application contained a list of agreed points at which to tweet.</p>
<p>These were at landmarks and certain spectator spots too, but also at course milestones — the first mile complete, halfway complete, one mile to go etc, as well as the start and finish.</p>
<p>The application monitored the elapsed distance and updated accordingly, grabbing the latest time and <a title="Keep On Running by Marc Hibbins" href="http://blog.marchibbins.com/2011/04/13/keep-on-running/">statistics from the RunKeeper data</a>, as well as geolocating the tweet with the latest set of GPS coordinates.</p>
<p>Since Twitter <a title="Basic Auth Shutdown | dev.twitter.com" href="http://dev.twitter.com/pages/basic_auth_shutdown" target="_blank">deprecated support of Basic Auth</a>, applications much authenticate users with OAuth to gain write access.</p>
<p>This means rather than handing over your username and password to applications and trust (hope) that they’re friendly — <a title="Keep it to Yourself by Marc Hibbins" href="http://blog.marchibbins.com/2009/05/06/keep-it-to-yourself/">as used to be the only way</a> — OAuth allows applications to request access on your behalf without users ever parting with precious login credentials. You simple give, or deny, permission.</p>
<h3><a name="Using_TwitterOAuth" class="anchor">Using TwitterOAuth</a></h3>
<p>Twitter offer a number of links to <a title="Twitter Libraries | dev.twitter.com" href="http://dev.twitter.com/pages/libraries" target="_blank">OAuth libraries</a> for various languages to make the this job a lot easier. There are many Twitter specific OAuth libraries in particular, purposely tailored for the API.</p>
<p>I picked up PHP-based <a title="abraham/twitteroauth - GitHub" href="https://github.com/abraham/twitteroauth" target="_blank">TwitterOAuth by Abraham Williams</a>, which gets you up and running very rapidly.</p>
<p>The <a title="Authenticating Requests with OAuth | dev.twitter.com" href="http://dev.twitter.com/pages/auth" target="_blank">OAuth flow is documented at length</a>, but essentially performs three major tasks:</p>
<ul>
<li>Obtains access from Twitter to make requests on behalf of a user</li>
<li>Directs a user to Twitter in order to authenticate their account</li>
<li>Gains authorisation from the user to make requests on their behalf</li>
</ul>
<p><br/>This is achieved with a cycle of exchanging authenticating tokens between application and Twitter to verify permission. TwitterOAuth particularly creates a session object in your application and rebuilds itself with each token exchange to remain contained in a single class instance.</p>
<p>In a very abridged manner, something like the following:</p>
<blockquote><p>// Build TwitterOAuth object with client credentials<br />
$connection = new TwitterOAuth(<strong>CONSUMER_KEY</strong>, <strong>CONSUMER_SECRET</strong>);<br />
// Get temporary credentials to allow application to make requests and set callback<br />
$request_token = $connection-&gt;getRequestToken(<strong>OAUTH_CALLBACK</strong>);</p></blockquote>
<p>This initial call verifies your application has access the Twitter API, basically that it&#8217;s registered and good to go. Twitter returns two tokens, if successful, that we store:</p>
<blockquote><p>$_SESSION['oauth_token'] = $request_token['<strong>oauth_token</strong>'];<br />
$_SESSION['oauth_token_secret'] = $request_token['<strong>oauth_token_secret</strong>'];</p></blockquote>
<p>Then the application sends the user to Twitter to authorise it’s access, using a URL generated with the token received above:</p>
<blockquote><p>$authoriseURL = $connection-&gt;getAuthorizeURL(<strong>$request_token['oauth_token']</strong>);<br />
header(&#8216;Location: &#8216; . $authoriseURL);</p></blockquote>
<p>On successful authorisation Twitter will return the user to your callback URL (set above) with a verification token. TwitterOAuth now rebuilds for the first time with the OAuth tokens in our session and uses the new verification token to get an a new access token which will grant us user account access:</p>
<blockquote><p>// Create TwitterOAuth object with application tokens<br />
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, <strong>$_SESSION['oauth_token']</strong>, <strong>$_SESSION['oauth_token_secret']</strong>);</p></blockquote>
<blockquote><p>// Request access tokens from Twitter on behalf of current user with verifier<br />
<strong>$access_token</strong> = $connection-&gt;getAccessToken(<strong>$_REQUEST['oauth_verifier']</strong>);</p></blockquote>
<p>This final request gets us two OAuth tokens specific to the current user, allowing us to make requests on their behalf, with which TwitterOAuth rebuilds again:</p>
<blockquote><p>// Rebuild TwitterOAuth object with user granted tokens<br />
$connection = new TwitterOAuth(<strong>CONSUMER_KEY</strong>, <strong>CONSUMER_SECRET</strong>, <strong>$access_token['oauth_token']</strong>, <strong>$access_token['oauth_token_secret']</strong>);
</p></blockquote>
<blockquote><p>// Test that everything is working<br />
$connection-&gt;get(&#8216;account/verify_credentials&#8217;);</p></blockquote>
<p>The dance made a hell of a lot easier with a library such as this.</p>
<p>Usually applications store these tokens final two tokens, the user generated <strong>oauth_token</strong> and <strong>oauth_token_secret</strong>, which saves the need to authorise the user again.</p>
<p>Storing these details (in a session or database) means that a username and password need not be saved. The tokens are good until the user revokes access, no sensitive information is ever released to the application, all the user ever gives is their permission.</p>
<p>With access tokens stored, the connection to Twitter is a lot simpler — just create the TwitterOAuth object with those user-generated codes as in the very last step, without any of the redirecting to and from Twitter.com. Of course, those tokens could only have ever been obtained by carrying out the full process to begin with.</p>
<p>Beats Per Mile is a <a title="Using one access token with OAuth | dev.twitter.com" href="http://dev.twitter.com/pages/oauth_single_token" target="_blank">single-user case application</a>, so Gemma only had to authenticate the application once and then we hard-coded them into the scripts.</p>
<h3><a name="Tweet_Away" class="anchor">Tweet Away</a></h3>
<p>With access granted the application was free to send out updates, based on the run data we were collecting and posting it directly.</p>
<p>As mentioned, locations translated into distances and that’s when we tweeted.</p>
<p style="text-align: center;"><a href="http://twitter.com/gemb1/status/59570220540493824" target="_blank"><img class="aligncenter size-full wp-image-1708" title="Beats Per Mile over Tower Bridge" src="http://blog.marchibbins.com/wp-content/uploads/2011/05/tweet-1.jpg" alt="" width="500" height="261" /></a><a href="http://twitter.com/gemb1/status/59602876221227008" target="_blank"><img class="aligncenter size-full wp-image-1709" title="Beats Per Mile past Big Ben" src="http://blog.marchibbins.com/wp-content/uploads/2011/05/tweet-2.jpg" alt="" width="500" height="261" /></a><a href="http://twitter.com/gemb1/status/59573365601603584" target="_blank"><img class="aligncenter size-full wp-image-1710" title="Beats Per Mile halfway" src="http://blog.marchibbins.com/wp-content/uploads/2011/05/tweet-3.jpg" alt="" width="500" height="261" /></a></p>
<p>At mile twenty, it looked back at the mile splits so far and reported which were the fastest:</p>
<p><a href="http://twitter.com/gemb1/status/59589487105282048" target="_blank"><img class="aligncenter size-full wp-image-1711" title="Beats Per Mile fastest miles" src="http://blog.marchibbins.com/wp-content/uploads/2011/05/tweet-4.jpg" alt="" width="500" height="285" /></a></p>
<p>Relying on the total reported distance alone was flawed. There was a slight hiccup when RunKeeper lost GPS coverage under Blackfriars tunnel and in an attempt to compensate found the nearest location to be the other side of the Thames.</p>
<p><img class="aligncenter size-full wp-image-1712" title="RunKeeper getting confused" src="http://blog.marchibbins.com/wp-content/uploads/2011/05/wonky-map.jpg" alt="" width="500" height="261" /></p>
<p>This caused a problem by adding extra distance to her total, so some of the latter tweets (“one mile to go”, for example) posted a little prematurely.</p>
<p>It was more of a problem for Gemma when running, the app announcing in her ear that she’d run further than she had, disheartened to see mile markers on the course thought to have already passed.</p>
<p>This is also why the total distance on the site clocks up to 27.65 miles, the race was long enough as it is!</p>
<p>The final touch to was to drop them on the map, alongside the Instagram images.</p>
<p><a href="http://coffeeandpie.net/beatspermile/" target="_blank"><img class="aligncenter size-full wp-image-1713" title="Tweets and Images on the map" src="http://blog.marchibbins.com/wp-content/uploads/2011/05/on-the-map.jpg" alt="" width="500" height="330" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.marchibbins.com/2011/05/18/as-long-as-you-follow/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Moving Pictures</title>
		<link>http://blog.marchibbins.com/2011/05/16/moving-pictures/</link>
		<comments>http://blog.marchibbins.com/2011/05/16/moving-pictures/#comments</comments>
		<pubDate>Mon, 16 May 2011 22:22:08 +0000</pubDate>
		<dc:creator>Marc Hibbins</dc:creator>
				<category><![CDATA[beatspermile]]></category>
		<category><![CDATA[iheartplay]]></category>

		<guid isPermaLink="false">http://blog.marchibbins.com/?p=1679</guid>
		<description><![CDATA[How Instagram's new API served images for Beats Per Mile, using real-time location data to find geotagged images nearby our runner.]]></description>
			<content:encoded><![CDATA[<p><a title="Beats Per Mile" href="http://coffeeandpie.net/beatspermile/" target="_blank">Beats Per Mile</a> uses the <a title="instagr.am" href="http://instagr.am/" target="_blank">Instagram API</a> to find pictures taken around the marathon course.</p>
<p>We decided we’d need to find a way to put pictures on the map quite early on, knowing Gemma couldn’t be the one to stop and take them. Rather than try to pin a camera to her vest or strap one to a hat, the simplest solution was to find photos taken by spectators.</p>
<p>The <a title="instagr.am - A More Open Platform: The Instagram API" href="http://instagr.am/blog/40/instagram-api" target="_blank">Instagram API is fairly new</a> and the app itself is getting extremely popular. Being mobile-based, we hoped it would be popular among spectators on the day, taking quick snaps and hopefully uploading a good amount of photos to dig around in.</p>
<p>The pictures are geo-tagged as well as captioned, so we could perform location queries and text-based searches (ultimately, a combination).</p>
<p>Firstly we agreed on places around which we’d search for pictures — busy spectator spots and London’s landmarks.</p>
<p>The idea was to look for pictures at these places as Gemma passed them. So we translated them into distances, i.e. determine the elapsed distance that would have been run when reaching each of these places.</p>
<p>Tower Bridge, for example, is at 12.5 miles. Big Ben is at 25 miles and so on — for about 10-15 hotspots.</p>
<p>The application monitored the total distance covered and at each of these key numbers hit the Instagram API for the most recent pictures around the location.</p>
<p>Setting up an Instagram application is instantaneous, though I waited a long time for my API key initially — I did apply when the announcement was first made however, so the turnaround may be a lot faster now.</p>
<p>There’s no moderation or application approval process, when you’re up and running you can start performing queries immediately.</p>
<p>The API is RESTful over HTTPS with a number of endpoints to query images, comments, users, locations, tags and so on. The <a title="Instagram API" href="http://instagr.am/developer/" target="_blank">developer docs</a> are fairly comprehensive.</p>
<p>We’re interested in the media endpoint. Note that the following URLs require an access token or client id, which you will be given, omitted here for brevity.</p>
<p>Get the current most popular photos:</p>
<blockquote><p>https://api.instagram.com/v1/media/popular</p></blockquote>
<p>Or to get information about a single image with a media id:</p>
<blockquote><p>https://api.instagram.com/v1/media/<strong>72612696</strong></p></blockquote>
<p>The search method was our main tool. It takes up to five parameters, <strong>lat</strong>, <strong>lng</strong>, <strong>max_timestamp</strong>, <strong>min_timestamp</strong> and <strong>distance</strong>.</p>
<blockquote><p>https://api.instagram.com/v1/media/search?<strong>lat=51.54242</strong>&amp;<strong>lng=-0.059702</strong>&amp;distance=<strong>1000</strong></p></blockquote>
<p>Note only latitude and longitude parameters are required and distance is in meters, default at 1km with a maximum of 5km.</p>
<p>So at each of our key distances, the app took the latest latitude and longitude positions and grabbed the latest photos.</p>
<p>In an attempt to only select images of the marathon, this was backed up by inspecting within the result set for images with a caption containing any one of a set of predefined keywords such as ‘marathon’ or ‘runners’. That way we could almost ensure that we wouldn’t pick up any images not concerned with the race — though would find false positives.</p>
<p>Once we had the JSON data it was simply visualised on the map, Instagram host the images for us.</p>
<p>One thing lacking in the data perhaps, is that the location information only offers the latitude and longitude coordinates, no place or address names unless otherwise nominated by the user. For each image then I ran the data through <a title="Google Maps Javascript API V3 Services - Google Maps JavaScript API V3 - Google Code" href="http://code.google.com/apis/maps/documentation/javascript/services.html#Geocoding" target="_blank">Google’s Geocoding</a> service to get a street or area name, just for display purposes.</p>
<p>On the whole, it worked well. The API is as straightforward as any and realistically, the biggest worry for us is that most people seem to use Instagram to take pictures of food and little else, we thought we’d have pictures of everyone’s breakfast around various parts of London all day.</p>
<p>Doubtingly, I wrote a simple ‘refresh’ button to rerun any query in case anything untoward or particularly boring popped up when I logged in to check, but between the huge crowds and the caption matching, I only had used it twice and both very early on in the day.</p>
<p>Here’s a handful of the pictures:</p>
<p><a href="http://images.instagram.com/media/2011/04/17/9e0a5b2f7ade49899839225e66ffff3c_7.jpg" target="_blank"><img style="margin-top: 10px; margin-right: 10px;" src="http://images.instagram.com/media/2011/04/17/9e0a5b2f7ade49899839225e66ffff3c_7.jpg" alt="" width="150" height="150" /></a><a href="http://images.instagram.com/media/2011/04/17/83301708cf86499d838ef2907f4f0877_7.jpg" target="_blank"><img style="margin-top: 10px; margin-right: 10px;" src="http://images.instagram.com/media/2011/04/17/83301708cf86499d838ef2907f4f0877_7.jpg" alt="" width="150" height="150" /></a><a href="http://images.instagram.com/media/2011/04/17/2fa5fa73fe464a409d39e6d8889b8eb8_7.jpg" target="_blank"><img style="margin-top: 10px; margin-right: 10px;" src="http://images.instagram.com/media/2011/04/17/2fa5fa73fe464a409d39e6d8889b8eb8_7.jpg" alt="" width="150" height="150" /></a><a href="http://images.instagram.com/media/2011/04/17/d039ae5ae25e4b2a9920c080c437e44f_7.jpg" target="_blank"><img style="margin-top: 10px; margin-right: 10px;" src="http://images.instagram.com/media/2011/04/17/d039ae5ae25e4b2a9920c080c437e44f_7.jpg" alt="" width="150" height="150" /></a><a href="http://images.instagram.com/media/2011/04/17/aa0bbbc18734426ab1e1c2c0b8537134_7.jpg" target="_blank"><img style="margin-top: 10px; margin-right: 10px;" src="http://images.instagram.com/media/2011/04/17/aa0bbbc18734426ab1e1c2c0b8537134_7.jpg" alt="" width="150" height="150" /></a><a href="http://images.instagram.com/media/2011/04/17/b0916949b8b44fdc9d02a9dd33cc6eb6_7.jpg" target="_blank"><img style="margin-top: 10px;" src="http://images.instagram.com/media/2011/04/17/b0916949b8b44fdc9d02a9dd33cc6eb6_7.jpg" alt="" width="150" height="150" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.marchibbins.com/2011/05/16/moving-pictures/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The Show Must Go On</title>
		<link>http://blog.marchibbins.com/2011/04/27/the-show-must-go-on/</link>
		<comments>http://blog.marchibbins.com/2011/04/27/the-show-must-go-on/#comments</comments>
		<pubDate>Wed, 27 Apr 2011 20:06:55 +0000</pubDate>
		<dc:creator>Marc Hibbins</dc:creator>
				<category><![CDATA[beatspermile]]></category>
		<category><![CDATA[iheartplay]]></category>

		<guid isPermaLink="false">http://blog.marchibbins.com/?p=1654</guid>
		<description><![CDATA[How Beats Per Mile handles the music, initially attempting to use SoundCloud only to be thwarted by copyright infringement, ultimately to build a player from scratch just in time.]]></description>
			<content:encoded><![CDATA[<p>One of the key features of <a title="Beats Per Mile" href="http://coffeeandpie.net/beatspermile/" target="_blank">Beats Per Mile</a> was the ability to listen to a &#8216;stream&#8217; of Gemma&#8217;s iPod playlist, enabling you to hear exactly what she was listening to whenever you logged on.</p>
<p>We didn&#8217;t actually have a stream broadcasting from her iPod of course, rather a stream playing from the site that was synchronised with her start time.</p>
<p>We planned on using the <a title="SoundCloud Developers" href="http://developers.soundcloud.com/" target="_blank">SoundCloud API</a> to do this and  it was one of the last thing left to build before race day.</p>
<p>Part of the playlist was curated by friends, donated tracks with sentimental value or just old favourites for a personal touch and to provide an extra kick of motivation.</p>
<p>Asking a lot of people for contributions meant that the playlist wasn&#8217;t finalised and mixed until very late on — Saturday evening.</p>
<p>I created the player using the <a title="Docs - Widget - SoundCloud Developers" href="http://developers.soundcloud.com/docs/widget" target="_blank">SoundCloud Player Widget</a> in preparation for the tracks hoping that the player would be ready before they were uploaded.</p>
<p>It&#8217;s a Javascript-enhanced Flash Widget which uses Actionscript&#8217;s ExternalInterface to expose method handlers and control playback via a in-built API.</p>
<p>Unfortunately this meant it wouldn&#8217;t play on the iPhone. SoundCloud do offer a HTML5-based <a title="Docs - Custom Player - SoundCloud Developers" href="http://developers.soundcloud.com/docs/custom-player" target="_blank">Custom Player</a> (which falls back to Flash), but we didn&#8217;t have time to fully investigate wrangling together a player from scratch.</p>
<h3><a name="SoundCloud_Content_Identification" class="anchor">SoundCloud Content Identification</a></h3>
<p>This would be shortly be rendered irrelevant when we discovered that it is now nearly impossible to upload any copyrighted songs, or tracks containing any samples of copyrighted songs, to SoundCloud. We also discovered that they&#8217;re very, very clever in how they go about detecting them.</p>
<p>Here’s <a title="SoundCloud » Blog Archive » Q&amp;A: Our New Content Identification System" href="http://blog.soundcloud.com/2011/01/05/q-and-a-content-identification-system/" target="_blank">what they wrote in January</a>:</p>
<blockquote><p>Starting in the last few weeks we’ve turned on an automatic content identification system, similar to those used on other major media sharing sites. The system is used primarily for identifying audio that rightsholders have requested to be taken off SoundCloud. This is good news because it makes it easier for artists, labels and other content owners to control how the content they’ve created is available. And when you upload your own audio to SoundCloud, we can find out more quickly if somebody is uploading a copy to their own page without your permission.</p></blockquote>
<p>SoundCloud have always has the right to remove audio deemed in violation of rights as stipulated in their <a title="Terms of Use on SoundCloud - Create, record and share your sounds for free" href="http://soundcloud.com/terms-of-use" target="_blank">terms of use</a>. They also host plenty of mixes and DJ sets as many other similar sites do.</p>
<p>When we tried to upload ours though, nothing would work. None of our mixes were authorised and the refusals would come after spending considerable (precious) time attempting to upload them.</p>
<p>SoundCloud are essentially performing some kind of wave form analysis, comparing uploads to audio already in their databases to detect duplicates.</p>
<h3><a name="Alternatives" class="anchor">Alternatives</a></h3>
<p>There are a few ropey ways to (possibly) slip the net, such as adding a layer of low-level noise to distort the wave form or apply an amount of time-stretching (which was happening anyway, as songs were mixed together).</p>
<p>Too much of either would ruin the music. We were running out of time and didn’t want to risk any hacked attempt being found later and removed, perhaps mid-marathon in a worst case scenario.</p>
<p>So I began to an attempt to recreate the widget, from scratch after all.</p>
<p>I looked at the <a title="Yahoo! Media Player" href="http://mediaplayer.yahoo.com/" target="_blank">Yahoo! Media Player</a>, which is actually suspiciously similar to SoundCloud&#8217;s Widget API. The methods are almost exactly the same, but I had trouble handling multiple files — it really wasn&#8217;t anywhere near as easy to implement.</p>
<p>After browsing for alteratives I eventually found <a title="jPlayer : HTML5 Audio &amp; Video for jQuery" href="http://www.jplayer.org/" target="_blank">JPlayer</a>, a very simple and easily customisable JQuery plug-in. This would also mean we&#8217;d be iPhone-compatible.</p>
<p>It also meant that the files would need to be hosted ourselves, on a normal server, rather than letting SoundCloud handle the load — another reason for our initial choice. We actually ended up serving 7.68GB of streamed audio, fortunately my host is very stable and it didn&#8217;t end up being a problem.</p>
<p>When visitors landed on the page the player would calculate how long the run had been in progress and therefore where you should be in the playlist. The idea was to give no controls, other than mute, the playback would always be synchronised.</p>
<p>Rather than upload all the songs individually, the playlist was divded into five 30-60ish minute tracks and would be easier to navigate.</p>
<p>Once the player determined what track you should be on and where within that the playhead should be, it begins to buffer. Annoyingly, you could have been waiting some time. If the tracks were on SoundCloud’s giant servers then the audio would be properly streamed, but not from mine.</p>
<p>The wait entirely depended on when you happened to arrive, for some it wasn&#8217;t a problem. Say you luckily arrived at the page needing only to jump in five minutes on the current track, you&#8217;d have a small waiting time. If the jump was twenty-five minutes, then start twiddling your thumbs.</p>
<p>There wasn&#8217;t a wait when the player switched between tracks. If a track was paused, the player would record how long for and resume at a later point — where the playhead would otherwise be if you hadn&#8217;t paused, not where you left off.</p>
<p>This also took into account track changes if you paused toward the end of a track or paused for more than the duration of an entire part.</p>
<h3><a name="Back_to_the_Cloud" class="anchor">Back to the Cloud</a></h3>
<p>SoundCloud have an obligation to artists and labels and choose to be very strict in authorising uploads that aren&#8217;t your own. I&#8217;ve wondered how copyright works for these sites, <a href="http://www.mixcloud.com/" title="Home: Listen to trusted DJs, radio hosts &amp; Podcasters. Start here | Mixcloud - Re-think radio" target="_blank">Mixcloud</a> for example host countless mixed songs and sets.</p>
<p>Mixcloud in fact is where you’ll now find the mixes saved indefinitely, without complaint, <a title="gemb1 | Mixcloud - Re-think radio" href="http://www.mixcloud.com/gemb1/" target="_blank">on Gemma’s page</a>.</p>
<p>Have a listen!</p>
<div><object width="300" height="300"><param name="movie" value="http://www.mixcloud.com/media/swf/player/mixcloudLoader.swf?v=106"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="flashVars" value="feed=http://www.mixcloud.com/api/1/cloudcast/gemb1/my-marathon-mix-part-one.json&#038;embed_uuid=690cae07-7e5b-416d-9fb7-89a5ad9ce24c&#038;embed_type=widget_standard"></param><embed src="http://www.mixcloud.com/media/swf/player/mixcloudLoader.swf?v=106" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" flashvars="feed=http://www.mixcloud.com/api/1/cloudcast/gemb1/my-marathon-mix-part-one.json&#038;embed_uuid=690cae07-7e5b-416d-9fb7-89a5ad9ce24c&#038;embed_type=widget_standard" width="300" height="300"></embed></object>
<div style="clear:both; height:3px;"></div>
<div style="clear:both; height:3px;"></div>
</div>
<p><a title="gemb1 - My Marathon Mix - Part One | Mixcloud - Re-think radio" href="http://www.mixcloud.com/gemb1/my-marathon-mix-part-one/" target="_blank">Marathon Mix &#8211; Part One</a><br />
<a title="gemb1 - My Marathon Mix - Part Two | Mixcloud - Re-think radio" href="http://www.mixcloud.com/gemb1/my-marathon-mix-part-two/" target="_blank">Marathon Mix &#8211; Part Two</a><br />
<a title="gemb1 - My Marathon Mix - Part Three | Mixcloud - Re-think radio" href="http://www.mixcloud.com/gemb1/my-marathon-mix-part-three/" target="_blank">Marathon Mix &#8211; Part Three</a><br />
<a title="gemb1 - My Marathon Mix - Part Four | Mixcloud - Re-think radio" href="http://www.mixcloud.com/gemb1/my-marathon-mix-part-four/" target="_blank">Marathon Mix &#8211; Part Four</a><br />
<a title="gemb1 - My Marathon Mix - Part Five | Mixcloud - Re-think radio" href="http://www.mixcloud.com/gemb1/my-marathon-mix-part-five/" target="_blank">Marathon Mix &#8211; Part Five</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.marchibbins.com/2011/04/27/the-show-must-go-on/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>If You Got the Money</title>
		<link>http://blog.marchibbins.com/2011/04/26/if-you-got-the-money/</link>
		<comments>http://blog.marchibbins.com/2011/04/26/if-you-got-the-money/#comments</comments>
		<pubDate>Tue, 26 Apr 2011 22:43:54 +0000</pubDate>
		<dc:creator>Marc Hibbins</dc:creator>
				<category><![CDATA[beatspermile]]></category>
		<category><![CDATA[iheartplay]]></category>

		<guid isPermaLink="false">http://blog.marchibbins.com/?p=1642</guid>
		<description><![CDATA[How Beats Per Mile uses the JustGiving platform to show an up-to-date fundraising total using their PHP SDK.]]></description>
			<content:encoded><![CDATA[<p>The fundraising amount shown on <a title="Beats Per Mile" href="http://coffeeandpie.net/beatspermile/" target="_blank">Beats Per Mile</a> uses the <a title="Developer - JustGiving" href="http://www.justgiving.com/developer/" target="_blank">JustGiving API</a> to show the up-to-date figure on Gemma&#8217;s donation page.</p>
<p>By way of one of their various SDKs, the JustGiving API is a doddle to use and is pretty well featured. Unfortunately<a title="JustGiving" href="http://apimanagement.justgiving.com/" target="_blank"> the documentation</a> isn’t particularly comprehensive.</p>
<p>You can expect to be able to fetch any information found on a typical page — event and charity details, lists of donations and comments, even the colour scheme — all of which can be fetched without any need for authentication.</p>
<p>We&#8217;re using the PHP SDK to show a very simple totaliser, the current percentage raised of the target amount. This can be achieved in just a few lines of code.</p>
<p>Firstly you need to register your application, which will get you an API key and access to the staging sandbox. This access is only for development, you can query live pages but the information you&#8217;ll obtain is out-of-date.</p>
<p>Applications go through a two day approval process but require nothing more than an decent application description, from that point on you&#8217;re free to access real pages and live data.</p>
<p>The <a title="JustGiving/JustGiving.Api.Sdk - GitHub" href="https://github.com/JustGiving/JustGiving.Api.Sdk" target="_blank">PHP SDK is available from GitHub</a> and comes with plenty of examples and cover a lot of use cases for data queries.</p>
<p>As I say, the API offers plenty more, such as automatic page creation, user account creation and <a title="Simple Donation Integration - JustGiving" href="https://www.justgiving.com/developer/simple-donation-integration/" target="_blank">integrating donations from third-party sites</a>.</p>
<p>Our needs are far simpler, however. The code is exactly this:</p>
<blockquote><p>include &#8216;./JustGivingClient.php&#8217;;</p>
<p>$client = new JustGivingClient(&#8220;https://api.justgiving.com/&#8221;, &#8220;API_KEY&#8221;, 1);<br />
$page = $client-&gt;Page-&gt;Retrieve(&#8220;Gemma-Bardsley&#8221;);<br />
$funds = $page-&gt;grandTotalRaisedExcludingGiftAid;<br />
$target = round($page-&gt;fundraisingTarget);<br />
$percent_raised = round($funds/$target * 100);</p></blockquote>
<p>As simple as querying the page, grabbing the current total and the target total then determining the percentage.</p>
<p>We retrieve the page via it’s short name, which for us is “Gemma-Bardsley”, this will be set in the JustGiving page admin area.</p>
<p>Note that the target amount has to be explicitly set there too (you&#8217;ll get a similar totaliser on your fundraising page anyway if that&#8217;s set correctly) and this queries the live API staging over HTTPS, not the developer sandbox.</p>
<p>The third parameter sent to the client constructor specifies version one of the API.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.marchibbins.com/2011/04/26/if-you-got-the-money/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Keep On Running</title>
		<link>http://blog.marchibbins.com/2011/04/13/keep-on-running/</link>
		<comments>http://blog.marchibbins.com/2011/04/13/keep-on-running/#comments</comments>
		<pubDate>Wed, 13 Apr 2011 18:21:32 +0000</pubDate>
		<dc:creator>Marc Hibbins</dc:creator>
				<category><![CDATA[beatspermile]]></category>
		<category><![CDATA[iheartplay]]></category>

		<guid isPermaLink="false">http://blog.marchibbins.com/?p=1603</guid>
		<description><![CDATA[How Beats Per Mile uses RunKeeper to retrieve and visualise live running data from an iPhone strapped to a marathon runner.]]></description>
			<content:encoded><![CDATA[<p>Central to the <a title="Beats Per Mile" href="http://coffeeandpie.net/beatspermile/" target="_blank">Beats Per Mile</a> application is the run data, the live information reporting Gemma’s current location, elapsed time, covered distance, average pace and speed.</p>
<p>Working out how to capture this information was our first challenge. Determining whether it was even possible was pivotal as to whether we could build the site.</p>
<p>Before deciding on the eventual solution of using <a title="Running app and fitness community | RunKeeper" href="http://runkeeper.com/" target="_blank">RunKeeper</a>, we looked for a few different routes we could take, of varying levels of complexity.</p>
<p>Fellow <a title="iheartplay" href="http://iheartplay.com/" target="_blank">iheartplay</a> developer <a title="Home › Birdsigh" href="http://birdsigh.com" target="_blank">Tim</a> took up the challenge.</p>
<p>Firstly we considered building our own hardware, the basic idea being a GSM/GPS module for about £50 along with a SIM card with unlimited text messages. The module is then programmed to send it&#8217;s location to Twitter via SMS every minute or so.</p>
<p>There are a lot of posts around with extensive details on how to build this kind of thing yourself, <a title="OHARARP LLC » Twitter Tracker" href="http://ohararp.com/wp/?p=119" target="_blank">here’s one doing exactly that</a>. He’s also considering selling pre-built units, which would save us needing to get our soldering irons out.</p>
<p>With this option, whilst the geo-positioning data would be accurate, you can run pretty far in a minute so the route would be a little raw and wouldn&#8217;t accurately reflect the true path and wouldn&#8217;t look great on a map.</p>
<p>One step further would be to use something like <a title="Open GPS Tracker" href="http://www.opengpstracker.org/" target="_blank">Open GPS Tracker</a>, which is similar but rather than using a combined GSM/GPS module just uses a GPS module plugged directly into a mobile phone.</p>
<p>This particular unit comes pre-built and with firmware, so perhaps might be limited in what we could capture, but otherwise ready to roll straight out of the box and only requires an old (pre-paid) handset. It’s also small in size, easy enough to strap onto a runner or put in a pocket. This comes in around £50ish for parts again, plus $35.91 for the software.</p>
<p>There are commercially manufactured units available too, trackers for mountain climbers, skiers, pilots, sailors and such that aren’t products of home brew electronics.</p>
<p>The <a title="SPOT Personal Tracker" href="http://www.findmespot.eu/en/index.php?cid=101" target="_blank">SPOT Personal Tracker</a> is one option, a satellite GPS tracker with built-in location services including sending your current location to Google Maps in near real-time, specifically for sharing with others at home. Very reliable and robust, tiny and light, the location services lend to exactly what we want to do but it&#8217;s very over-spec. It&#8217;s able to work beyond cellular coverage and above 15,000ft — not particularly necessary for us — and comes in at €149 for the unit and a service charge of €99 for a years use.</p>
<p>It does however come with a particularly impressive distress button, notifying the GEOS International Emergency Response Center in case of emergencies should one press it. Presumably this calls in an airlift or team of S.A.R. St. Bernards, which could come in handy when she hits the wall.</p>
<p>Satellite GPS is more immediately available though, in the form of an iPhone.</p>
<p>Initially we considered developing an app from scratch, but that’s not really anything any of us have done before. We could make an online application running on Safari with the <a title="Geolocation - Dive Into HTML5" href="http://diveintohtml5.org/geolocation.html" target="_blank">Geolocation API</a>, but this would mean having a page open for the entire run and hoping that it doesn’t time out or anything gets pressed on-screen as arms start swinging.</p>
<p>On the app store and found <a title="InstaMapper - Free Real-Time GPS Tracking" href="http://www.instamapper.com/" target="_blank">InstaMapper</a>, a lo-fi and free app which sends location updates to the InstaMapper site which are available via a public API. It’s compatible on iPhone, Android and any Java-capable phone, it’s well supported and tested — a far more viable solution than anything we could knock up in the limited time we have. It also works with the phone locked, conserving battery life. The only cost is the data plan, which Gemma already has.</p>
<p>Then came the revelation of <a title="RunKeeper Elite" href="http://runkeeper.com/elite" target="_blank">RunKeeper Elite</a>, our eventual winner. At the time, Gemma had only just started using RunKeeper Pro — the Pro and Elite labels refer to your subscription status, Elite is a premium service for $4.99 — and we hadn’t really looked into what was available with the upgrade.</p>
<p>RunKeeper does all the things you’d expect from a standard GPS-enabled running app (or Garmin device) — track time, distance, pace, calories burned etc, over a geolocated route visualised on a map, stored with all your previous runs.</p>
<h3><a name="RunKeeper_Elite" class="anchor">RunKeeper Elite</a></h3>
<p>The Elite service offers a few training programs, alerts and reports and <a title="Introducing… RunKeeper Live! | RunKeeper" href="http://blog.runkeeper.com/uncategorized/introducing-runkeeper-live" target="_blank">as of March last year added a ‘Live’ service</a> that pushes your running data to their website as you run, rather than only uploading a report once it’s complete (as the Pro version does). It’s exactly the same data, but made publicly available in real time.</p>
<p><iframe title="YouTube video player" width="480" height="390" src="http://www.youtube.com/embed/VL81NLe8iy4" frameborder="0" allowfullscreen></iframe></p>
<p>Unfortunately, RunKeeper doesn&#8217;t actually have an API yet. <a title="Is there an API? / Questions / Discussion Area - RunKeeper Support" href="http://support.runkeeper.com/discussions/questions/135-is-there-an-api" target="_blank">On the forums</a> they <a title="Is there an API? / Questions / Discussion Area - RunKeeper Support" href="http://support.runkeeper.com/discussions/questions/135-is-there-an-api#comment_5939628" target="_blank">promise to soon</a>, but not before race day.</p>
<p>Googling around there are a few sites that have worked out ways to grab the data. For example, <a title="Runkeeper API  : firehole" href="http://www.firehole.us/runkeeper-api/" target="_blank">Firehole has developed an interface</a> to capture and save activity, there’s a ton of info in the right-hand column of his page. There are plenty of ways to <a title="Creating a Runkeeper RSS Feed - the hack way. | ASchroder.com" href="http://www.aschroder.com/2011/01/creating-a-runkeeper-rss-feed-the-hack-way/" target="_blank">hack and scrape</a>, but it&#8217;s a bit dirty and not particularly relevant to us, but shows the data retrieval is possible nonetheless.</p>
<p>These seem to concern the syndication of complete activities, publishing a list of recent runs as one would their latest tweets, or calculating the total number of miles run per month, say. But we&#8217;re only interested in the data for one run, a single activity.</p>
<p>Rather than regularly monitoring a profile for updates, we only have to load the data that populates a single RunKeeper page.</p>
<p>This is handy, because rather than scraping and traversing a profile to find what we&#8217;re looking for (from rendered HTML) we can load the single data file directly, a JSON file hosted on RunKeeper, then work with the data in exactly the same way as they do.</p>
<p>Now, this is all a bit naughty. According the the <a title="RunKeeper - Terms of Service" href="http://runkeeper.com/termsofservice" target="_blank">RunKeeper Terms of Service</a>, we are strictly not allowed to load the data from their site in this way, even with the permission of the account holder. Any form of scraping at all, is prohibited.</p>
<p>This is a big problem. So what do we do?</p>
<p>Well Len Hardy of Firehole openly links to his API interface <a title="Is there an API? / Questions / Discussion Area - RunKeeper Support" href="http://support.runkeeper.com/discussions/questions/135-is-there-an-api#comment_2266949" target="_blank">in the forum thread mentioned before</a>, pointing out that you can scrape the page data, that he does so and that he&#8217;ll freely share the code. RunKeeper haven&#8217;t responded to his comment specifically or reminded him of their TOS, but have commented later on the same thread.</p>
<p>I think it&#8217;s highly unlikely that RunKeeper could be unaware of these goings on, these scrapers are easily found if you search for them and have been around for a while without being shut down.</p>
<p>I&#8217;m rather hoping that they actually don&#8217;t mind too much, right now. They must be aware of the demand, they have an API in development. Perhaps when that&#8217;s published they&#8217;ll chase up these sites and direct them that way. I&#8217;m sure (read, hope) that the developers would happily migrate to doing things the correct way.</p>
<p>So what&#8217;s our solution? Well, <a title="Twitter: marchibbins to @runkeeper" href="http://twitter.com/marchibbins/status/53375077240152064" target="_blank">I asked anyway</a>, but no response so far. We will go ahead and load the data, but in my opinion do so more conscientiously than some of the other attempts. For one, we&#8217;re not loading and filtering complete HTML pages as they are, that&#8217;s what screen scraping is, attempting to extract data from a rendered output. We&#8217;ll be loading the data directly, which is publicly accessible and hopefully with less overhead being a straight-up JSON file.</p>
<p>We&#8217;re also not loading anything regularly or for a long duration of time. We&#8217;re only concerned with one activity, the site will only load the data once the race has begun and when it&#8217;s finished we&#8217;ll take a copy of the file and serve it from our own servers as a static file.</p>
<p>We also intend on intelligently caching the data on our servers whilst the marathon is in progress. This way we can limit the amount of requests to RunKeeper and take on some of the load ourselves. It&#8217;s easily done and will actually improve the performance of the application anyway.</p>
<p>Really, I would like RunKeeper to see what we&#8217;re doing and think it&#8217;s cool. They have an awesome product and platform, people want to play with it more.</p>
<h3><a name="How_it_works" class="anchor">How it works</a></h3>
<p>Once the race starts, all the data we need will be continually pushed from Gemma’s iPhone to the RunKeeper site.</p>
<p>Beats Per Mile will handle page requests and retrieve this data from RunKeeper in the form of a JSON file, which the server will then cache. We can cache the data knowing that the route already run will not change, we’re only interested in the new stuff.</p>
<p>RunKeeper updates their page every 25 seconds, so our cache will be about this length too. When a request is made, if the cache is still fresh we’ll serve that data, if not we’ll retrieve more from RunKeeper.</p>
<p>From then on, the page polls for the latest dataset and we serve the new information, appending data to the client-side stored JSON. The application works our what data each individual client has and serves the difference.</p>
<p>Once the run is complete, a flag will be raised by RunKeeper in the JSON file and we’ll update our page to reflect that. From this point on the application no longer needs to contact RunKeeper, so will serve static JSON from our own side.</p>
<p>It shapes up a little like this, where points 2 and 8 are conditional to the cache being in date:</p>
<p><a href="http://blog.marchibbins.com/wp-content/uploads/2011/04/bpm_flow.jpg" target="_blank"><img class="aligncenter size-medium wp-image-1615" title="Beats Per Mile data diagram" src="http://blog.marchibbins.com/wp-content/uploads/2011/04/bpm_flow.jpg" alt="" width="500" height="140" /></a></p>
<p>As well as reducing the amount of calls to RunKeeper, caching the data means that visitors get the latest JSON nice and quickly.</p>
<p>I built a prototype in time for the Silverstone Half Marathon which Gemma ran last month which all went surprisingly smoothly. Here&#8217;s an image of the application as it was then:</p>
<p><a href="http://twitpic.com/46rn1x" target="_blank"><img class="aligncenter size-full wp-image-1619" title="Beats Per Mile prototype" src="http://blog.marchibbins.com/wp-content/uploads/2011/04/bpm_prototype.jpg" alt="" width="500" height="308" /></a></p>
<p>The data is visualised on a Google map. The GPS coordinates are used to draw the red Polyline, the activity statistics are calculated by the iPhone app and simply refreshed here with every update.</p>
<p>Eventually our map will also have some more informative overlays, mile markers for example, and when and where the application sent tweets or found pictures taken nearby.</p>
<p>Initially we were going to show pace, speed and elevation graphs (you can see a very early attempt if you click through to the image link) but we&#8217;ve run out of time for those. Maybe version two.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.marchibbins.com/2011/04/13/keep-on-running/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Gave it a Name</title>
		<link>http://blog.marchibbins.com/2011/04/02/gave-it-a-name/</link>
		<comments>http://blog.marchibbins.com/2011/04/02/gave-it-a-name/#comments</comments>
		<pubDate>Sat, 02 Apr 2011 18:07:43 +0000</pubDate>
		<dc:creator>Marc Hibbins</dc:creator>
				<category><![CDATA[beatspermile]]></category>
		<category><![CDATA[iheartplay]]></category>

		<guid isPermaLink="false">http://blog.marchibbins.com/?p=1589</guid>
		<description><![CDATA[Introducing Beats Per Mile, the marathon project.]]></description>
			<content:encoded><![CDATA[<p>In my last post I previewed a forthcoming project where we track a marathon as it&#8217;s run in real-time, with a whole heap of maps, graphs, tweets and APIs flying about the place.</p>
<p>It has now been named <a title="Beats Per Mile" href="http://coffeeandpie.net/beatspermile/" target="_blank">Beats Per Mile</a>.</p>
<p>We&#8217;ve put up a <a title="Beats Per Mile" href="http://coffeeandpie.net/beatspermile/" target="_blank">holding page</a> on Gemma&#8217;s site (she&#8217;s the one having to run the thing) and she&#8217;s written up <a title="coffee and pie » Blog Archive » Beats per mile" href="http://coffeeandpie.net/?p=2991" target="_blank">a preview on her blog</a>, too.</p>
<p><a href="http://coffeeandpie.net/beatspermile/"><img class="aligncenter size-full wp-image-1600" title="Beats Per Mile" src="http://blog.marchibbins.com/wp-content/uploads/2011/04/bpm.jpg" alt="" width="400" height="545" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.marchibbins.com/2011/04/02/gave-it-a-name/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Modern Running</title>
		<link>http://blog.marchibbins.com/2011/03/30/modern-running/</link>
		<comments>http://blog.marchibbins.com/2011/03/30/modern-running/#comments</comments>
		<pubDate>Wed, 30 Mar 2011 22:03:32 +0000</pubDate>
		<dc:creator>Marc Hibbins</dc:creator>
				<category><![CDATA[beatspermile]]></category>
		<category><![CDATA[iheartplay]]></category>

		<guid isPermaLink="false">http://blog.marchibbins.com/?p=1570</guid>
		<description><![CDATA[Tracking a marathon race in real-time, GPS-locating a marathon runner, live fundraising updates, tweets and pictures all in one application.]]></description>
			<content:encoded><![CDATA[<p>I mentioned before that I’m working on a new project that combines two big interests of mine, programming and running.</p>
<p>Whenever I run I use a <a title="Garmin: Into Sports" href="http://www.garmin.com/garmin/cms/site/us/intosports/" target="_blank">Garmin device</a> for timing and recording. It’s GPS-enabled, so also tracks my location, evelation, speed and pace. It also has a wireless heart-rate monitor. Once I’ve completed a workout, it’s logged in the desktop software they provide, to track my progress and compare with similar activities.</p>
<p>It also provides an option to export the activity as an XML file. This is usually for transferring to other devices, but as a developer, presents a huge opportunity to mess around with the data.</p>
<p>Plenty of people have created mashups with run data (<a title="RunVis - Visualizing GPS Running Data from a Garmin 305. | BIT-101" href="http://www.bit-101.com/blog/?p=2463" target="_blank">usually map-based</a>) and I&#8217;ve often wanted to try my hand too.</p>
<p>I’ve entered the ballot for the London Marathon for the past three years but haven’t ever been successful. I figured if I ever did run, the race would give me a huge volume of data to play with — and besides, would would be far too big an occasion to pass up doing <em>something</em>.</p>
<p>I was unsuccessful this year too, but a <a title="coffee and pie » Blog Archive » Run this town" href="http://coffeeandpie.net/?p=2517" target="_blank">friend of mine gained a last minute place</a> at the end of January, so the spotlight turns to her to be the star of the show.</p>
<p>Gemma doesn’t train with a Garmin, she uses the <a title="Running app and fitness community | RunKeeper" href="http://runkeeper.com/" target="_blank">RunKeeper Elite iPhone app</a> which records the same kind of data, so we can perform the same kind of analysis.</p>
<p>Brilliantly, it also publishes the activity to the RunKeeper website <em>live </em>as she’s running.</p>
<p>Where my data would be only accessible after the race, to manipulate and visualise after the fact, her data will be readily available as the marathon is being run.</p>
<p>This means we can map her progress around the course on race day, tracking her position, so we’re able to watch from home or on a mobile device at the event. We will have real-time access to her elapsed time, total distance and metrics like current pace, so we can keep up-to-date with her performance as the race is run, rather than retrospectively look back on the result.</p>
<p>RunKeeper isn&#8217;t the only toy to play with.</p>
<p>Gemma is running the race for the charity <a title="RettUK : Rett UK" href="http://www.rettuk.org/rettuk-public/rettuk.html" target="_blank">Rett UK</a> and hopefully, if the application is interesting enough, we can drum up some additional fundraising contributions. She has <a title="Gemma Bardsley is fundraising for RETT syndrome Association UK" href="http://www.justgiving.com/Gemma-Bardsley" target="_blank">a Just Giving page</a>, who offer an API to access their data. We can show to-the-minute fundraising levels and take donations from our site.</p>
<p>She’s also inseparable of her iPod when running and has asked friends to contribute tracks to a race day playlist. We’re considering using either the <a title="SoundCloud Developer Zone" href="http://soundcloud.com/developers" target="_blank">Soundcloud API</a> or <a title="API documentation | Mixcloud - Re-think radio" href="http://www.mixcloud.com/developers/documentation/" target="_blank">Mixcloud API</a> to stream this playlist on the site, synchronised with her start time so visitors can hear exactly what she’s listening to.</p>
<p>Knowing when and where she is on the course, we are able to Tweet updates such as mile paces and intermediate timings, when she’s passed notable landmarks or approaching the finish line. Collecting good-will tweets is also pretty easy with a hash tag or mention.</p>
<p>Another idea is to pull in photos taken on the day, using the time and location data around busy spectator spots. For example, <a title="instagr.am - A Real-Time API for Next-Generation Apps" href="http://instagr.am/blog/42/realtime-api" target="_blank">Instagram have recently released an API</a> to retrieve their geolocated pictures.</p>
<p>By querying the API at certain locations at the time we know Gemma has passed them, we can create our own gallery of Where’s Wally style photos in case anyone paps her — if not, we’re left with some nice pictures of the race.</p>
<p>So an assortment of APIs to play with, we should be able to produce something pretty cool. I’ll likely be blogging my experiments with each platform, none of which I’ve worked with before — other than Twitter, back when it was Basic Auth.</p>
<p>Less than three weeks to go, can’t be as hard as actually having to run the twenty-six miles can it?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.marchibbins.com/2011/03/30/modern-running/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

