aboutsummaryrefslogtreecommitdiff
path: root/libraries/aspen.py
blob: bb430c3a405711b70795c691bf82e5db999eb071 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#!/usr/bin/env python3
# File: libraries/aspen.py
# Author: bgstack15
# Startdate: 2024-07-06-7 08:06
# SPDX-License-Identifier: GPL-3.0-only
# Title: Library Plugin for Aspen
# Project: library_info
# Purpose: plugin for aspen-based library websites
# History:
# Usage:
# Reference:
# Improve:
# Dependencies:
#    dep-devuan: python3-bs4

from .base import *
import requests, json, dateutil, base64, os, sys
from bs4 import BeautifulSoup

class Library(BaseLibrary):

   def __init__(self, config_obj = None, alias = None, username = None, password = None, baseurl = None, session = None):
      if config_obj and "username" in config_obj:
         self.username = config_obj["username"]
      else:
         self.username = username
      if config_obj and "password" in config_obj:
         self.password = config_obj["password"]
      else:
         self.password = password
      if config_obj and "baseurl" in config_obj:
         self.baseurl = config_obj["baseurl"]
      else:
         self.baseurl = baseurl if baseurl else "https://aspen.example.org"
      self.baseurl = self.baseurl.rstrip("/")
      if session and type(session) == requests.sessions.Session:
         self.session = session
      else:
         self.session = requests.Session()
      if config_obj and "alias" in config_obj:
         self.alias = config_obj["alias"]
      else:
         self.alias = alias if alias else "Aspen-based library"
      # log in now. Why would we not?
      self.login()

   def get_reservations(self, verbose = False):
      availableReservations = []
      unavailableReservations = []
      b = self.baseurl
      s = self.session
      # step 1: visit "titles on hold" page so it does not complain that I am taking shortcuts
      headers = {
         "Referer": f"{b}/MyAccount/CheckedOut?source=all"
      }
      s.get(f"{b}/Holds?source=ils",headers=headers)
      params = {
         "method": "getHolds",
         "source": "all"
      }
      output = s.get(f"{b}/MyAccount/AJAX",params=params,headers=headers)
      output = json.loads(output.content)["holds"].replace("\xa0"," ")
      soup = BeautifulSoup(output, "html.parser")
      try:
         availableholds_all = soup.find("label",attrs={"for":"availableHoldSort_all"}).parent.next_sibling.next_sibling
      except AttributeError:
         # the label will not exist if there are no availableHolds
         availableholds_all = None
      try:
         unavailableholds_all = soup.find("label",attrs={"for":"unavailableHoldSort_all"}).parent.next_sibling.next_sibling
      except AttributeError:
         # the label will not exist if there are no unavailableHolds
         unavailableholds_all = None
      if unavailableholds_all:
         items = unavailableholds_all.find_all("div",class_=["result"])
         for i in items:
            labels = [j.text for j in i.find_all("div","result-label")]
            values = [j.text for j in i.find_all("div","result-value")]
            values_dict = dict(map(lambda i,j:(i,j),labels,values))
            title_obj = i.find("a",class_="result-title")
            img_href = i.find("img")["src"]
            img_b64, img_type = self.get_image(img_href)
            obj = {
               "patron": self.alias,
               "position": values_dict["Position"] if "Position" in values_dict else "",
               "status": values_dict["Status"],
               "date_placed": values_dict["Date Placed"],
               "format": values_dict["Format"],
               "location": values_dict["Pickup Location"],
               "title": title_obj.text,
               "img_href": img_href,
               "img50": img_b64[:50],
               "img": img_b64,
               "img_type": img_type,
            }
            unavailableReservations.append(obj)
      if availableholds_all:
         items = availableholds_all.find_all("div",class_=["result"])
         for i in items:
            labels = [j.text for j in i.find_all("div","result-label")]
            values = [j.text for j in i.find_all("div","result-value")]
            values_dict = dict(map(lambda i,j:(i,j),labels,values))
            title_obj = i.find("a",class_="result-title")
            img_href = i.find("img")["src"]
            img_b64, img_type = self.get_image(img_href)
            if verbose:
               #print(f"DEBUG available: item {i}", file=sys.stderr)
               print(f"DEBUG available: title {title_obj.text}", file=sys.stderr)
               print(f"DEBUG available: values_dict {values_dict}", file=sys.stderr)
            obj = {
               "patron": self.alias,
               "position": values_dict["Position"] if "Position" in values_dict else "",
               "status": "ready",
               "date_placed": values_dict["Date Placed"],
               "format": values_dict["Format"],
               "location": values_dict["Pickup Location"],
               "title": title_obj.text,
               "img_href": img_href,
               "img50": img_b64[:50],
               "img": img_b64,
               "img_type": img_type,
            }
            availableReservations.append(obj)
      # Return a single list of objects
      return availableReservations + unavailableReservations

   def get_checkouts(self, verbose = False):
      checked_out_objects = []
      b = self.baseurl
      s = self.session
      # step 1: visit the "checked out" web page, so it doesn't freak out that I am taking shortcuts
      headers = {
         "Content-Type": "application/x-www-form-urlencoded",
         "Referer": f"{b}/MyAccount/Home",
         "Priority": "u=1"
      }
      s.get(f"{b}/MyAccount/CheckedOut", headers = headers)
      # step 2: visit the checkout list which is a cruddy html-inside-json garbage
      headers = {
         "Referer": f"{b}/MyAccount/CheckedOut?source=all"
      }
      params = {
         "method": "getCheckouts",
         "source": "all"
      }
      output = s.get(f"{b}/MyAccount/AJAX",params=params,headers=headers)
      output = json.loads(output.content)["checkouts"].replace("\xa0"," ")
      soup = BeautifulSoup(output, "html.parser")
      # goals: get title, format, picture, barcode, due date, possible renewal date, times_renewed, when_checked_out
      results = soup.find_all("div", class_ = "result row")
      #results = soup.find_all("span",class_="result-index")
      #results = [i.parent.parent.parent for i in results]
      for i in results:
         title = i.find(class_ = "result-title").contents[0]
         labels = [j.contents[0] for j in i.find_all("div", class_ = "result-label")]
         values = [j.contents[0] for j in i.find_all("div", class_ = "result-value")]
         values_dict = dict(map(lambda i,j:(i,j),labels,values))
         if verbose:
            print(f"DEBUG: Values_dict: {values_dict}",file=sys.stderr)
         # contains Call number, Format, Barcode, Due
         img_href = i.find("img", class_="listResultImage")["src"]
         img_b64, img_type = self.get_image(img_href)
         # normalize format
         item_format = ""
         item_format = "book" if "book" in values_dict["Format"].lower() else ""
         if not item_format:
            item_format = values_dict["Format"]
         obj = {
            "patron": self.alias,
            "title": title,
            "format": item_format,
            "barcode": values_dict["Barcode"],
            "due": dateutil.parser.parse(values_dict["Due"]),
            "img_href": img_href,
            "img50": img_b64[:50],
            "img": img_b64,
            "img_type": img_type,
         }
         checked_out_objects.append(obj)
      return checked_out_objects

   def get_class_name(self):
      return os.path.basename(__file__).replace(".py","")

   def login(self):
      b = self.baseurl
      s = self.session
      # step 1: visit login page
      s.get(f"{b}/MyAccount/Home")
      # step 2: log in
      # curl 'https://aspen.example.org/MyAccount/Home' -X POST -H 'Content-Type: application/x-www-form-urlencoded' -H 'Referer: https://aspen.example.org/MyAccount/Home' -H 'Priority: u=1' --data-raw 'username=987213497234&password=1234&submit=Login'
      data = {
         "username": self.username,
         "password": self.password,
         "submit": "Login"
      }
      headers = {
         "Content-Type": "application/x-www-form-urlencoded",
         "Referer": f"{b}/MyAccount/Home",
         "Priority": "u=1"
      }
      s.post(f"{b}/MyAccount/Home", headers = headers, data = data)
bgstack15