from requests import *
import datetime
import glob, os

## Load local *.htm file in a list 
os.chdir("Klasse")
aktuellerplan = []
for file in sorted(glob.glob("*00*.htm")):
    with open(file,"r") as f:
        aktuellerplan.append("".join(f.readlines()))

for i in range(len(aktuellerplan)-1):
    pos = aktuellerplan[i].index("<font")-5
    aktuellerplan[i] = aktuellerplan[i][:pos]
#removes end of html (except on the last LE)
for j in range(len(aktuellerplan)-1):
    pos = aktuellerplan[j+1].index("center")+7
    aktuellerplan[j+1]=aktuellerplan[j+1][pos:]
#removes beginning of html (except the first LE)
aktuellerplan[0]=aktuellerplan[0][:aktuellerplan[0].index("</style>")+10]+aktuellerplan[0][aktuellerplan[0].index("</style>")+219:]
aktuellerplan[0]=aktuellerplan[0][:aktuellerplan[0].index("\"mon_head\"")+21]+"<p>"+aktuellerplan[0][aktuellerplan[0].index("Runzstr. 83")+17:]
aktuellerplan[0]=aktuellerplan[0][:aktuellerplan[0].index("tr.odd")+20]+" #ffffff"+aktuellerplan[0][aktuellerplan[0].index("tr.odd")+28:]
aktuellerplan[0]=aktuellerplan[0][:aktuellerplan[0].index("tr.even")+21]+" #e3e3e3"+aktuellerplan[0][aktuellerplan[0].index("tr.even")+29:]
#removal of UNTIS Text, saves space
aktuellerplan[-1]=aktuellerplan[-1][:-285]+"<p>Last upload:"+str(datetime.datetime.now().time())+"</p>"+"</body></html>"
#optional, adds last upload time (for debugging?)
weekdays=["Montag","Dienstag","Mittwoch","Donnerstag","Freitag"]
for i in range(5):
    if weekdays[i] in aktuellerplan[0]:
        day=weekdays[i]
        try:
            nextday=weekdays[i+1]
        except:
            nextday=weekdays[0]
#looks for Day in title (to determine the number of elements for th 1rst and 2nd table)
aktuellerplan[0]=aktuellerplan[0][:-8]
#removing additional content (useful?)
try:
    lenght_of_1st_day = int(aktuellerplan[0][aktuellerplan[0].index(day)+len(day)+21])
except:
    lenght_of_1st_day = 1
#tries to determine lenght of 1rst day, might crash if >10 (2 digits), couldn't try it...
for i in range(lenght_of_1st_day-1):
    aktuellerplan[i+1]=aktuellerplan[i+1][aktuellerplan[i+1].index("mon_title"):]
    aktuellerplan[i+1]=aktuellerplan[i+1][aktuellerplan[i+1].index("mon_list")+397:]
    if i>0:
      aktuellerplan[i]=aktuellerplan[i][:aktuellerplan[i].index("/table")-1]
tabletext=""
for i in range(lenght_of_1st_day):
    tabletext=tabletext+aktuellerplan[0]
    del aktuellerplan[0]
#puts the LE of 1rst day onto 1 table + puts them into 1 html
for i in range(len(aktuellerplan)-1):
    aktuellerplan[i+1]=aktuellerplan[i+1][aktuellerplan[i+1].index("mon_title"):]
    aktuellerplan[i+1]=aktuellerplan[i+1][aktuellerplan[i+1].index("mon_list")+397:]
    if i==0:
        aktuellerplan[i]=aktuellerplan[i][:-8]
    elif i>0:
        aktuellerplan[i]=aktuellerplan[i][:aktuellerplan[i].index("/table")-1]
#same for 2nd day
for x in aktuellerplan:
    tabletext=tabletext+x
try:
    tabletext=tabletext[:tabletext.index("1 / ")-7]+tabletext[tabletext.index("1 / ")+6:]
except:
    print("1 page for day1")
try:
    tabletext=tabletext[:tabletext.index("1 / ")-7]+tabletext[tabletext.index("1 / ")+6:]
except:
    print("1 page for day2")
#removes title showing (Tag1/X)
if nextday not in tabletext:
    nextday=weekdays[0]
#nextday = daynow+1, but that won't work for vacations. This fixes the issue (assuming school starts on monday afterwards)
tabletext=tabletext[:tabletext.index("tr.even")+32]+"\ntd.list{width:14%;}"+tabletext[tabletext.index("tr.even")+32:]
tabletext=tabletext[:tabletext.index("<center>")]+"<table id=\"overview\" style=\"width:100%;\"><tr><td valign=\"top\" style=\"width:50%;\">"+tabletext[tabletext.index("<center>"):]
tabletext=tabletext[:tabletext.index(nextday)-34]+"</td><td valign=\"top\" style=\"width:50%;\">"+tabletext[tabletext.index(nextday)-34:]
tabletext=tabletext[:tabletext.index("<font")]+"</td></tr></table>"+tabletext[tabletext.index("<font")-2:]
#puts table for both days into another table to be able to show them both on 1 screen
tabletext=tabletext.replace("<center>","")
tabletext=tabletext.replace("</center>","")
#removes center-tags bc they distort the layout of the table

js_func="""<script>
$(function() {
var pageScan = {
    speed : 40000,
    loop  : true,
    delayRestart : 2000,
    start : function(){
      	pageHeight = $('body').height() - window.innerHeight;
        pageScan.proc(pageHeight);
    },
    proc  : function(to){
        $("body").animate(
            {scrollTop: to},
             pageScan.speed,
             "linear",
             function(){
        	    if (pageScan.loop) {
                    setTimeout(function() {
                        window.scrollTo(0, 0);
                        pageScan.start();
                    }, pageScan.delayRestart);
                }
        });
    }
};
pageScan.start();
});
</script>"""
tabletext = tabletext[:tabletext.index("</body>")]+js_func+tabletext[tabletext.index("</body>"):]
tabletext = tabletext[:tabletext.index("</head>")]+"<script src=\"jquery.js\"></script>"+tabletext[tabletext.index("</head>"):]
#adds js function for autoscroll (up and down)
#REQUIRES JQUERY
tabletext = tabletext[:tabletext.index("<body>")+6]+tabletext[tabletext.index("Stand"):tabletext.index("Stand")+23]+tabletext[tabletext.index("Stand")+65:]
#removes some faulty div/span content (was useless anyways)
tabletext = tabletext.replace("margin: 0;","text-align: center;")
tabletext = tabletext[:tabletext.index("table.info")+13]+"	margin:auto;"+tabletext[tabletext.index("table.info")+13:]
#centers title of day
print("Conversion finished, Writing file")
with open("index.html","w") as f:
    f.write(tabletext)
#alternatively: upload to FTP?