import hashlib, datetime, struct, urllib, sys, time, wx

while True:
	today = datetime.date.today()
	date = today-datetime.timedelta(1) #30W-Regel
	try:
		djia = urllib.urlopen(date.strftime("http://irc.peeron.com/xkcd/map/data/%Y/%m/%d")).read()
	except IOError:
		print 'Keine Verbindung zu peeron. Warte 30 Minuten...'
		time.sleep(1800)
	else:	
		if djia.find('404 Not Found') == -1:
			sum = hashlib.md5("%s-%s" % (today, djia)).digest()
			breite = struct.unpack(">Q", sum[0:8])[0] / (2.**64)
			lange = struct.unpack(">Q", sum[8:16])[0] / (2.**64)
			minlange = 0.60707
			maxlange = 1
			minbreite = 0.54037
			maxbreite = 0.67906
			if breite > minbreite and breite < maxbreite and lange > minlange and lange < maxlange: 
				app = wx.PySimpleApp()
				app.MainLoop()
				wx.MessageBox("Das heutige Ziel (%s, %s) ist nah!" % (breite,lange),"Geohashing")
			else: print 'Das Ziel (%s, %s) ist zu weit weg.' % (breite,lange)
			print 'Koordinaten erfolgreich analysiert. Warte 12 Stunden...'
			time.sleep(43200)
		else:
			print 'peeron hat die Daten noch nicht. Warte eine Stunde...'
			time.sleep(3600)