#!/usr/bin/python

#
#	Blog information, for the Blogging Ecosystem
#
#		Copyright (C) 2002 Phillip Pearson
#
#	http://www.myelin.co.nz/ecosystem/
#

#       Permission is hereby granted, free of charge, to any person
#       obtaining a copy of this software and associated documentation
#       files (the "Software"), to deal in the Software without
#       restriction, including without limitation the rights to use,
#       copy, modify, merge, publish, distribute, sublicense, and/or
#       sell copies of the Software, and to permit persons to whom the
#       Software is furnished to do so, subject to the following
#       conditions:

#       The above copyright notice and this permission notice shall be
#       included in all copies or substantial portions of the
#       Software.

#       THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
#       KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
#       WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
#       PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
#       COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#       LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
#       OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
#       SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


class Blog:
	"Some information about a blog"
	def __init__( self, url, name ):
		self.url = url
		self.name = name
		self.backLinks = {}
		self.fwdLinks = {}
	def __repr__( self ):
		return "<Blog: '%s' at %s, fwd: %d, back: %d>" % ( self.name, self.url, len( self.fwdLinks ), len( self.backLinks ) )

def tidyLink( url ):
	import re
	"Tidy up a link so it can be fetched quicker - put '/' on the end, etc"
	# Get rid of a '#' at the end
	url = re.sub( r'#.*', '', url )
	# Put in a '/' if one is missing and convert the server part to lower case
	m = re.search( r'(.*?)://([^/]*)(.*)', url )
	if not m: print "link isn't a link?",url
	proto, server, path = m.groups()
	if path == '': path = '/'
	url = "%s://%s%s" % ( proto.lower(), server.lower(), path )
	#
	return url

def genericLink( url ):
	"Turn a link into a hashable 'generic' version"
	import re
	url = re.sub( r'http://www\.', r'http://', tidyLink( url ) )
	if url[-1] == '/': url = url[:-1]
	return url
