Twitter status tag

 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. Twitter template tags and filters by moxypark 3 years, 7 months ago
  2. automating twitter by lawgon 4 years, 9 months ago
  3. Template tag: Last x twitter msgs of user by coulix 4 years, 9 months ago
  4. Template tag to sort a list of links by pytechd 6 years, 8 months ago
  5. Yet another SQL debugging facility by miracle2k 6 years, 8 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

#

(Forgotten your password?)