Re: setting template variables from plugins/ unkludging code/

"William Cybriwsky" <[email protected]>
Newsgroups gmane.comp.web.pyblosxom.devel
Message-ID <[email protected]>
scratch that, host is back up. You can find the plugin attached.

On 7/7/07, William Cybriwsky <[email protected]> wrote:
>
> oh, I've been talking with will and have actually developed the plugin to
> the point where it is
> a) a proper plugin
> b) gets the uname/pass from config.py
> c) provides the last 'tweet', its date, and its time as vars.
> d) caches for 60 seconds
>
> but my site just went down around an hour ago. thanks for the
> help+enthusiasm, this would be my first public code (ever).
>
> On 7/7/07, Ryan Barrett <pyblosxom-6sb6M7qyT/[email protected]> wrote:
> >
> > cool! it'd be great to have a twitter plugin for pyblosxom.
> >
> > On Fri, 6 Jul 2007, William Cybriwsky wrote:
> >
> > > Hi, I'm a new user of pyblosxom, and I wanted to know how to set
> > template
> > > variables from a plugin. I currently have the chunk of python code:
> >
> > if you want the variables to be available in existing templates, you can
> > set
> > them on the request data. for example:
> >
> >    def cb_prepare(args):
> >      data = args['request'].getData()
> >      data['twitterstatus'] = ...
> >
> > if you're rendering an entry yourself, you can set it directly on the
> > entry.
> > for example:
> >
> >    entry = args['entry']  # or entry = FileEntry(request) or ...
> >    entry['twitterstatus'] = ...
> >
> > see http://pyblosxom.sourceforge.net/1.3.1/api/ .
> >
> >
> > > P.S. : if I wanted to write some simple cache code, how would I just
> > do a
> > > check for a file age of 60 seconds?
> >
> > if i understand right, you only want to check your twitter status every
> > 60
> > seconds, so on each request, you want to check if tmptwit.xml is over 60
> > seconds old? try os.path.getmtime():
> >
> > http://docs.python.org/lib/module-os.path.html#l2h-2168
> >
> > -Ryan
> >
> > --
> > http://snarfed.org/
> >
>
>

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/

_______________________________________________
Pyblosxom-devel mailing list
Pyblosxom-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/pyblosxom-devel
twitget.py (application/octet-stream, 1.9 KB)
"""
twitget.py: twitter interaction for pyblosxom
	Copyright 2007 William Cybriwsky
"""
__author__      = "William Cybriwsky - jadeshade at gmail dot com"
__version__     = "0.00"
__url__         = "http://cornerguy.adakist.com"
__description__ = "Retrieve information from the twitter webservice"
__depends__ = ["curl"]

import os, time
from xml.dom import minidom

def verify_installation(request):
	sofar = 1
	config = request.getConfiguration()
	if not config.has_key("twitter_username"):
		print ""
		print "you need to specify a username in config.py!"
		print "example: py[\"twitter_username\"] = \"joe\""
		sofar = 0
	if not config.has_key("twitter_password"):
		print ""
		print "you need to specify a password in config.py!"
		print "example: py[\"twitter_password\"] = \"supahsekret\""
		print ""
		sofar = 0
	return sofar

def updatexml(username,password):
	getxml="curl -u %s:%s http://twitter.com/statuses/user_timeline.xml?count=1 > tmptwit.xml" % (username,password)
	os.system(getxml)

def maketimestring(mainxml,type):
	timestring = mainxml.childNodes[1].childNodes[0].data
	timelist = timestring.split(" ")
	if type == 0:
		del timelist[3:6]
		timestring = " ".join(timelist)
	if type == 1:
		timestring = timelist.pop(3)
	return timestring

def currentstatus(mainxml):
	return mainxml.childNodes[5].childNodes[0].data

def cb_prepare(incoming):
	request = incoming["request"]
	vars = request.getData()
	config = request.getConfiguration()
	(username,password) = (config["twitter_username"],config["twitter_password"])
	if not (os.path.exists("tmptwit.xml") and os.stat("tmptwit.xml").st_mtime + 60 < time.time()):
		updatexml(username,password)
	xmlResponse=minidom.parse("tmptwit.xml")
	mainxml=xmlResponse.childNodes[0].childNodes[1]
	vars["twitterdate"] = maketimestring(mainxml,0)
	vars["twittertime"] = maketimestring(mainxml,1)
	vars["twitterstatus"] = currentstatus(mainxml)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.