Skip to content

Commit

Permalink
library url + user barcode spell
Browse files Browse the repository at this point in the history
  • Loading branch information
myTselection committed Mar 24, 2024
1 parent 1465809 commit 16d6093
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ content: >-
<details><summary><b>{{state_attr(user,'username') }}
{{state_attr(user,'libraryName') }}:</b></summary>
- Kaart {{state_attr(user,'barcode') }}:
- Kaart {{state_attr(user,'barcode') }} ({{state_attr(user,'barcode_spell')| join(', ') }}):
[<img src="{{state_attr(user,'barcode_url') }}" height=100></img>]({{state_attr(user,'barcode_url') }})
- Gereserveerde stuks: {{state_attr(user,'num_reservations') }}
Expand Down
2 changes: 1 addition & 1 deletion custom_components/bibliotheek_be/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/myTselection/bibliotheek_be/issues",
"requirements": ["bs4", "html5lib","httpx[http2]"],
"version": "1.6.12"
"version": "1.7.0"
}
22 changes: 14 additions & 8 deletions custom_components/bibliotheek_be/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,12 @@ async def _force_update(self):
assert loandetails is not None
username = userdetail.get('account_details').get('userName')
barcode = userdetail.get('account_details').get('barcode')
barcode_spell = userdetail.get('account_details').get('barcode_spell')
for loan_info in loandetails.values():
loan_info["user"] = username
loan_info["userid"] = user_id
loan_info["barcode"] = barcode
loan_info["barcode_spell"] = barcode_spell
libraryName = loan_info.get('library')
libraryurl = f"{loan_info['url'].split('/resolver')[0]}/adres-en-openingsuren"
if not self._librarydetails.get(libraryName):
Expand Down Expand Up @@ -190,6 +192,7 @@ def __init__(self, data, hass, userid):
self._num_reservations = self._data._userdetails.get(self._userid).get('reservations').get('reservations')
self._open_amounts = self._data._userdetails.get(self._userid).get('open_amounts').get('open_amounts')
self._barcode = self._data._userdetails.get(self._userid).get('account_details').get('barcode')
self._barcode_spell = self._data._userdetails.get(self._userid).get('account_details').get('barcode_spell')
self._username = self._data._userdetails.get(self._userid).get('account_details').get('userName')
self._libraryName = self._data._userdetails.get(self._userid).get('account_details').get('libraryName')
self._loandetails = self._data._loandetails.get(self._userid)
Expand All @@ -208,6 +211,7 @@ async def async_update(self):
self._num_reservations = self._data._userdetails.get(self._userid).get('reservations').get('reservations')
self._open_amounts = self._data._userdetails.get(self._userid).get('open_amounts').get('open_amounts')
self._barcode = self._data._userdetails.get(self._userid).get('account_details').get('barcode')
self._barcode_spell = self._data._userdetails.get(self._userid).get('account_details').get('barcode_spell')
self._username = self._data._userdetails.get(self._userid).get('account_details').get('userName')
self._libraryName = self._data._userdetails.get(self._userid).get('account_details').get('libraryName')
self._loandetails = self._data._loandetails.get(self._userid)
Expand Down Expand Up @@ -243,6 +247,7 @@ def extra_state_attributes(self) -> dict:
"last update": self._last_update,
"userid": self._userid,
"barcode": self._barcode,
"barcode_spell": self._barcode_spell,
"barcode_url": f"https://barcodeapi.org/api/128/{self._barcode}",
"num_loans": self._num_loans,
"num_reservations": self._num_reservations,
Expand Down Expand Up @@ -294,7 +299,7 @@ def __init__(self, data, hass, libraryName, loanTypes):
self._num_loans = 0
self._num_total_loans = 0
self._loantypes = loanTypes
self._current_lbrarydetails = self._data._librarydetails.get(libraryName)
self._current_librarydetails = self._data._librarydetails.get(libraryName)


@property
Expand Down Expand Up @@ -372,14 +377,15 @@ def extra_state_attributes(self) -> dict:
"num_loans": self._num_loans,
"num_total_loans": self._num_total_loans,
"loandetails": self._loandetails,
"address": self._current_lbrarydetails.get('address'),
"latitude": self._current_lbrarydetails.get('lat'),
"longitude": self._current_lbrarydetails.get('lon'),
"url": self._current_librarydetails.get('url'),
"address": self._current_librarydetails.get('address'),
"latitude": self._current_librarydetails.get('lat'),
"longitude": self._current_librarydetails.get('lon'),
"entity_picture": "https://raw.githubusercontent.com/myTselection/bibliotheek_be/master/icon.png",
"phone": self._current_lbrarydetails.get('phone'),
"email": self._current_lbrarydetails.get('email'),
"opening_hours": self._current_lbrarydetails.get('hours'),
"closed_dates": self._current_lbrarydetails.get('closed_dates')
"phone": self._current_librarydetails.get('phone'),
"email": self._current_librarydetails.get('email'),
"opening_hours": self._current_librarydetails.get('hours'),
"closed_dates": self._current_librarydetails.get('closed_dates')
}
attributes.update(self._loantypes)
return attributes
Expand Down
27 changes: 26 additions & 1 deletion custom_components/bibliotheek_be/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from urllib.parse import urlsplit, parse_qs
from bs4 import BeautifulSoup
import httpx
from collections import OrderedDict


import voluptuous as vol
Expand Down Expand Up @@ -213,12 +214,35 @@ def login(self, username, password):
open_amounts_url = f"https://bibliotheek.be{div.find('a', href=re.compile('te-betalen')).get('href')}"
except AttributeError:
open_amounts_url = ""

account_details['barcode_spell'] = self.count_repeated_numbers(account_details['barcode'])
_LOGGER.debug(f"uitleningen {loans} , url: {loans_url}, reservatie: {reservations}, url {account_url}, account_details {account_details}")
self.userdetails[account_details.get('id')]={'account_details': account_details , 'loans': { 'loans': loans, 'url': loans_url, 'history': loan_history_url}, 'reservations': {'reservations': reservations, 'url':reservations_url}, 'open_amounts': {'open_amounts': open_amounts, 'url':''}}
_LOGGER.debug(f"self.userdetails {json.dumps(self.userdetails,indent=4)}")
return self.userdetails

def count_repeated_numbers(self, input_string):
counts = []
current_char = None
current_count = 0

for char in input_string:
if char == current_char:
current_count += 1
else:
if current_count > 1:
counts.append(str(current_count) + "x" + current_char)
elif current_char is not None:
counts.append(current_char)
current_char = char
current_count = 1

if current_count > 1:
counts.append(str(current_count) + "x" + current_char)
else:
counts.append(current_char)

return counts

def library_details(self, url):
header = {"Content-Type": "application/json"}

Expand All @@ -239,6 +263,7 @@ def library_details(self, url):
libraryArticle = soup.find('article',class_='library library--page-item')
# libraryArticle = soup.find('div',class_='block block-system block-system-main-block')
library_info = {}
library_info['url'] = url.replace('/adres-en-openingsuren','')
if libraryArticle is None:
_LOGGER.error(f"No library info found, {url}")
return library_info
Expand Down

0 comments on commit 16d6093

Please sign in to comment.