#!/usr/bin/python
# vimtips.py -- parse tips from www.vim.org
# written by Ali Polatel
import sys
import re, urllib2
def gettip(id):
""" Get the tip from www.vim.org
"""
tip_url = 'http://www.vim.org/tips/tip.php?tip_id=%d' % id
data = urllib2.urlopen(tip_url)
return data.read()
def parsetip(tip):
""" Parse the tip
"""
title_r = re.compile('(Tip #\d.*?)\s:')
rating_r = re.compile('Rating\s(-?\d+/\d+)')
body_r = re.compile('
(.*)
')
try:
title = title_r.search(tip).groups()[0]
except AttributeError: # Tip doesn't exist
return None
rating = rating_r.search(tip).groups()[0]
body = body_r.search(tip).groups()[0]
body = body.replace('\r','')
lst = tip.split('\n')
# The lines we want are two lines below these lines
created_td = ' created: | '
complexity_td = ' complexity: | '
author_td = ' author: | '
version_td = ' as of Vim: | '
created = re.sub('\s*<.?td>','',lst[lst.index(created_td)+2])
complexity = re.sub('\s*<.?td>','',lst[lst.index(complexity_td)+2])
author = re.sub('\s*<.?td>','',lst[lst.index(author_td)+2])
version = re.sub('\s*<.?td>','',lst[lst.index(version_td)+2])
if not version: # Version empty
version = 'Unknown'
return title , rating, created, complexity, author, version, body
if __name__ == '__main__':
if len(sys.argv) != 2:
print 'Usage %s tip_id' % sys.argv[0]
sys.exit(-1)
id = int(sys.argv[1])
tip = gettip(id)
parsedtip = parsetip(tip)
if parsedtip is None:
print 'No tip with id %d exists on www.vim.org' % id
sys.exit(-2)
print parsedtip[0]
print 'Rating: %s' % parsedtip[1]
print 'Created: %s' % parsedtip[2]
print 'Complexity: %s' % parsedtip[3]
print 'Author: %s' % parsedtip[4]
print 'Version of Vim required : %s' % parsedtip[5]
print parsedtip[6]