Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New api #1

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
2 changes: 0 additions & 2 deletions custom_components/bibliotek_dk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from .library_api import Library

from .const import (
CONF_AGENCY,
CONF_HOST,
CONF_MUNICIPALITY,
CONF_PINCODE,
Expand All @@ -29,7 +28,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
entry.data[CONF_PINCODE],
entry.data[CONF_HOST],
libraryName=entry.data[CONF_MUNICIPALITY],
agency=entry.data[CONF_AGENCY],
)

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
Expand Down
23 changes: 7 additions & 16 deletions custom_components/bibliotek_dk/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er, selector
from homeassistant.helpers import selector
from homeassistant import config_entries

from .library_api import Library
Expand All @@ -28,7 +28,6 @@
CONF_NAME,
CONF_PINCODE,
CONF_SHOW_DEBTS,
CONF_SHOW_E_LIBRARY,
CONF_SHOW_LOANS,
CONF_SHOW_RESERVATIONS,
CONF_UPDATE_INTERVAL,
Expand All @@ -54,16 +53,9 @@ async def validate_input(
data[CONF_UPDATE_INTERVAL] if data[CONF_UPDATE_INTERVAL] else UPDATE_INTERVAL
)

# Add agency for ereolen.dk if boolean is set
data[CONF_AGENCY] = (
libraries[data[CONF_MUNICIPALITY]][CONF_AGENCY]
if data[CONF_SHOW_E_LIBRARY]
else None
)

# Typecast userId and Pincode to string:
data[CONF_USER_ID] = re.sub("\D", "", data[CONF_USER_ID])
data[CONF_PINCODE] = re.sub("\D", "", data[CONF_PINCODE])
data[CONF_USER_ID] = re.sub(r"\D", "", data[CONF_USER_ID])
data[CONF_PINCODE] = re.sub(r"\D", "", data[CONF_PINCODE])

# If there is any other instances of the integration
if DOMAIN in hass.data:
Expand All @@ -77,7 +69,7 @@ async def validate_input(

# If instance is running wait...
while any(
libraryObj.running == True for libraryObj in hass.data[DOMAIN].values()
libraryObj.running is True for libraryObj in hass.data[DOMAIN].values()
):
await asyncio.sleep(random.randint(5, 10))

Expand Down Expand Up @@ -143,7 +135,7 @@ def refreshLibraries() -> tuple:
libraries, excLibraries = {}, {}
if "folk" in librariesJSON.keys():
for library in librariesJSON["folk"]:
p = re.compile("^.+?[^\/:](?=[?\/]|$)")
p = re.compile(r"^.+?[^\/:](?=[?\/]|$)")
m = p.match(library["registrationUrl"])
# Only use libraries NOT using gatewayf
if "gatewayf" not in library["registrationUrl"]:
Expand Down Expand Up @@ -172,7 +164,7 @@ def municipalityFromCoor(lon, lat):
r.raise_for_status()

except requests.exceptions.HTTPError as err:
_LOGGER.error(f"HTTP Error while fetching (%s): {err}",URL_FALLBACK + URL_LOGIN_PAGE)
_LOGGER.error(f"HTTP Error while fetching (%s): {err}", URL_FALLBACK + URL_LOGIN_PAGE)
# Handle the error as needed, e.g., raise it, log it, or notify the user.
return ""
except requests.exceptions.Timeout:
Expand All @@ -182,7 +174,7 @@ def municipalityFromCoor(lon, lat):
"Too many redirects fecthing (%s)", URL_FALLBACK + URL_LOGIN_PAGE
)
except requests.exceptions.RequestException as err:
_LOGGER.error(f"Request Exception while fetching (%s): {err}",URL_FALLBACK + URL_LOGIN_PAGE)
_LOGGER.error(f"Request Exception while fetching (%s): {err}", URL_FALLBACK + URL_LOGIN_PAGE)
return ""

municipality = json.loads(r.text)
Expand Down Expand Up @@ -235,7 +227,6 @@ def municipalityFromCoor(lon, lat):
),
vol.Required(CONF_USER_ID, default=""): str,
vol.Required(CONF_PINCODE, default=""): str,
vol.Required(CONF_SHOW_E_LIBRARY, default=True): bool,
vol.Required(CONF_SHOW_LOANS, default=True): bool,
# vol.Required(CONF_SHOW_LOANS_OVERDUE, default=True): bool,
vol.Required(CONF_SHOW_DEBTS, default=True): bool,
Expand Down
29 changes: 16 additions & 13 deletions custom_components/bibliotek_dk/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
CONF_NAME = "name"
CONF_PINCODE = "pincode"
CONF_SHOW_DEBTS = "show_debts"
CONF_SHOW_E_LIBRARY = "show_e_library"
CONF_SHOW_LOANS = "show_loans"
CONF_SHOW_RESERVATIONS = "show_reservations"
CONF_UPDATE_INTERVAL = "update_interval"
CONF_USER_ID = "user_id"
CREDITS = "J-Lindvig (https://github.com/J-Lindvig)"
CREDITS = "J-Lindvig & TermeHansen (https://github.com/TermeHansen/Bibliotek_dk)"

DOMAIN = "bibliotek_dk"

Expand All @@ -23,17 +22,21 @@
"Referer": "https://www.google.dk/",
}

JSON_HEADERS = {
"Accept-Encoding": "gzip, deflate, br, zstd",
"Content-Type": "application/json",
'Accept': '*/*',
}

MUNICIPALITY_LOOKUP_URL = "https://api.dataforsyningen.dk/kommuner/reverse?x=LON&y=LAT"

UPDATE_INTERVAL = 60
URL_FALLBACK = "https://fmbib.dk"
URL_LOGIN = "/adgangsplatformen/login"
URL_LOGIN_PAGE = URL_LOGIN + "?destination=ding_frontpage"
URL_LOGIN_PAGE_ELIB = URL_LOGIN + "?destination=/user"
USER_AGENTS = [
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:77.0) Gecko/20100101 Firefox/77.0",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36",
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
]
URL_FALLBACK = "https://bibliotek.kk.dk"
URL_LOGIN = "/login"
URL_LOGIN_PAGE = "/login?current-path=/user/me/dashboard"
ICON = '''<svg fill="none" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 35 35">
<g clip-path="url(#clip0)">
<path d="M33.42 21.64C33.0397 20.6347 32.4655 19.7139 31.73 18.93C31.0559 18.1869 30.2353 17.5911 29.32 17.18C28.7903 16.9202 28.238 16.7093 27.67 16.55C28.8958 16.0436 29.9852 15.2557 30.85 14.25C32.1307 12.6395 32.7748 10.6145 32.66 8.56005C32.7195 7.4066 32.5347 6.25358 32.1177 5.17652C31.7007 4.09945 31.0608 3.12264 30.24 2.31005C29.3502 1.50944 28.3106 0.893081 27.1812 0.496591C26.0518 0.100101 24.855 -0.0686637 23.66 4.78951e-05H0V2.43005H3.54V32.57H0V35H24.88C26.1219 35.0529 27.361 34.8423 28.5157 34.3821C29.6705 33.9219 30.7148 33.2226 31.58 32.3301C33.3112 30.3743 34.2085 27.8188 34.08 25.21C34.1048 23.9876 33.8802 22.7729 33.42 21.64ZM24.1 14.25C25.3771 12.6381 26.0176 10.6132 25.9 8.56005C25.9512 7.4655 25.7849 6.37167 25.4107 5.34179C25.0365 4.31192 24.4619 3.36645 23.72 2.56005C24.3934 2.55725 25.0603 2.69246 25.6794 2.95732C26.2986 3.22218 26.8569 3.61109 27.32 4.10005C27.7985 4.62229 28.1689 5.23403 28.41 5.90005C28.8031 6.88614 29.0034 7.93849 29 9.00005C29.0136 10.0236 28.8373 11.0408 28.48 12C28.2435 12.741 27.8514 13.4229 27.33 14C26.7903 14.5444 26.1388 14.9649 25.4205 15.2325C24.7022 15.5002 23.9343 15.6085 23.17 15.55H22.73C23.228 15.1625 23.6868 14.727 24.1 14.25ZM14.63 2.53005H16.41C17.1752 2.47082 17.9441 2.57978 18.6628 2.84927C19.3814 3.11877 20.0324 3.54226 20.57 4.09005C20.9442 4.49544 21.2514 4.95792 21.48 5.46005C22.004 6.56564 22.2674 7.77669 22.25 9.00005C22.2611 10.0966 22.0537 11.1845 21.64 12.2C21.2665 13.2589 20.5475 14.1611 19.5987 14.7616C18.65 15.362 17.5268 15.6257 16.41 15.51H14.63V2.53005ZM10.92 32.4701H7.25V18.05H10.92V32.4701ZM10.92 15.52H7.25V2.52005H10.92V15.52ZM22.58 29.66C22.3813 30.0238 22.1359 30.36 21.85 30.66C21.3028 31.2359 20.6423 31.6922 19.91 32C19.0449 32.3282 18.125 32.4877 17.2 32.4701H14.63V18.05H17.2C18.1299 18.0299 19.0536 18.2071 19.91 18.57C20.6427 18.8541 21.3047 19.2943 21.85 19.86C22.0047 20.0272 22.1483 20.2042 22.28 20.39C23.2494 21.8215 23.7428 23.5221 23.69 25.25C23.7204 26.7928 23.3372 28.3155 22.58 29.66ZM29.65 29C29.4274 29.616 29.0879 30.183 28.65 30.67C28.091 31.2478 27.4163 31.701 26.67 32C26.0294 32.2468 25.3551 32.3952 24.67 32.44L24.77 32.3301C26.5048 30.3758 27.4058 27.8202 27.28 25.21C27.3012 23.9879 27.0768 22.7739 26.62 21.64C26.2606 20.6403 25.7105 19.7199 25 18.93C24.6918 18.6124 24.3572 18.3214 24 18.06C24.9093 18.0425 25.8122 18.2162 26.65 18.57C27.3784 18.8567 28.0366 19.2967 28.58 19.86C29.0035 20.3013 29.3424 20.8165 29.58 21.38C30.137 22.5933 30.4171 23.9152 30.4 25.25C30.4166 26.5389 30.1611 27.8167 29.65 29Z" fill="#3333FF"/>
</g>
</svg>'''

Loading