#!/usr/bin/python
from BeautifulSoup import BeautifulSoup
import os
import re
import sys
import pycurl

class Fok:
	def __init__(self):
		self.titlescan = re.compile('.*<title>(.*)</title>.*')
		self.contents = ''
		self.title = ''
		self.replies = []
	
	def body_callback(self, buf):
		self.contents = self.contents + buf

	def fetch(self, topic):
		c = pycurl.Curl()
		c.setopt(c.COOKIEFILE, mycookie)
		c.setopt(c.COOKIEJAR, mycookie)
		c.setopt(c.URL, 'http://forum.fok.nl/topic/'+topic)
		c.setopt(c.WRITEFUNCTION, self.body_callback)
		c.perform()
		c.close()
	
	def title(self):
		result = self.titlescan.search(f.contents)
		try:
			self.title = result.group(1)
		except:
			pass




mycookie = '/tmp/fok/fok.cookie'

print 'Content-type: text/plain'
print ''

topicscan = re.compile('([0-9]+).xml')

try:
	result = topicscan.search(os.environ['QUERY_STRING'])

	try:
		topic = result.group(1)

		f = Fok()
		f.fetch(topic)
		f.title()
		print f.title


#		titlescan = re.compile('.*<title>(.*)</title>.*')

#		result = titlescan.search(f.contents)

#		try:
#			f.title = result.group(1)
#			print f.title
#		except:
#			raise

	except:
		raise
except:
	raise

#BeautifulSoup(''.join(lines), smartQuotesTo="xml")

print 'hoi'

sys.exit(1)

