########################################################################
#       KazoSQL - A Kazorum (DAJ_Glass-themed) to MySQL-converter      #
#            Copyright (C) 2007 Nindra (nindrag a gmail.com)           #
#                                                                      #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or    #
# (at your option) any later version.                                  #
#                                                                      #
# This program is distributed in the hope that it will be useful,      #
# but WITHOUT ANY WARRANTY; without even the implied warranty of       #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        #
# GNU General Public License for more details.                         #
#                                                                      #
# You should have received a copy of the GNU General Public License    #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.#
########################################################################

from BeautifulSoup import BeautifulSoup
import elementtree.ElementTree as ET
import sys
import md5
import re
import sets
import MySQLdb

db=MySQLdb.connect(host="127.0.0.1",passwd="pw",user="user",db="kazosql")
x = 1
dbc = db.cursor()

def uniquer(seq, idfun=None):
    if idfun is None:
	def idfun(x): return x
    seen = {}
    result = []
    for item in seq:
	marker = idfun(item)
	# in old Python versions:
	# if seen.has_key(marker)
	# but in new ones:
	if marker in seen: continue
	seen[marker] = 1	
	result.append(item)
    return result

def StripTags(text):
    finished = 0
    while not finished:
	finished = 1
	# check if there is an open tag left
	start = text.find("<")
	if start >= 0:
	    # if there is, check if the tag gets closed
	    stop = text[start:].find(">")
	    if stop >= 0:
		# if it does, strip it, and continue loop
		text = text[:start] + text[start+stop+1:]
		finished = 0
    return text

def CreateMember(name,msn,yahoo,aim,joindate,postcount,location,homepage,occu,interests):
    global dbc
    #md5hash = md5.new(post)
    name = MySQLdb.escape_string(name)
    msn = MySQLdb.escape_string(msn)
    yahoo = MySQLdb.escape_string(yahoo)
    aim = MySQLdb.escape_string(aim)
    joindate = MySQLdb.escape_string(joindate)
    postcount = MySQLdb.escape_string(postcount)
    location = MySQLdb.escape_string(location)
    homepage = MySQLdb.escape_string(homepage)
    occu = MySQLdb.escape_string(occu)
    interests = MySQLdb.escape_string(interests)
    sql = "INSERT INTO members (name,joined,msn,yahoo,aim,postcount,location,website,occupation,interests) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')" % (name,joindate,msn,yahoo,aim,postcount,location,homepage,occu,interests)
    print sql
    #    try:
    dbc.execute(sql)
    #except:
#	pass
    
def postextract(soup):
    #print "foo"
    global x
    x = x+1
    soup = BeautifulSoup(soup)
    temp = soup.body.fetchNext(name='span', attrs={'class' : 'mainmenu'})
    name = temp[2].next[8:len(temp[2].next)]
    contact = soup.body.fetchNext(name='tr',attrs={'valign' : 'middle'},limit=5)
    try:
	msn = contact[2].span.next.next.next.next.next 
    except:
	return
    yim = contact[3].span.next.next.next.next.next
    try: 
	yahoo = yim[59:yim.index('&amp')]
    except:
	yahoo = "";
    aimtemp = str(contact[4].next.next.next.next.next.next.next)
    
    try:
	aim = aimtemp[29:aimtemp.index('&amp')]
    except:
	aim = "";
	
    others = soup.body.fetchNext(name='td', attrs={'align' : 'right', 'nowrap' : 'nowrap'})
    joindate = others[8].next.next.next.next.next.next.next;
    postcount = others[9].next.next.next.next.next.next.next
    location = others[10].next.next.next.next.next.next.next
    homepage = others[11].next.next.next.next.next.next.next.next
    occu = others[12].next.next.next.next.next.next.next
    interests = others[13].next.next.next.next.next.next.next

    CreateMember(str(name),
    str(msn),
    str(yahoo),
    str(aim),
    str(joindate),
    str(postcount),
    str(location),
    str(homepage),
    str(occu),
    str(interests))

while(x < len(sys.argv)):
    print x,"/",len(sys.argv)-1
    print(sys.argv[x])
    f = file(sys.argv[x], 'r')
    postextract(f)
    f.close()