# Coded By DONNO (Email: darkdonno@gmail.com)
import urllib, os,re
import xbmc,xbmcgui

StuffFolder = "q:\\skin\\MC360\\extras\\"
SkinFolder = "q:\\skin\\MC360\\PAL\\"
SkinMediaFolder = "q:\\skin\\MC360\\media\\"

def ping(host):
    import socket,time
    s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
    t1=float(0)
    t2=float(0)
    t1 = float(time.time()*1000)
    try:
        s.connect(( host, 80 ))
        t2 = float(time.time()*1000)
        s.close()
        del s
        return int((t2 - t1))
    except socket.error, (errcode, errmsg):
        t2 = time.time()
        if errcode == 111:# connection refused; means that the machine is up
            return int((t2 - t1))
        else:# something else; means that the machine is not up
            return None
def unikeyboard(default,header=""):
    kb = xbmc.Keyboard(default,header)
    kb.doModal()
    if (kb.isConfirmed()):
        return kb.getText()
    else:
        return default
def onlineChecker():
    pingdns=ping("www.xbox.com")
    pingip=ping("212.73.231.111")
    if not(pingdns and pingip):
        xbmcgui.Dialog().ok('GamerCard','Detected that DNS is most likey','not working correctly','Please set and try again')
        return 0
    else:
        return 1

def xebi(td):
    xbmc.executebuiltin(td)
def SetSettings(gamername,rep,score,zone):
    xebi('XBMC.Skin.SetString(GamerName,' +gamername+ ')')
    xebi('XBMC.Skin.SetString(GamerScore,' +score+ ')')
    xebi('XBMC.Skin.SetString(GamerRep,' +rep+ ')')
    xebi('XBMC.Skin.SetString(GamerZone,' +zone+ ')')
    xebi('XBMC.Skin.SetString(GamerIcon,' +SkinMediaFolder+"myusericon.jpg"+')')
  
def DownloadImage(url,destfold,dest=None):
    """ From the KML Browser :P  """
    rawfilename = destfold + dest
    filename = os.path.abspath(rawfilename)
    try:
        urllib.urlretrieve(url, filename)
        return rawfilename
    except:
        return None

def GetGTNFO(GamerNamer):
    card_html = ""
    GamerNamer = GamerNamer.replace(" ","%20")
    card_url = "http://gamercard.xbox.com/" + GamerNamer + ".card"
    xebi('XBMC.Notification(Downloading,From Xbox.com)')
    card_html = urllib.urlopen(str(card_url)).readlines()
    card_data = card_html[5]
    nfo = re.compile('</head><body><div class="XbcgcContainer"><div class="Xbcgc"><h3 class=".*"><a href="http://live.xbox.com/member/.*"><span class="XbcFLAL">(.*)</span></a></h3><div class="XbcgcInfo"><a href="http://live.xbox.com/member/.*"><img class="XbcgcGamertile" height="64" width="64" src="(.*)" /></a><div class="XbcgcStats"><p><span class="XbcFLAL">Rep</span><span class="XbcFRAR"><img src="/xweb/lib/images/gc_repstars_external_(.*).gif" /></span></p><p><span class="XbcFLAL"><img alt="Gamerscore" src=".*" /></span><span class="XbcFRAR">(.*)</span></p><p><span class="XbcFLAL">Zone</span><span class="XbcFRAR">(.*)</span>.*', re.IGNORECASE).findall(card_data)[0]
    return nfo

#########
## MAIN #
#########
def main():
    kb = xbmc.Keyboard(xbmc.getInfoLabel('skin.string(gamername)'),"Enter Xbox.com Gamertag")
    kb.doModal()
    if (kb.isConfirmed()):
        gamername =  kb.getText()
        if gamername == "":
            xbmcgui.Dialog().ok('GamerCard',"Blank Name given","Settings remain unchanged")
        else:
            online = onlineChecker()
            if online == 1:
                nfo = GetGTNFO(gamername)
                SetSettings(nfo[0],"gamercard_rep_"+nfo[2]+".png",nfo[3],nfo[4])
                DownloadImage(nfo[1],SkinMediaFolder,"myusericon.jpg")
                xebi("XBMC.ReloadSkin")
            else: 
                if not xbmcgui.Dialog().yesno('GamerCard',"No Internet Found","Would you like to keep old info"):
                        SetSettings(gamername,"gamercard_rep_12.png","-","-")
                        xebi("XBMC.ReloadSkin")
if __name__ == "__main__":
    main()
