import smtplib
from time import *
import time 
import requests
import re
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import difflib
import os
#from difflib_data import *
from lxml.html.clean import Cleaner

LOGIN_URL = 'https://iu.zid.tuwien.ac.at/AuthServ.portal'
DATA_URL2 = 'https://iu.zid.tuwien.ac.at/AuthServ.authenticate?app=36'
DATA_URL = 'https://tiss.tuwien.ac.at/education/favorites.xhtml?windowId=895'
DATA_URL_FAV='https://tuwel.tuwien.ac.at/mod/page/view.php?id=276628'
#DATA_URL = 'https://tiss.tuwien.ac.at/education/course/examDateList.xhtml?windowId=a86&courseNr=101132&semester=2015W'

payload = {
    'name': 'matrikelnr',
    'pw': 'PASSWORT_HIER_REIN'
}

def sendmail(datain):
    to = '...@student.tuwien.ac.at'
    gmail_user = '...@student.tuwien.ac.at'
    gmail_pwd = 'PASSWORT_HIER_REIN'
    smtpserver = smtplib.SMTP("mail.student.tuwien.ac.at",587)
    smtpserver.ehlo()
    smtpserver.starttls()
    smtpserver.ehlo
    smtpserver.login(gmail_user, gmail_pwd)
    header = 'To:' + to + '\n' + 'From: ' + gmail_user + '\n' + 'Subject:testing \n'
    print (header)
    #msg = header + '\n this \n'
    msg = MIMEMultipart('alternative')
    msg['Subject'] = "Seitenupdate Tuwel"
    msg['From'] = gmail_user
    msg['To'] = 'you'
    part1 = MIMEText(datain, 'plain')
    part2 = MIMEText(datain, 'html')
    msg.attach(part1)
    msg.attach(part2)
    smtpserver.sendmail(gmail_user, to, msg.as_string())
    #smtpserver.sendmail(gmail_user, to, msg)
    print ('done!')
    smtpserver.close()
    return 0

with requests.Session() as s:
    cleaner = Cleaner(page_structure=False)

    s.post(LOGIN_URL, data=payload)
    s.get(DATA_URL2)
    r = s.get(DATA_URL_FAV)
    #htmlold=cleaner.clean_html(r.content)
    r = s.get(DATA_URL_FAV)
    htmlold=cleaner.clean_html(r.content)
    htmlold=re.sub('id="[^"]','',htmlold)
#    htmlold=re.sub('<ul[^>]','',htmlold)
    htmlold=re.sub('............_group','',htmlold)
#    f = open("htmlold.txt","r")
#    htmlold = f.read()
#    f.close()

    while 1:
        r=s.get(DATA_URL_FAV)
        htmlnew=cleaner.clean_html(r.content)

#changes
        htmlnew=re.sub('id="[^"]','',htmlnew)
#        htmlnew=re.sub('<ul[^>]','',htmlnew)
        htmlnew=re.sub('............_group','',htmlnew)
#

        f = open("htmlold.txt","w")
        f.write(htmlold)
        f.close()
        f = open("htmlnew.txt","w")
        f.write(htmlnew)
        f.close() 
        if htmlnew == htmlold:
            print (strftime("true: %H:%M:%S", localtime()))
        else:
            os.system("bash -c \"diff htmlold.txt htmlnew.txt\" > changes.txt")
            f = open("changes.txt","r")
            changes = f.read()
            f.close()
            while(changes.find("img src=")!=-1):
                i=changes.find("img src=")
                changes=changes[1:i]+"IMG"+changes[i+3:len(changes)]
                changes=changes[1:i+8]+"https://tiss.tuwien.ac.at"+changes[i+8:len(changes)]
            sendmail("<div>alt --- > neu</div>"+changes)
	   # print(changes)
            htmlold=htmlnew
        time.sleep(600)
#        time.sleep(3600)
