# mamehof.py
#
# Authors:
#     Stephen Taylor (http://bacoheist.com)
#     Michael Alexander (http://beefsack.com)
# Website:
#     http://beefsack.com/project/view/p/mamehof
# Date:
#     3 Jun 2009
#
# mamehof scrapes the http://maws.mameworld.info/maws MAME Hall of Fame
# and outputs it as an ini file that can be placed in the MameUI
# (http://mameui.classicgaming.gamespy.com/) "folders" folder.  After
# placing the ini file in the folder, a new custom folder will appear
# showing the first 15 pages of the hall of fame.

import urllib2

def extractstring(originalstring, stringstart, stringend, intialstart):
    try:
        extracttxt = originalstring.split(intialstart)[1]
        extracttxt2 = extracttxt.split(stringstart)[1].split(stringend)[0]
        return extracttxt2
    except Exception,error:
        return ''
    
def GetHOF():
    text_file = open('MAMEHOF.ini', "w")
    text_file.write("[FOLDER_SETTINGS]\nRootFolderIcon golden\nSubFolderIcon golden\n\n[ROOT_FOLDER]\n")
    for page in range(1, 16):
        error = 0
        theurl = "http://maws.mameworld.info/maws/srch.php?page=" + str(page) + "&by=rating&order=desc&mode=hof&has_rank=yes"
        try:
            req = urllib2.Request(theurl)
            req.add_header('User-agent', 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')
            handle = urllib2.urlopen(req)
            output = handle.read()
        except Exception,error:
            print "Error fetching HOF page " + theurl
            break
        StartStr = "<!-- MAWS results: start item -->"
        GameName = extractstring(output, '<a href="romset/', '">', StartStr)
        while GameName != '':
            print "Extracted " + GameName;
            text_file.write(GameName + "\n")
            StartStr = '<a href="romset/' + GameName + '">'
            GameName = extractstring(output, '<a href="romset/', '">', StartStr)
    text_file.close()

if __name__ == "__main__":
    GetHOF()
