linuxTodayHeadlines.py

I wrote this as a CGI script for my local web server. It retrieves the headlines and timestamps of the Linux Today news stories and produces a summary web page.

Here is an example screenshot:

../../images/software/snippets/linuxtoday.jpg

Here is the code:

#!/usr/bin/env python

"""
$Id: linuxTodayHeadlines.py 422 2002-06-25 13:01:03Z david $

Simple script to extract the headers from Linux Today and produce a web
page containing them.
"""

import urllib, string, sys

LIST_ITEM = """\
	<LI><A HREF="%s">%s</A> - %s</LI>
"""

PAGE = """\
Content-type: text/html

<HTML>

<HEAD><TITLE>Linux Today Headlines</TITLE></HEAD>

<BODY>

<H1 ALIGN="CENTER">Linux Today Headlines</H1>

<HR WIDTH=50%%>

<UL>
%s
</UL>

<HR WIDTH=50%%>

Generated by: linuxTodayHeadlines.py<BR>
Author: David Taylor<BR>

</BODY>
</HTML>
"""

# Get the headlines
linuxToday = urllib.urlopen("http://linuxtoday.com/backend/lthead.txt")
headlines = linuxToday.read()
headlines = string.split(headlines, "&&")[1:]
# Extract and format each headline
listItems = []
for headline in headlines:
	headline = string.strip(headline)
	title, url, date = string.split(headline, "\n")
	listItems.append(LIST_ITEM % (url, title, date))
unorderedList = string.join(listItems)
sys.stdout.write(PAGE % unorderedList)