NEWS

Automatische Abholung der Müllabholtermine von awg.de

  • Developer

    Hallo,

    hier mein Script zum automatischen abholen der Mülltermine der AWG
    Vielleicht kann es ja jemand gebrauchen.

    LG bazidibavaria

    /*
      AWG Mülltonnen Termine
      Author: bazidibavaria - 2020-03-18
      Version 0.1
    */
    
    // Ort, Straße bitte wie auf der Webseite hinterlegen
    // --CONFIG------------------------------
    var city = "";
    var street = "";
    var housenumber = "";
    // ------------------------------CONFIG--
    
    var DataUrl = "https://wastemanagement.awg.de/WasteManagementDonauwald/WasteManagementServlet";
    
    var WasteDisposalUrl = "wasteDisposal.url";
    var WasteDisposalLastRun = "wasteDisposal.lastRun";
    var WasteDisposalError = "wasteDisposal.error";
    
    var WasteNextDate = "wasteDisposal.next.date";
    var WasteNextDateFormat = "wasteDisposal.next.dateformat";
    var WasteNextDaysLeft = "wasteDisposal.next.daysleft";
    var WasteNextTypes = "wasteDisposal.next.types";
    var WasteNextTypesText = "wasteDisposal.next.typestext";
    var WasteNextWeekday = "wasteDisposal.next.weekday";
    var WasteNextWeekdayText = "wasteDisposal.next.weekdaytext";
    
    var WasteTypeRestmuellDaysLeft = "wasteDisposal.type.Restmuell.daysleft";
    var WasteTypeRestmuellNextDate = "wasteDisposal.type.Restmuell.nextdate";
    var WasteTypeRestmuellNextDateFormat = "wasteDisposal.type.Restmuell.nextdateformat";
    var WasteTypeRestmuellNextWeekday = "wasteDisposal.type.Restmuell.nextweekday";
    var WasteTypeRestmuellNextWeekdayText = "wasteDisposal.type.Restmuell.nextweekdaytext";
    var WasteTypeRestmuellImage = "wasteDisposal.type.Restmuell.image";
    
    var WasteTypeRestmuellContainerDaysLeft = "wasteDisposal.type.RestmuellContainer.daysleft";
    var WasteTypeRestmuellContainerNextDate = "wasteDisposal.type.RestmuellContainer.nextdate";
    var WasteTypeRestmuellContainerNextDateFormat = "wasteDisposal.type.RestmuellContainer.nextdateformat";
    var WasteTypeRestmuellContainerNextWeekday = "wasteDisposal.type.RestmuellContainer.nextweekday";
    var WasteTypeRestmuellContainerNextWeekdayText = "wasteDisposal.type.RestmuellContainer.nextweekdaytext";
    var WasteTypeRestmuellContainerImage = "wasteDisposal.type.RestmuellContainer.image";
    
    var WasteTypePapierDaysLeft = "wasteDisposal.type.Papier.daysleft";
    var WasteTypePapierNextDate = "wasteDisposal.type.Papier.nextdate";
    var WasteTypePapierNextDateFormat = "wasteDisposal.type.Papier.nextdateformat";
    var WasteTypePapierNextWeekday = "wasteDisposal.type.Papier.nextweekday";
    var WasteTypePapierNextWeekdayText = "wasteDisposal.type.Papier.nextweekdaytext";
    var WasteTypePapierImage = "wasteDisposal.type.Papier.image";
    
    var WasteTypePapierContainerDaysLeft = "wasteDisposal.type.PapierContainer.daysleft";
    var WasteTypePapierContainerNextDate = "wasteDisposal.type.PapierContainer.nextdate";
    var WasteTypePapierContainerNextDateFormat = "wasteDisposal.type.PapierContainer.nextdateformat";
    var WasteTypePapierContainerNextWeekday = "wasteDisposal.type.PapierContainer.nextweekday";
    var WasteTypePapierContainerNextWeekdayText = "wasteDisposal.type.PapierContainer.nextweekdaytext";
    var WasteTypePapierContainerImage = "wasteDisposal.type.PapierContainer.image";
    
    var WasteTypeBioDaysLeft = "wasteDisposal.type.Bio.daysleft";
    var WasteTypeBioNextDate = "wasteDisposal.type.Bio.nextdate";
    var WasteTypeBioNextDateFormat = "wasteDisposal.type.Bio.nextdateformat";
    var WasteTypeBioNextWeekday = "wasteDisposal.type.Bio.nextweekday";
    var WasteTypeBioNextWeekdayText = "wasteDisposal.type.Bio.nextweekdaytext";
    var WasteTypeBioImage = "wasteDisposal.type.Bio.image";
    
    var validTypes = ["Restmuell", "RestmuellContainer", "Papier", "PapierContainer", "Bio"];
    
    createState(WasteDisposalUrl, DataUrl, {name:'data url',type:"string",write:false,role:"text.url"});
    createState(WasteDisposalLastRun, {name:'last run',type:"string",write:false,role:"date.start"});
    createState(WasteDisposalError,false,{name:'error',def:false,type:"boolean",write:false,role:"indicator"});
    
    createState(WasteNextDate, {name:'next day',type:"string",write:false,role:"date.start"});
    createState(WasteNextDateFormat, "01.01.1970", {name:'next day formated',type:"string",write:false,role:"text"});
    createState(WasteNextDaysLeft, 0, {name:'days left',def:0,type:"number",min:0,max:365,unit:"days",write:false,role:"value"});
    createState(WasteNextTypes, [], {name:'next types',type:"array",write:false,role:"list"});
    createState(WasteNextTypesText, "", {name:'next types as text',type:"string",write:false,role:"text"});
    createState(WasteNextWeekday, -1 , {name:'next weekday',def:-1,type:"number",min:-1,max:6,write:false,role:"value"});
    createState(WasteNextWeekdayText, "", {name:'next weekday as text',type:"string",write:false,role:"text"});
    
    createState(WasteTypeRestmuellDaysLeft, 0, {name:'days left',def:0,type:"number",min:0,max:365,unit:"days",write:false,role:"value"});
    createState(WasteTypeRestmuellNextDate, {name:'date',type:"string",write:false,role:"date.start"});
    createState(WasteTypeRestmuellNextDateFormat, "01.01.1970", {name:'day formated',type:"string",write:false,role:"text"});
    createState(WasteTypeRestmuellNextWeekday, -1, {name:'weekday',def:-1,type:"number",min:-1,max:6,write:false,role:"value"});
    createState(WasteTypeRestmuellNextWeekdayText, "", {name:'weekday as text',type:"string",write:false,role:"text"});
    createState(WasteTypeRestmuellImage, "https://wastemanagement.awg.de/WasteManagementDonauwald/content/Restmuelltonnefreigestellt.gif", {name:'type image',type:"string",write:false,role:"text.url"});
    
    createState(WasteTypeRestmuellContainerDaysLeft, 0, {name:'days left',def:0,type:"number",min:0,max:365,unit:"days",write:false,role:"value"});
    createState(WasteTypeRestmuellContainerNextDate, {name:'date',type:"string",write:false,role:"date.start"});
    createState(WasteTypeRestmuellContainerNextDateFormat, "01.01.1970", {name:'day formated',type:"string",write:false,role:"text"});
    createState(WasteTypeRestmuellContainerNextWeekday, -1, {name:'weekday',def:-1,type:"number",min:-1,max:6,write:false,role:"value"});
    createState(WasteTypeRestmuellContainerNextWeekdayText, "", {name:'weekday as text',type:"string",write:false,role:"text"});
    createState(WasteTypeRestmuellContainerImage, "https://wastemanagement.awg.de/WasteManagementDonauwald/content/1100-Restfreigestellt.gif", {name:'type image',type:"string",write:false,role:"text.url"});
    
    createState(WasteTypePapierDaysLeft, 0, {name:'days left',def:0,type:"number",min:0,max:365,unit:"days",write:false,role:"value"});
    createState(WasteTypePapierNextDate, {name:'date',type:"string",write:false,role:"date.start"});
    createState(WasteTypePapierNextDateFormat, "01.01.1970", {name:'day formated',type:"string",write:false,role:"text"});
    createState(WasteTypePapierNextWeekday, -1, {name:'weekday',def:-1,type:"number",min:-1,max:6,write:false,role:"value"});
    createState(WasteTypePapierNextWeekdayText, "", {name:'weekday as text',type:"string",write:false,role:"text"});
    createState(WasteTypePapierImage, "https://wastemanagement.awg.de/WasteManagementDonauwald/content/papiertonnefreigestellt.gif", {name:'type image',type:"string",write:false,role:"text.url"});
    
    createState(WasteTypePapierContainerDaysLeft, 0, {name:'days left',def:0,type:"number",min:0,max:365,unit:"days",write:false,role:"value"});
    createState(WasteTypePapierContainerNextDate, {name:'date',type:"string",write:false,role:"date.start"});
    createState(WasteTypePapierContainerNextDateFormat, "01.01.1970", {name:'day formated',type:"string",write:false,role:"text"});
    createState(WasteTypePapierContainerNextWeekday, -1, {name:'weekday',def:-1,type:"number",min:-1,max:6,write:false,role:"value"});
    createState(WasteTypePapierContainerNextWeekdayText, "", {name:'weekday as text',type:"string",write:false,role:"text"});
    createState(WasteTypePapierContainerImage, "https://wastemanagement.awg.de/WasteManagementDonauwald/content/1001-Papierfreigestellt.gif", {name:'type image',type:"string",write:false,role:"text.url"});
    
    createState(WasteTypeBioDaysLeft, 0, {name:'days left',def:0,type:"number",min:0,max:365,unit:"days",write:false,role:"value"});
    createState(WasteTypeBioNextDate, {name:'date',type:"string",write:false,role:"date.start"});
    createState(WasteTypeBioNextDateFormat, "01.01.1970", {name:'day formated',type:"string",write:false,role:"text"});
    createState(WasteTypeBioNextWeekday, -1, {name:'weekday',def:-1,type:"number",min:-1,max:6,write:false,role:"value"});
    createState(WasteTypeBioNextWeekdayText, "", {name:'weekday as text',type:"string",write:false,role:"text"});
    createState(WasteTypeBioImage, "https://wastemanagement.awg.de/WasteManagementDonauwald/content/biotonnefreigestellt.gif", {name:'type image',type:"string",write:false,role:"text.url"});
    
    // Schedule für die Updates
    schedule("0 16 * * *",  Muellabfuhr);
    
    // Skript ausführen
    Muellabfuhr();
    
    function Muellabfuhr() {
        setState(WasteDisposalUrl, DataUrl);
        
        request(DataUrl, function (error, response, body) {
            if (!error && response.statusCode == 200) {
                var ApplicationName = "";
                var findApplicationName = /ID="ApplicationName" VALUE="([^"]+)"/i;
                var matchApplicationName = findApplicationName.exec(body);
                if(matchApplicationName != null && matchApplicationName.length == 2){
                    ApplicationName = matchApplicationName[1];
                }
    
                var SessionId = "";
                var findSessionId = /ID="SessionId" VALUE="([^"]+)"/i;
                var matchSessionId = findSessionId.exec(body);
                if(matchSessionId != null && matchSessionId.length == 2){
                    SessionId = matchSessionId[1];
                }
                
                var Build = "";
                var findBuild = /ID="Build" VALUE="([^"]+)"/i;
                var matchBuild = findBuild.exec(body);
                if(matchBuild != null && matchBuild.length == 2){
                    Build = matchBuild[1];
                }
                
                var PageXMLVers = "";
                var findPageXMLVers = /ID="PageXMLVers" VALUE="([^"]+)"/i;
                var matchPageXMLVers = findPageXMLVers.exec(body);
                if(matchPageXMLVers != null && matchPageXMLVers.length == 2){
                    PageXMLVers = matchBuild[1];
                }
                
                var PageName = "";
                var findPageName = /ID="PageName" VALUE="([^"]+)"/i;
                var matchPageName = findPageName.exec(body);
                if(matchPageName != null && matchPageName.length == 2){
                    PageName = matchBuild[1];
                }
    
                if(ApplicationName.length > 0 && SessionId.length > 0 && Build.length > 0 && PageXMLVers.length > 0 && PageName.length > 0){
                    var formData = {
                        SubmitAction: "changedEvent"
                        , ApplicationName: ApplicationName
                        , SessionId: SessionId
                        , Method: "GET"
                        , VerticalOffset: "0"
                        , Build: Build
                        , PageXMLVers: PageXMLVers
                        , InFrameMode: "FALSE"
                        , ID: ""
                        , Focus: "Ort"
                        , ModulName: ""
                        , PageName: PageName
                        , NextPageName: ""
                        , NewTab: "default"
                        , IsLastPage: "false"
                        , IsSubmitPage: "false"
                        , Ajax: "FALSE"
                        , ShowMenue: "false"
                        , Ort: city
                        , Strasse: street
                        , Hausnummer: ""
                        , Hausnummerzusatz: ""
                    }
    
                    request.post({
                        url:DataUrl
                        ,form:formData
                    }, function(error, response, body) {
                        if (!error && response.statusCode == 200) {
                            var formData = {
                                SubmitAction: "forward"
                                , ApplicationName: ApplicationName
                                , SessionId: SessionId
                                , Method: "POST"
                                , VerticalOffset: "0"
                                , Build: Build
                                , PageXMLVers: PageXMLVers
                                , InFrameMode: "FALSE"
                                , ID: ""
                                , Focus: "Hausnummer"
                                , ModulName: ""
                                , PageName: PageName
                                , NextPageName: ""
                                , NewTab: "default"
                                , IsLastPage: "false"
                                , IsSubmitPage: "false"
                                , Ajax: "FALSE"
                                , ShowMenue: "false"
                                , Ort: city
                                , Strasse: street
                                , Hausnummer: housenumber
                                , Hausnummerzusatz: ""
                            };
                            request.post({
                                url:DataUrl
                                ,form:formData
                            }, function(error, response, body) {
                                if (!error && response.statusCode == 200) {
                                    var terminTables = [];
                                    var terminDates = [];
                                    var terminTypes = [];
                                    var dateNow = new Date();
                                    dateNow.setHours(0,0,0,0);
                                    //dateNow.setDate(dateNow.getDate() + 2);
                                    var dayNames = ['Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'];
    
                                    var findTables = /<table[^>]+>/ig;
                                    var matchTables = body.match(findTables);
    
                                    matchTables.forEach(table => {
                                        var findTerminTable = /<table NAME="[A-Za-z]+TermineLIST"[^>]+>/ig;
                                        var matchTerminTable = table.match(findTerminTable);
                                        if(matchTerminTable != null && matchTerminTable.length>0){
                                            var startTableStart = body.indexOf(table);
                                            var startTableEnd = startTableStart + table.length;
                                            
                                            var endNode = "</TABLE>";
                                            var endTableStart = body.indexOf(endNode,startTableStart);
                                            var endTableEnd = endTableStart + endNode.length;
    
                                            var compTable = body.substring(startTableStart,endTableEnd);
                                            if(!terminTables.includes(compTable)){
                                                terminTables.push(compTable);
                                            }
                                        }
                                    });
    
                                    terminTables.forEach(table => {
                                        var findName = /<table name="([A-Za-z]+)TermineLIST"/ig;
                                        var matchName = findName.exec(table);
                                        var name = "";
                                        if(matchName.length == 2){
                                            name = matchName[1];
    
                                            var findEmptyTable = /<table NAME="[A-Za-z]+TermineLIST"[^>]+>[^/]\/table>/ig;
                                            var matchEmptyTable = findEmptyTable.exec(table);    
                                            if(matchEmptyTable != null){
                                                setState(eval("WasteType" + name + "DaysLeft"),0);
                                                setState(eval("WasteType" + name + "NextDate"),"");
                                                setState(eval("WasteType" + name + "NextDateFormat"),"");
                                                setState(eval("WasteType" + name + "NextWeekday"),0);
                                                setState(eval("WasteType" + name + "NextWeekdayText"),"");
                                            } else {
                                                var findTermine = /"dia_c_abfuhrdatum[^>]+>([0-9]{2}\.[0-9]{2}\.[0-9]{4})/ig;
                                                var matchTermine = findTermine.exec(table);
                                                //var matchTermine = table.match(findTermine);
                                                //if(matchTermine.length > 0){
                                                if(matchTermine.length == 2){
                                                    var termin = matchTermine[1];
                                                    var dateParts = termin.split('.');
                                                    var date = new Date(dateParts[2], dateParts[1]-1, dateParts[0]); 
                                                    date.setHours(0,0,0,0);
                                                    var dayText = "";
    
                                                    if(date.getTime() == dateNow.getTime()){
                                                        table = table.replace(matchTermine[0], "");
                                                        findTermine = /"dia_c_abfuhrdatum[^>]+>([0-9]{2}\.[0-9]{2}\.[0-9]{4})/ig;
                                                        matchTermine = findTermine.exec(table);
                                                        //var matchTermine = table.match(findTermine);
                                                        //if(matchTermine.length > 0){
                                                        if(matchTermine.length == 2){
                                                            termin = matchTermine[1];
                                                            dateParts = termin.split('.');
                                                            date = new Date(dateParts[2], dateParts[1]-1, dateParts[0]); 
                                                        } else {
                                                            setState(eval("WasteType" + name + "DaysLeft"),0);
                                                            setState(eval("WasteType" + name + "NextDate"),"");
                                                            setState(eval("WasteType" + name + "NextDateFormat"),"");
                                                            setState(eval("WasteType" + name + "NextWeekday"),0);
                                                            setState(eval("WasteType" + name + "NextWeekdayText"),"");
                                                        }
                                                    }
    
                                                    if(terminDates.indexOf(date)<0){
                                                        terminDates.push(date);
                                                        terminTypes.push(name);
                                                    }
                                                    setState(eval("WasteType" + name + "DaysLeft"),Math.round((date-dateNow)/(1000*60*60*24)));
                                                    setState(eval("WasteType" + name + "NextDate"),date.toUTCString());
                                                    setState(eval("WasteType" + name + "NextDateFormat"),termin);
                                                    setState(eval("WasteType" + name + "NextWeekday"),date.getDay());
                                                    setState(eval("WasteType" + name + "NextWeekdayText"),dayNames[date.getDay()]);
                                                } else {
                                                    setState(eval("WasteType" + name + "DaysLeft"),0);
                                                    setState(eval("WasteType" + name + "NextDate"),"");
                                                    setState(eval("WasteType" + name + "NextDateFormat"),"");
                                                    setState(eval("WasteType" + name + "NextWeekday"),0);
                                                    setState(eval("WasteType" + name + "NextWeekdayText"),"");
                                                }
                                            }
                                        }
                                    });
    
                                    var nextDate = new Date(Math.min.apply(null,terminDates));
    
                                    var nextDateDay=nextDate.getDate();
                                    var nextDateMonth=nextDate.getMonth();
                                    var nextDateMonth=nextDateMonth+1;
                                    if((String(nextDateDay)).length==1) nextDateDay='0'+nextDateDay;
                                    if((String(nextDateMonth)).length==1) nextDateMonth='0'+nextDateMonth;
                                    var nextDateFormat=nextDateDay + '.' + nextDateMonth + '.' + nextDate.getFullYear();
    
                                    var nextTypes = [];
                                    var nextTypesText = "";
    
                                    terminTypes.forEach(type => {
                                        var dateVal = getState(eval("WasteType" + type + "NextDate")).val;
                                        var checkDate = new Date(dateVal);
                                        var addType = (checkDate.getTime() == nextDate.getTime());
                                        if(addType){
                                            nextTypes.push(type)
                                        }
                                    });
    
                                    nextTypes.forEach(nextType => {
                                        nextTypesText += (nextTypesText.length>0)?" und ":"";
                                        nextTypesText += nextType;
                                    });
    
                                    setState(WasteNextDate, nextDate.toUTCString());
                                    setState(WasteNextDateFormat, nextDateFormat);
                                    setState(WasteNextDaysLeft, Math.round((nextDate-dateNow)/(1000*60*60*24)));
                                    setState(WasteNextTypes, nextTypes);
                                    setState(WasteNextTypesText, nextTypesText);
                                    setState(WasteNextWeekday, nextDate.getDay());
                                    setState(WasteNextWeekdayText, dayNames[nextDate.getDay()]);
    
                                    setState(WasteDisposalLastRun, new Date().toUTCString());
                                    setState(WasteDisposalError,false);
    
                                } else {  
                                    log(error);
                                    doError();
                                }
                            });
                        } else {  
                            log(error);
                            doError();
                        }
                    });
                }
            } else {
                log(error);
                doError();
            }
        });
    
        function doError(){
            log('Mülltermine können nicht abgeholt werden!');
            
            setState(WasteDisposalError,true);
    
            setState(WasteNextDate, "");
            setState(WasteNextDateFormat, "");
            setState(WasteNextDaysLeft, 0);
            setState(WasteNextTypes, [])
            setState(WasteNextTypesText, "")
            setState(WasteNextWeekday, -1);
            setState(WasteNextWeekdayText, "");
    
            validTypes.forEach(name => {
                setState(eval("WasteType" + name + "DaysLeft"),0);
                setState(eval("WasteType" + name + "NextDate"),"");
                setState(eval("WasteType" + name + "NextDateFormat"),"");
                setState(eval("WasteType" + name + "NextWeekday"),-1);
                setState(eval("WasteType" + name + "NextWeekdayText"),"");
            });
        }
    }
    

Suggested Topics

2.0k
Online

34.9k
Users

40.9k
Topics

561.6k
Posts