#!/usr/bin/env python

from nntplib import NNTP
from cgi import escape
from mimify import mime_decode

polstopka = '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2"> \
    <body bgcolor="#FFFFFF" text="#000000">'


# this is ugly
def genpage(group,plik):
	s = NNTP('news.tpi.pl', readermode='reader')
	resp, count, first, last, name = s.group(group)
	first = str(eval(last) - 10)
	file = open(plik +".html","w+")
	file2 = open(plik+"-index.html","w+")
	if ((group[:2] == 'pl') or (group[:6] == 'alt.pl')):
	    file.write(polstopka)
	for bu in range(eval(first),eval(last)):
		i = str(bu)
		a= s.body (i)
		n= s.xhdr ('From',i)
		m= s.xhdr ('Subject',i)
		file.write("<BR>")
		file.write('<FONT COLOR="DD0000">')
		file.write('<a name="'),
		file.write(a[1])
		file.write('"></a>')
		#kto
		kto = n[1][0][1]
		kto = kto[0:20]
		file.write(escape(kto))
		file.write('</FONT>')
		file.write("<BR>")
		file.write('<FONT COLOR="00BB00">')
		#co
		co = m[1][0][1]
		co = co[0:20]
		y = mime_decode(co)
		print y
		file.write(escape(co))
		file.write('</FONT>')
    		file.write("<BR>")
		liszta = a[3]
		for i in liszta:
		    file.write(i)
		    file.write("<BR>")
    		file.write("<BR><HR><BR>")
		
		file2.write('<a href="nntp/'+plik+'.html#')
		file2.write(a[1])
		file2.write('">')
		file2.write(escape(co))
		file2.write('</a>')
		file2.write('<br>')
	file.close()
	file2.close()
	resp = s.quit()

if (__name__=='__main__'):
    grupy = ['pl.comp.os.linux.programowanie','comp.lang.python', 
    'alt.pl.comp.os.hacking','pl.comp.os.linux']
    for i in grupy:
	print i
	genpage(i,i)
	print "Done"