« The problem with Django's Template Tags | Announcing Django Crawler and django-test-utils »
A couple posts back, I was talking about software that I use all the time. I was going through and linking to all of the software. I would go to google, type in the project name, go to the first result, and copy that URL back into my post. I figured that there had to be a better way. Any software project worth it's name owns the top result in google.
The web is a dynamic place, and websites move, change, and disappear all the time. The popularity and importance of some things change over time as well. So I was thinking about how to go about linking to the most popular thing for a search. It also happened to be useful for my last post and linking to things. A similar approach could be used with Wikipedia. Creating something that just linked to somethings wikipedia page.
I couldn't use Google, because I couldn't find a good web search API for them. Yahoo however has a really nice one that is out there to use. This, combined with James Bennett's awesomely useful template utils allowed me to whip this up in about 10 minutes. Here's the code below.
from template_utils.nodes import ContextUpdatingNode
from yahoo.search.web import WebSearch
class SearchNode(ContextUpdatingNode):
def __init__(self, search):
self.search = search
def get_content(self, context):
srch = WebSearch('EricSearch', query=self.search)
res = srch.parse_results()
return {'top_url': res.results[0].Url}
@register.tag
def first_yahoo_link(parser, token):
return SearchNode(token.split_contents()[1])
Easy as pie, and awesome. This ties into my previous post about template tags being hard to write. If you just want to make a template tag that sets a context, it's as easy as making a node and returning a dictionary in the get_content() function. This isn't a super robust solution, but now i can do {% first_yahoo_link "search terms" %} and {{ top_url }} will contain the Url of it!
Also note how easy it is to use Yahoo's search api! That's awesome. If this was on a more highly trafficked site, you would want to cache the results (maybe daily, because they shouldn't change much). I may go ahead and do a tutorial on how to do caching with template tags and template-utils if people are interested in it.
The observant will note that this doesn't help me writing a blog post, because there's no way to call a template tag from within. That might be something for me to cook up later this month. The template tag is still neat however, for introducing yahoo's web api, and template-utils.
Posted at 2:49 p.m. on November 9, 2008
Comments: 8
Tags: django , idea , post-a-day , search , template-tags
The problem with Django's Template Tags
Making a Django Uber-Community
The value of conventions, aka testmaker for template tags.
Welcome to the home of Eric Holscher on the web. I talk about software development, mostly in the realm of Django. I am interested in the real time web, testing, mobile apps, and other things.
Why Read the Docs matters
2 weeks, 1 day ago (Comments: 7)
Read the Docs Update
10 months ago (Comments: 2)
Using Reviewboard with Git
1 year ago (Comments: 0)
Read the Docs Updates
1 year ago (Comments: 1)
Handling Django Settings Files
1 year ago (Comments: 12)
Required Reading
1 year, 2 months ago (Comments: 0)
Using Haystack to index non-database content
1 year, 2 months ago (Comments: 4)
Correct commands to check out and update VCS repos
1 year, 2 months ago (Comments: 0)
Site upgrades
1 year, 2 months ago (Comments: 0)
Building a Django App Server with Chef: Part 4
1 year, 2 months ago (Comments: 1)
Setting up Django and mod_wsgi
Building a Django App Server with Chef: Part 1
Screencast: Django Command Extensions
Big list of Django tips (and some python tips too)
Handling Django Settings Files
Lessons Learned From The Dash: Easy Django Deployment
Large Problems in Django, Mostly Solved: Delayed Execution
Building a Django App Server with Chef: Part 2


Comments
1 RichardH says...
Neat idea Eric. I would definitely be interested in a tutorial covering caching with template side of life. That's not something I have given any thought to.
Posted at 9:45 p.m. on November 9, 2008
2 Dave K says...
You seriously couldn't find the Google API? Not to mention you don't even need an API you just need a URL :P
http://www.google.com/search?hl=en&btnI=I'm+Feeling+Lucky...
Posted at 10:19 p.m. on November 9, 2008
3 Eric Florenzano says...
I really like this idea! Very cool, simple, powerful idea. I was also unaware of the whole template_utils ContextUpdatingNode thing, which looks really nice.
Posted at 10:48 p.m. on November 9, 2008
4 Benjamin Kudria says...
Also, see http://str8.to
Posted at 11:37 p.m. on November 9, 2008
5 Al says...
The Google Search API lives at: http://code.google.com/apis/ajaxsearch/
Posted at 10:56 a.m. on November 10, 2008
6 Alexey says...
As Benjamin said, feel free to use http://str8.to/the-project-name if you want it to work for Google.
Posted at 6:25 p.m. on November 10, 2008
7 Chris Heisel says...
That's a great concept!
You'd have to carefully pick the search terms to get the result set[0] that you want.
It should be pretty easy to drop an XML or Markdown/Textile-ish "tag" into a body of a post and templatefilter it...
Posted at 10:15 p.m. on November 10, 2008
8 barbara says...
Ooh, thank you - I can think of so many fun use cases for this - suggesting content to a user based on tags, or some profile variable - the possibilities are endless.
Posted at 7:58 p.m. on November 11, 2008