Help me with my Schoolsoft project?

Im new to python and would like some experienced guidance in how to create this Connector.

The idea is to pass the userid/pwd to a site that my Childs uses and retrieve the lunch menu.
They uses a site called Schoolsoft.

The result from the lunch menu should be passed, per weekday back to 5 sensors, one for each schoolday.

I have done some things but can’t make it to work and are pretty sure that there are a few things that needs correction. eg. the pass of data from site to 5 sensors.

Anyone up for advices or would like to get payed to develop this for me?

Thanks!

"""
Schoolsoft lunchmeny
"""

import logging

import voluptuous as vol

import homeassistant.helpers.config_validation as cv
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_SCHOOL CONF_USERNAME CONF_PASSWORD CONF_USERTYPE
from homeassistant.helpers.entity import Entity
from homeassistant.util import Throttle


_LOGGER = logging.getLogger(__name__)

# Validation of the user's configuration
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
	vol.Required(CONF_SCHOOL): cv.string,
	vol.Required(CONF_USERNAME): cv.string,
	vol.Required(CONF_PASSWORD): cv.string,
	vol.Required(CONF_USERTYPE): cv.string
})



def setup_platform(hass, config, add_devices, discovery_info=None):
		
	sensor_name = CONF_NAME
	school = config.get(CONF_SCHOOL)
	username = config.get(CONF_USERNAME)
	password = config.get(CONF_PASSWORD)
	usertype = config.get(CONF_USERTYPE)
    sensor_name = 'Lunchmenu'

	add_devices(schoolsoft)


class Schoolsoft(Entity):
	"""Implementation of the schoolsoft sensor."""

	def __init__(self, sensor_name, matsedel, school, username, password, usertype):
		"""Initialize the sensor."""
		
		self._name = sensor_name
		self._state = None
	
		
		self._data = {}
		self.update()
			
		self.school = school
		self.username = username
		self.password = password
		self.usertype = usertype

		self.cookies = {}

		_login_page_re = r"https://sms(\d*).schoolsoft.se/%s/html/redirect_login.htm"
		self._login_page_re = re.compile(_login_page_re % school)

	@property
	def name(self):
		"""Return the name of the sensor."""
		return self._name

	@property
	def state(self):
		"""Return the state of the sensor."""
		return self._state

	@property
	def icon(self):
		"""Icon to use in the frontend, if any."""
		return 'mdi:food'

    def try_get(self, url, attempts = 0):
		
		r = requests.get(url, cookies=self.cookies)

		login_page_match = self._login_page_re.match(r.url)
		if login_page_match:
			server_n = login_page_match.groups()
			if attempts < 1:
				# Sends a post request with self.username && self.password
				loginr = requests.post(self.login_page, data = {
					"action": "login",
					"usertype": self.usertype,
					"ssusername": self.username,
					"sspassword": self.password
					}, cookies=self.cookies, allow_redirects=False)

				# Saves login cookie for faster access after first call
				self.cookies = loginr.cookies

				return self.try_get(url, attempts+1)
			else:
				raise AuthFailure("Invalid username or password")
		else:
			return r

	def update(self):

		menu_html = self.try_get("https://sms5.schoolsoft.se/{}/jsp/student/right_student_lunchmenu.jsp?menu=lunchmenu".format(self.school))
		menu = BeautifulSoup(menu_html.text, "html.parser")

		lunch_menu = []

		for div in menu.find_all("td", {"style": "word-wrap: break-word"}):
			food_info = div.get_text(separator=u"<br/>").split(u"<br/>")
			lunch_menu.append(food_info)

		self._state = true

No-one that can offer to help?

I made a bash-script to get info from schoolsoft. PM if you want it.

1 Like

Found this old topic and I’m trying to make a family Dashboard. All three kids will be using Schoolsoft soon and an integration for schedules, homework and lunch menu would be great! I this script still valid?

Stort tack för ultrasnabbt svar!

Dyker ner i koden i helgen och ser om jag får ut data nästa vecka!

Det här hade ju varit superkul att få igång. Jag tror jag lyckats en bit men får inte in några värden i sensorn. Har du någon koll på vad jag kan göra för fel?

image

Hej! Jag kan inte hitta shell koden längre. Har den försvunnit? Skulle ni kunna mejla det kanske?
Tack!

Jag plockade bort den. Har gjort om den till Python. Kan se om jag hinner lägga upp den i helgen.

Ah! Bra! Jag håller på att försöka med python själv men det går sisådär. Jag lyckas inte logga in en requests.POST över inloggning. Om du kan visa hur du gör det skulle vara jätte tacksam!

Been running the script for +1 year now but recently it returns nothing. Did schoolssoft change the data?

Thanks! But i get RC 1.

Traceback (most recent call last):
  File "/config/python_scripts/schoolsoft.py", line 339, in <module>
    meals = api.fetch_lunch_menu(student=settings[0][0])
  File "/config/python_scripts/schoolsoft.py", line 123, in fetch_lunch_menu
    lunch_menu_grouped.append([lunch_menu[day],lunch_menu[day+1]])
IndexError: list index out of range
bash-5.1# 


Hi! i’m getting an error when running the python script:
“Traceback (most recent call last):
File “/tmp/kk/schoolsoft.py”, line 332, in
settings = api.fetch_settings()
File “/tmp/kk/schoolsoft.py”, line 100, in fetch_settings
return students
UnboundLocalError: local variable ‘students’ referenced before assignment”
Any idea on why I’m getting this? Thanks!

I moved the code to:

Make an issue there, it’s easier to follow.