Trackback Class in Python/Django
Trackback is a simple way of linking blog entries together, and it might be the most misused for SPAM.
When i'm trying to build Gaelog, I google a lot about trackback lib writing in Python, but only get one called tblib. It's a very simple trackback class using the default httplib. The thing about httplib is that it can not set a timeout attribute which i think is very important in doing the trackback. Because if a remote server can not handle the post in time, the function will always be blocked.
So i end up writing my own trackback class based on the wonderful pycurl.
Here is the code ( Most of them based on the tblib, it's ok? ):
class TrackBack:
def __init__(self, tbUrl=None, title=None, excerpt=None, url=None, blog_name=None):
self.tbUrl = tbUrl
self.title = title
self.excerpt = excerpt
self.url = url
self.blog_name = blog_name
self.tbErrorCode = None
self.tbErrorMessage = None
def ping(self):
if self.tbUrl:
params = urllib.urlencode({'title': self.title, 'url': self.url, 'excerpt': self.excerpt, 'blog_name': self.blog_name})
agent = "Appoil.com/0.0.1 Python"
c = pycurl.Curl()
c.setopt(pycurl.URL, smart_str(self.tbUrl))
c.setopt(c.POSTFIELDS, params)
b = StringIO.StringIO()
c.setopt(pycurl.WRITEFUNCTION, b.write)
c.setopt(pycurl.FOLLOWLOCATION, 0)
c.setopt(pycurl.USERAGENT, agent)
c.setopt(pycurl.SSL_VERIFYPEER, 0)
c.setopt(pycurl.TIMEOUT, 30)
c.perform()
self.tbResponse = b.getvalue()
errorpattern = r'(.*?) '
reg = re.search(errorpattern, self.tbResponse)
if reg:
self.tbErrorCode = reg.group(1)
if int(self.tbErrorCode) == 1:
errorpattern2 = r'(.*?) '
reg2 = re.search(errorpattern2, self.tbResponse)
if reg2:
self.tbErrorMessage = reg2.group(1)
else:
return 1
If you came here from the trackback links that means this class works just fine. :-)
There is one more thing i should say about the pycurl. If you are trying to compile pycurl lib from source code, the LD_LIBRARY_PATH environment variable is a bit tricky. I spent quite some time to figure out why it don't accept the PATH i set. After compiling the lib for several times, it's ok within Bash. But Django still can't find the libcurl.so file. My last attempt was to copy the .so file into my own lib directory. Luckily everything seems all right this time.
Inspired by:
http://weblog.philringnalda.com/2003/02/01/tblib-a-python-trackback-library