Login

Twitter status tag

Author:
gmacgregor
Posted:
February 11, 2008
Language:
Python
Version:
.96
Score:
4 (after 4 ratings)

Requires the twitter module (easy_install python_twitter).

Your project settings file should define TWITTER_USERNAME.

Call the tag like:

{% get_twitter_status as tweet tweet_time tweet_url %} <p><q cite="{{ tweet_url }}">{{ tweet }}</q> ({{ tweet_time }})</p>

EDIT: I've also included an alternative method as suggested in the comments. Called with:

{% get_twitter_status as tweet %} <p><q cite="{{ tweet.url }}">{{ tweet.status }}</q> ({{ tweet.time }})</p>

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from django.template import Library, Node, TemplateSyntaxError
from django.conf import settings
import twitter

register = Library()

class TwitterStatusNode(Node):
	def __init__(self, tweet, tweet_time, tweet_url):
		self.tweet = tweet
		self.tweet_time = tweet_time
		self.tweet_url = tweet_url
	
	def render(self, context):
		try:
			api = twitter.Api()
			most_recent_status = api.GetUserTimeline(settings.TWITTER_USERNAME)[0]
			context[self.tweet] = most_recent_status.text
			context[self.tweet_time] = most_recent_status.relative_created_at
			context[self.tweet_url] = "http://twitter.com/%s/statuses/%s" % (settings.TWITTER_USERNAME, most_recent_status.id)
		except:
			context[self.tweet] = "Ack! Looks like Twitter's codes are broken!"
			context[self.tweet_time] = ""
			context[self.tweet_url] = ""
		return ''

@register.tag(name='get_twitter_status')
def twitter_status(parser, token):
	"""
	Call this tag with: 
		get_twitter_status as tweet_var tweet_time_var tweet_url_var
	"""
	bits = token.split_contents()
	if len(bits) != 5:
		raise TemplateSyntaxError, "%s takes 4 arguments" % bits[0]
	if bits[1] != "as":
		raise TemplateSyntaxError, "First argument for %s should be 'as'" % bits[0]
	return TwitterStatusNode(bits[2], bits[3], bits[4])

######################################################
# Alternative to the above tag
######################################################

class TwitterStatusNode(Node):
	def __init__(self, tweet):
		self.tweet = tweet
	
	def render(self, context):
		try:
			api = twitter.Api()
			most_recent_status = api.GetUserTimeline(settings.TWITTER_USERNAME)[0]
			context[self.tweet] = {
				"status": "%s" % most_recent_status.text,
				"url": "http://twitter.com/%s/statuses/%s" % (settings.TWITTER_USERNAME, most_recent_status.id),
				"time": "%s" % most_recent_status.relative_created_at,
			}			
		except:
			context[self.tweet] = {
				"status": "Ack! Looks like Twitter's codes are broken!",
				"url": "",
				"time": "",
			}			
		return ''

@register.tag(name='get_twitter_status')
def twitter_status(parser, token):
	"""
	Call this tag with: 
		get_twitter_status as tweet
	"""
	bits = token.split_contents()
	if len(bits) != 3:
			raise TemplateSyntaxError, "%s takes 2 arguments" % bits[0]	
	if bits[1] != "as":
		raise TemplateSyntaxError, "First argument for %s should be 'as'" % bits[0]
	return TwitterStatusNode(bits[2])

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

kylefox (on February 11, 2008):

This is cool, but why not have the Node set a dictionary-like context variable? That would make your syntax like this:

{% get_twitter_status as tweet %}

<q cite="{{ tweet.url }}">{{ tweet.status }}</q> at {{ tweet.time }}

#

gmacgregor (on February 12, 2008):

Nice suggestion! I've gone ahead and added an alternative method that does just that.

#

yeago (on March 14, 2009):

One thing I don't like is that it picks up @messages

#

Please login first before commenting.