from pyDes import *
import pynotify
from time import strftime
import base64
import urllib2
import re
import os

site = urllib2.urlopen("http://thedaemon.com/aboutauthor.html")
html = site.read()
homedir=os.path.expanduser('~')

try:
	configFile = open(homedir+'/.daemon', 'r')
except:
	configFile = open(homedir+'/.daemon', 'w')
	configFile.write('dateSource=05/15/88\n')
configFile.close()

temp = strftime("%m/%d/%Y")
date = temp[0]+temp[1]+temp[2]+temp[3]+temp[4]+temp[5]+temp[8]+temp[9]
configFile = open(homedir+'/.daemon', 'r')
config = configFile.read()
configFile.close()
pattern = re.compile("dateSource=\S+")
dateSource = str(pattern.match(config).group()).split('=')[1]
#print dateSource

#config.writeline('dateRead='+date)

key = "your key"
IV = "\x12\x34\x56\x78\x90\xAB\xCD\xEF"

html = html.split("stegoimage")[1].split("/div")[0]
pattern = re.compile("<(.|\n)+?>")
Mesg = pattern.sub('',html)

messages = Mesg.split("|-END-|")
date=messages[0].split("~")[1]
print date
if (date!=dateSource):
	try:
		new = open(homedir+'/Desktop/Daemon-'+date.replace('/','')+'.txt', 'w')
		#print date
		for message in messages:
			text = message.split("|-BEGIN-|")
			dMesg = base64.decodestring(text[1])
			k = des(key, CBC, IV, pad=None, padmode=None)
			dMessage = k.decrypt(dMesg)
			new.write(dMessage)
		new.close()
	except:
		print ""
	configFile = open(homedir+'/.daemon', 'w')
	pattern = re.compile("dateSource=\S+")
	config = pattern.sub("dateSource="+date, config)
	configFile.write(config)
	configFile.close()
	pynotify.init("Daemon")
	popup = pynotify.Notification("Daemon Update", "The author has updated his encrypted blog on "+date)
	popup.set_urgency(pynotify.URGENCY_NORMAL)
	popup.set_timeout(pynotify.EXPIRES_NEVER)
	popup.show()	


