#! /usr/bin/python

#
# lj_archiver.py
#         (c) 2006 Tejas Dinkar
#

#
# Insert GPL here
#
# Also, if you are re distributing this, please leave my url as the default
#

import urllib
url = 'http://tejasd.livejournal.com'
start_month = 8
start_year = 2005

# next two are optional to be set
end_month = None
end_year = None

class blog:
	'''This Class Helps Store Blog Results'''
	def __init__(self, title, url, datestamp):
		self.title = title
		self.url = url
		self.datestamp = datestamp
		self.year = int(datestamp[:4])
		self.month = int(datestamp[4:6])
		self.day = int(datestamp[6:])
	
	def __repr__(self):
		'''Just Eye Candy'''
		return '<a href="%s" datestamp="%s" date="%d/%02d/%02d">%s</a>' % \
	(self.url, self.datestamp, self.year, self.month, self.day, self.title)

def get_blogs(start_month, start_year, end_month = None, end_year = None):
	'''Return all Blogs Between two months, inclusive'''
	if end_month == None:
		import datetime
		today = datetime.datetime.today() + datetime.timedelta(days = 32)
		end_month = today.month
		end_year = today.year
		
	results = []
	
	year = start_year
	month = start_month

	exit = 0
	
	while year <= end_year:
		if exit == 1:
			break
		
		while month < 13:
			results += get_month(month, year)
			
			month = month + 1
			if month == end_month and year == end_year:
				exit = 1
				break
		
		month = 1
		year = year + 1
		if month == end_month and year == end_year:
			exit = 1
			break
	return results
	
	
def get_month(month, year):
	'''Gets all entries from a particular month'''
	file = urllib.urlopen(url + '/%04d/%02d/' % (year, month))
	results = []
	for line in file:
		line = line.strip()
		if "<dt>" in line:
			date = line.split(url)[-1].split('/')[3]
			datestamp = "%04d%02d%s" % (year, month, date)
		if "<dd>" in line:
			link = line.split('href="')[1].split('">')[0]
			title = line.split('">')[-1].split('</a>')[0]
			results.append(blog(title, link, datestamp))
	return results
			
if __name__ == '__main__':
	list = get_blogs(start_month, start_year, end_month, end_year)
	list.reverse()
	for i in list:
		print i

