def _getMailContent(itemlist):
    # Get template
    mainFile = open(os.path.join(getAbsoluteDir(), "report.html"), 'r')
    mainTemplate = unicode(mainFile.read(), 'utf-8')
    threadFile = open(os.path.join(getAbsoluteDir(), "accordiongroup.html"), 'r')
    threadTemplate = unicode(threadFile.read(), 'utf-8')
    mainFile.close()
    threadFile.close()

    accordionStr = ""
    for item in itemlist:
        threadRawContent = getThreadContent(item)
        accordion = threadTemplate % (item.link, item.title ,threadRawContent)
        accordionStr += accordion + "<hr>"
    return mainTemplate % accordionStr
def sendMail(resultDict):
    # mail account info
    mail_username='******' # e.g. [email protected]
    mail_password='******' #gmail password
    from_addr = '*****@*****.**'
    to_addrs= []

    # HOST & PORT
    HOST = 'smtp.gmail.com'
    PORT = 587

    message = MIMEMultipart('alternative')
    message['Subject'] = u'来自地里的战报'
    message['From'] = from_addr

    # get maillist
    mailFile = open(os.path.join(getAbsoluteDir(), "emailList.json"), 'r')
    emailDict = json.loads(mailFile.read())
    mailFile.close()

    for major, itemlist in resultDict.iteritems():
        mailStr = emailDict[major]
        if len(mailStr) == 0:
            continue
        to_addrs = mailStr.split(' ')
        
        html = _getMailContent(itemlist)
        mimetext = MIMEText(html.encode('utf-8'), 'html', 'utf-8')
        message.attach(mimetext)
        msgstr = message.as_string()

        session = smtplib.SMTP(HOST, PORT)
        session.ehlo()
        session.starttls()
        session.ehlo()
        session.login(mail_username, mail_password)
        session.sendmail(from_addr, to_addrs, msgstr)
        sleep(5)
        session.quit()
def _getResultDictFromRawContent(content):
	'''
	Form raw content into result dict
	@param content: raw content of html, 
    @return resultDict{"major": itemlist} 
    '''
	absoluteDir = getAbsoluteDir()
	# Get thread counter from cPickle
	tfilePath = os.path.join(absoluteDir, "threadCounter.pkl")
	threadFile = open(tfilePath, "r")
	threadCounter = cPickle.load(threadFile)
	soup = BeautifulSoup(content)
	# find tbodylist <tag>
	tbodylist = soup.find(summary="forum_82").find_all(id = re.compile("normalthread"))
	# filter the newest tbody
	newTbodyList = [tbody for tbody in tbodylist if int(tbody["id"].split("_")[1]) > threadCounter]
	threadnumberList = [int(tbody["id"].split("_")[1]) for tbody in newTbodyList]
	# no new thread
	if len(threadnumberList) == 0:
		return None
	latestThread = max(threadnumberList)
	# Update latest thead
	threadFile = open(tfilePath, "w")
	cPickle.dump(latestThread, threadFile)
	threadFile.close()

	resultDict = dict()
	for tbody in newTbodyList:
		a = tbody.find_all("a")[2]
		item = Item()
		item.title = a.string
		item.link = a["href"]
		major = tbody.find(color="#F60").string
		if(resultDict.has_key(major)):
			resultDict[major].append(item)
		else:
			resultDict[major] = [item]
	return resultDict