SchoolUtils/WebUntis

Getting Room of timetable entry

niklasarnitz opened this issue · 8 comments

I want to get the room number of this 'entry' in the for loop. When I query it, I only get '[object Object]'.

const time = parseInt('' + date.getHours() + date.getMinutes())

if (time >= 745 && time <= 915) {
	entries = timetable.filter(item=>item.startTime=="745")
	for (var i = entries.length - 1; i >= 0; i--) {
	        str = str + entries[i].sg + '\n'
	}
       console.log(str)
}

I am not sure why you use the sg property. You can find the rooms inside of the ro array. Sg is the student`s period group. Not a room.

const timetable = [...];

const time = parseInt(`${date.getHours()}${date.getMinutes()}`);

if (time >= 745 && time <= 915) { // Why are you doing this. You never check for the time again. Maybe you just left some context out. However, this makes it hard to understand what you actually try to accomplish.
	const entries = timetable.filter(item=> item.startTime === 745); // starTime is not a string
	let roomName;
	for (const entry of entries) {
		for (const { longname } of entry.ro) {
			roomName = longname;
		}
	}
	console.log(roomName); // Should print the last room in the room array.
	// Just a note: A lesson could have multiple rooms. 
}

If this is not what you want, you need to provide more context.

const WebUntis = require('webuntis');

const untis = new WebUntis(
	'WHG+Durmersheim',
	'Abi2020',
	'Hund2020',
	'borys.webuntis.com'
);

untis
	.login()
	.then(() => {
		return untis.getOwnTimetableForToday();
	})
	.then(timetable => {
		const date = new Date();
		const time = parseInt('' + date.getHours() + date.getMinutes());

		if (time >= 745 && time <= 915) {
			entries = timetable.filter(item => item.startTime == '745');
			for (var i = entries.length - 1; i >= 0; i--) {
				if (entries[i].sg == undefined) {
					console.log('undefined\n');
				} else {
					console.log(
						entries[i].startTime +
							' - ' +
							entries[i].sg +
							' in ' +
							entries[i].ro[0].longname
					);
				}
			}
		} else if (time >= 916 && time <= 1105) {
			entries = timetable.filter(item => item.startTime == '935');
			for (var i = entries.length - 1; i >= 0; i--) {
				if (entries[i].sg == undefined) {
					console.log('undefined\n');
				} else {
					console.log(
						entries[i].startTime +
							' - ' +
							entries[i].sg +
							' in ' +
							entries[i].ro[0].longname
					);
				}
			}
		} else if (time >= 1106 && time <= 1255) {
			entries = timetable.filter(item => item.startTime == '1125');
			for (var i = entries.length - 1; i >= 0; i--) {
				if (entries[i].sg == undefined) {
					console.log('undefined\n');
				} else {
					console.log(
						entries[i].startTime +
							' - ' +
							entries[i].sg +
							' in ' +
							entries[i].ro[0].longname
					);
				}
			}
		} else if (time >= 1256 && time <= 1555) {
			entries = timetable.filter(item => item.startTime == '1425');
			for (var i = entries.length - 1; i >= 0; i--) {
				if (entries[i].sg == undefined) {
					console.log('undefined\n');
				} else {
					console.log(
						entries[i].startTime +
							' - ' +
							entries[i].sg +
							' in ' +
							entries[i].ro[0].longname
					);
				}
			}
		} else if (time >= 1556 && time <= 1730) {
			entries = timetable.filter(item => item.startTime == '1600');
			for (var i = entries.length - 1; i >= 0; i--) {
				if (entries[i].sg == undefined) {
					console.log('undefined\n');
				} else {
					console.log(
						entries[i].startTime +
							' - ' +
							entries[i].sg +
							' in ' +
							entries[i].ro[0].longname
					);
				}
			}
		}
	});

And the one undefined comes from this:

Screenshot 2019-10-16 at 12 26 02

There is just no student group.

This is my code now. Sorry I didn't see your comments

const WebUntis = require('webuntis')

const untis = new WebUntis(
  'WHG+Durmersheim',
  'Abi2020',
  'Hund2020',
  'borys.webuntis.com'
)

untis
  .login()
  .then(() => {
    return untis.getOwnTimetableForToday()
  })
  .then(timetable => {
    const date = new Date()
    const time = parseInt(`${date.getHours()}${date.getMinutes()}`);

    if (time >= 745 && time <= 915) {
      entries = timetable.filter(item => item.startTime === "745")
    }

    if (time >= 916 && time <= 1105) {
      entries = timetable.filter(item => item.startTime === "935")
      printInfoString(entries)
    }

    if (time >= 1106 && time <= 1255) {
      entries = timetable.filter(item => item.startTime === "1125")
      printInfoString(entries)
    }

    if (time >= 1256 && time <= 1555) {
      entries = timetable.filter(item => item.startTime === "1425")
      printInfoString(entries)
    }

    if (time >= 1556 && time <= 1730) {
      entries = timetable.filter(item => item.startTime === "1600")
      printInfoString(entries)
    }
  })


function printInfoString(entries) {
  var str = ''
  for (const entry of entries) {
    str = str + getTime(entry) + ' - ' + getSubjectName() + ' in ' + getRoomName() + '\n'
  }
  console.log(str)
}

function getTime(entry) {
  return entry.startTime
}

function getSubjectName(entry) {
  return entry.sg
}

function getRoomName(entry) {
  for (const {
      longname
    } of entry.ro) {
    return longname
  }
}

And the one undefined comes from this:

Screenshot 2019-10-16 at 12 26 02

There is just no student group.

Yeah, I know. My school does not know how to use UNTIS the right way...

But because of something I can't wrap my head around, my current, rewritten code does not display anything except the newlines (\n).

But because of something I can't wrap my head around, my current, rewritten code does not display anything except the newlines (\n).

You now use ===

Correct:

entries = timetable.filter(item => item.startTime === 745);

Incorrect:

entries = timetable.filter(item => item.startTime === '745');

https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons

Thank you for your help, you may find my code now in this repo. It works now. https://github.com/niklasarnitz/untis-cli