#!/usr/bin/python
#mudzot20061230
import urllib2
import re
print "Go to vnthuquan.net, choose a story , then copy-n-paste its URL here below\n"
url=raw_input("URL :");
linkpatt=re.compile('<a href="truyen.aspx\?tid=(?P<tid>.*)&cochu=" class="normal8">(?P<chap>.*)</a> </acronym>');
truyen=urllib2.urlopen(url);
i=1;
print "Fetching ...\n";
toc=open('toc.html','w');
toc.write('<html>\n<head>\n<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">\n</head>\n<body>\n');
err=1;attempts=1;
for text in truyen:
	res=linkpatt.search(text);
	if (res!=None):
		print 'Page '+str(i);
		while (err>0) and (attempts<5):
			try:
				attempts=attempts+1;
				each=urllib2.urlopen('http://vnthuquan.net/truyen/truyentext.aspx?tid='+res.group('tid'));
				err=0;
			except urllib2.URLError:
				print str(attempts)+' attempts';
				err=1;
		err=1;attempts=1;
		rawcont=each.read();
		ff=open(str(i)+'.html','w');
		ff.write(rawcont);
		ff.close();
		toc.write('<a href="'+str(i)+'.html" target="noidung">'+res.group('chap')+'</a><br>\n');
		i=i+1;
toc.write('</body>\n</html>');
toc.close();
index=open('index.html','w');
index.write('<html>\n<head>\n<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">\n</head>\n<frameset cols="150,*" frameborder="0" noresize>\n<frame src="toc.html">\n<frame name="noidung" src="1.html">\n</frameset>\n</html>');
index.close();
 
