#!/usr/bin/env python # -*- coding: utf-8 -*- # # feed.py generate rss feed # # author: observer # email: jingchaohu@gmail.com # blog: http://obmem.com # last edit @ 2009.12.22 import sqlite3 import time import os,sys def feed(path=None,conn=None,cat1='',cat2=''): if (not path) or (not conn): path = "/var/www/simplecd.old" conn = sqlite3.connect(path+'/verycd.sqlite3.db') conn.text_factory = str c=conn.cursor() for _ in range(3): try: if not cat1: c.execute('select * from verycd order by updtime desc limit 20'); elif not cat2: c.execute('select * from verycd where category1=? order by updtime desc limit 20',(cat1,)); else: c.execute('select * from verycd where category1=? and category2=? order by updtime desc limit 20',(cat1,cat2)); break except: time.sleep(0.1) continue data = None try: data = c.fetchall() except: c.close() conn.commit() return c.close() conn.commit() pubdate = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()) feed = ''' SimpleCD - 最新电驴%s%s资源 http://www.simplecd.org zh-cn\n''' % (cat1,cat2,cat1,cat2) feed += '%s\n' % pubdate feed += '%s\n' % pubdate feed += '''http://blogs.law.harvard.edu/tech/rss SimpleCD.com jingchaohu@gmail.com (webmaster) jingchaohu@gmail.com (webmaster) 4 ''' for d in data: # data:0 1 2 3 4 5 6 7 8 9 # id ttl status brief pubtime pudtime cat1 cat2 ed2k content title = d[1] link = 'http://www.simplecd.org/id/%s' % d[0] rss = '摘要信息:'+d[3]+'
\n类别:'+d[6]+'
\n子类别:'+d[7]+'
\n' rss += d[9] feed +=''' <![CDATA[%s]]> %s %s observer '''% (title,link,rss,pubdate) feed +='''
''' return feed if __name__ == '__main__': path = os.path.dirname(os.path.realpath(sys.argv[0])) conn = sqlite3.connect(path+'/verycd.sqlite3.db') conn.text_factory = str print feed(path,conn,cat1="音乐")