<?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>mikhail panchenko / blog &#187; nextbus</title>
	<atom:link href="http://mihasya.com/blog/tag/nextbus/feed/" rel="self" type="application/rss+xml" />
	<link>http://mihasya.com/blog</link>
	<description>good things now come in packages of three</description>
	<lastBuildDate>Sat, 14 Jan 2012 07:28:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>APIMuni by Danny Roa &#8211; Bringing NextMuni To The Masses</title>
		<link>http://mihasya.com/blog/apimuni-by-danny-roa-bringing-nextmuni-to-the-masses/</link>
		<comments>http://mihasya.com/blog/apimuni-by-danny-roa-bringing-nextmuni-to-the-masses/#comments</comments>
		<pubDate>Sun, 01 Mar 2009 05:44:53 +0000</pubDate>
		<dc:creator>mihasya</dc:creator>
				<category><![CDATA[dev]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[gae]]></category>
		<category><![CDATA[google app engine]]></category>
		<category><![CDATA[nextbus]]></category>
		<category><![CDATA[yourmuni]]></category>

		<guid isPermaLink="false">http://mihasya.com/blog/?p=245</guid>
		<description><![CDATA[Danny Roa, whom I met at the last Django Meetup, has put out a quick API for accessing Nextbus data. It&#8217;s hosted on the App Engine and can be found here. His writeup is here. He recycled the scraping code from yourmuni, props to him for giving props Of course, that just means that when [...]]]></description>
			<content:encoded><![CDATA[<p>Danny Roa, whom I met at the last <a title="Django Meetup Group" href="http://www.meetup.com/The-San-Francisco-Django-Meetup-Group/calendar/9527778/">Django Meetup</a>, has put out a quick API for accessing Nextbus data.</p>

<p>It&#8217;s hosted on the App Engine and can be found <a title="APIMuni" href="http://apimuni.appspot.com/">here</a>.</p>

<p>His writeup is <a title="Danny Roa - APIMuni Post" href="http://blog.dannyroa.com/2009/02/28/apimuni-on-google-app-engine/">here</a>.</p>

<p>He recycled the scraping <a title="yourmuni source" href="http://github.com/mihasya/yourmuni">code</a> from <a title="Yourmuni" href="http://yourmuni.appspot.com">yourmuni</a>, props to him for giving props <img src='http://mihasya.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Of course, that just means that when Nextbus gets angry, they&#8217;re going to come after me first!</p>

<p>Developers don&#8217;t create API&#8217;s for nothing, so I am eagerly anticipating what Danny is going to use this API for.</p>
]]></content:encoded>
			<wfw:commentRss>http://mihasya.com/blog/apimuni-by-danny-roa-bringing-nextmuni-to-the-masses/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>yourmuni makes commuting easier</title>
		<link>http://mihasya.com/blog/yourmuni-makes-commuting-easier/</link>
		<comments>http://mihasya.com/blog/yourmuni-makes-commuting-easier/#comments</comments>
		<pubDate>Sat, 17 Jan 2009 21:39:17 +0000</pubDate>
		<dc:creator>mihasya</dc:creator>
				<category><![CDATA[dev]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[google app engine]]></category>
		<category><![CDATA[muni]]></category>
		<category><![CDATA[nextbus]]></category>

		<guid isPermaLink="false">http://mihasya.com/blog/?p=203</guid>
		<description><![CDATA[I am proud to present my latest app yourmuni. It is a cross between momuni.com and Paul Hammond&#8217;s minimuni. Its purpose is to make it easier for people to get to and from places they frequent, such as jobs, gyms, favorite spots, and bootycalls. yourmuni lets you define bookmarks which represent collections of transit stops, [...]]]></description>
			<content:encoded><![CDATA[<dl class="wp-caption alignright" style="width: 330px;"> <dt class="wp-caption-dt"><img class="size-full wp-image-206" title="yourmuni screenshot" src="http://mihasya.com/blog/wp-content/uploads/2009/01/photo.jpg" alt="what PH's &quot;to work&quot; bookmark would look like" width="320" height="480" /></dt> </dl>

<p>I am proud to present my latest app <a title="yourmuni" href="http://yourmuni.appspot.com">yourmuni</a>. It is a cross between <a title="momuni" href="http://momuni.com">momuni.com</a> and Paul Hammond&#8217;s <a title="minimuni" href="http://minimuni.paulhammond.org">minimuni</a>. Its purpose is to make it easier for people to get to and from places they frequent, such as jobs, gyms, favorite spots, and bootycalls. yourmuni lets you define bookmarks which represent collections of transit stops, and then view the bus/train arrival information for each bookmark on a single page. For example, if Paul didn&#8217;t already have his highly personalized mimimuni app, he could log onto yourmuni, define a &#8220;To Work&#8221; bookmark, and assign to it the same stops that he currently scrapes. See the screenshot on the right for an example.</p>

<p>While it&#8217;s obviously not a &#8220;disruptive&#8221; innovation, I think it&#8217;s a nice incremental improvement on what most people do, which is look up multiple routes using momuni or nextbus.com while walking out the door. I know I&#8217;ve been using it, and it has saved me a tremendous amount of time/clicking around on my iPhone, looking like an idiot.</p>

<p>Though yourmuni was developed with my iPhone in mind, it appears to work just fine on most phones.</p>

<p>Still on the burner:</p>

<ul>
    <li>Instant stop lookup (ala momuni)</li>
    <li>using other agencies that nextbus covers (including ones outside of NorCal)</li>
    <li>deleting stops from bookmarks</li>
    <li>better instructions while setting up bookmarks</li>
    <li>cleaning up some code</li>
</ul>

<p>yourmuni was demoed at the January Django Meetup, and everyone seemed to like it. I was very flattered by the positive feedback, since it&#8217;s a rather simple app.</p>

<h3>Technical Details</h3>

<p>yourmuni is written using the latest Django at the time of the start of the project, which was r9768. My previous post about getting the latest Django to work on the Google App Engine was the result of me setting yourmuni up on said App Engine, which is where it now lives. The source is <a title="yourmuni source" href="http://github.com/mihasya/yourmuni">on github</a>. It&#8217;s far from perfect, as it was my first real Django/Python project, and I am aware of several precise places in the code that could use a minor rewrite. However, here are the parts that I put lots of thought into, and that I think might be useful to others.</p>

<h4>App Engine userRequired Decorator</h4>

<p>Since the login_required decorator from django.contrib is useless when using the App Engine, I wrote my own, which checks to see if the user is logged in and, if not,  redirects them to the Google Accounts login page, while saving the URL they were trying to access as the callback URL. Here&#8217;s the source for all to enjoy (gist <a title="gist for @userRequired" href="http://gist.github.com/48451">here</a>):
<pre>def userRequired(fn):
    """decorator for forcing a login"""
    def new(<em>args, **kws):
        user = users.get_current_user()
        if not (user):
            r = args[0]
            return HttpResponseRedirect(users.create_login_url(
                                            r.build_absolute_uri()))
        else:
            return fn(</em>args, **kws)
    return new</pre></p>

<h4>Encapsulating Slug Generation in Form Code</h4>

<p>Since I only ask for one field (&#8220;Description&#8221;) when creating a bookmark and place no restrictions on that field (i want it to look like whatever the user wants to see in the interface), I need some way to generate an identifier for the bookmark. I could just give it a numeric or hash identifier, but then it would be useless to the user in terms of seeing it in their browser history (I want to allow the user to jump straight to the bookmark they want if their browser shows it as an option after they type &#8216;y&#8217;). I needed to create a slug. I could accept the &#8220;Description&#8221; field and then process it in my addBmark view, but instead I defined the form to have two fields, one optional, and used the contents of the description field to automatically populate the &#8220;name&#8221; field using Django&#8217;s built in slugify method available in the template API (thanks to the folks at the Django Meetup who pointed this out). This allows me to encapsulate the validation within the form, so my view code looks very clean &#8211; I just have to call the is_valid() method on the form, and the form then has two properties that give me everything I need to create the bookmark. Here&#8217;s the code (full source <a title="forms.py on github" href="http://github.com/mihasya/yourmuni/blob/master/forms.py">here</a>).
<pre>class AddBmarkForm(forms.Form):
    name = forms.CharField(max_length=50, required=False)
    description = forms.CharField(max_length=255, required=True)</p>

<pre><code>def clean_description(self):
    desc = self.cleaned_data['description']
    name = slugify(desc).decode()
    q = db.Query(Bmark)
    q.filter('name =', name)
    q.filter('user =', users.get_current_user())
    if (q.get()):
        raise forms.ValidationError(_("A bookmark with that \
                    name exists already"))
    else:
        self.cleaned_data['name'] = name
        return desc&lt;/pre&gt;
</code></pre>

<h4>Scraping Nextbus</h4>

<p>For an unclear reason, nextbus does not have a clean, public API. My assumption is that they want to sell their data, but that's sort of pointless since they provide a free, publicly accessible website everywhere they provide service. It just sucks. So in order to make something better, I basically had to scrape that same publicly accessible website. It wasn't easy, as apparently nextbus hired a live bear to write their markup. Though all of their pages look almost identical, each has its own qurky combination of li, a, nobr, and font tags. I still managed to write a single scrape function to handle all of them, but it ended up being a bit more complex than it needed to be. Thank the powers that be for the BeautifulSoup library. The scrape code is <a title="nextbus scrape code" href="http://github.com/mihasya/yourmuni/blob/master/lib/nextbus.py">here</a>.</p>

<h4>Misc Stuff</h4>

<p>As I had mentioned before, I used the latest Django avaialble to me at the start of development. Though I don't get to play with the cool ORM stuff that's been added recently, I did get to use some of the new template tags, such as the {% empty %} tag to specify the behavior in the event of an empty {% for %} loop (docs <a title="docs for django empty tag" href="http://docs.djangoproject.com/en/dev/ref/templates/builtins/#for-empty">here</a>, used <a title="source of template using empty django tag" href="http://github.com/mihasya/yourmuni/blob/028cd96a90e8e73f835606b7d97a7e33927c1a7e/tpl/user/catch.html">here</a>).</p>

<p>All in all, I hope this helps people get to and from wherever it is they're going easier. This is the first project I've actually launched in a very long time, and certainly the most useful one.</p>
]]></content:encoded>
			<wfw:commentRss>http://mihasya.com/blog/yourmuni-makes-commuting-easier/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

