publicpythonsnippets/pythonbookdef.py

155 lines
6.5 KiB
Python

class libaccount():
def __init__(self,name,barcode,pin):
self.name=name
self.barcode=barcode
self.pin=pin
class book():
def __init__(self,itemid,renewalcount,checkoutdate,duedate,vendor,resourceid,resourceinstanceid,title,materialtype,coverurl,person,authsession,renewresult,overdue):
self.id=itemid
self.renewalcount=renewalcount
self.checkoutdate=checkoutdate
self.duedate=duedate
self.vendor=vendor
self.resourceid=resourceid
self.resourceinstanceid=resourceinstanceid
self.title=title
self.materialtype=materialtype
self.coverurl=coverurl
self.person=person
self.authsession=authsession
self.renewresult=renewresult
self.overdue=overdue
#self.ill=ill
def print(self):
print(self.person, self.id, self.title, self.checkoutdate, self.renewalcount, self.duedate, self.materialtype,self.renewresult)
def renew(self):
print("Renewing Book",self.title)
renewuri='https://na2.iiivega.com/api/search-result/patrons/me/checkouts/' + self.id + '/renew'
try:
renewresult = self.authsession.post(renewuri).json()
if renewresult.get('message'):
self.renewresult=renewresult['message']
else:
self.renewalcount=renewresult['renewalCount']
self.duedate=renewresult['dueDate']
self.renewresult="Successfully Renewed"
except requests.exceptions.RequestException as e: # This is the correct syntax
print(e.json())
finally:
pass
def sendemail(body,subject):
print("Sending Email")
boto3.setup_default_session(profile_name='hamik')
sesclient=boto3.client('ses',region_name="us-east-2")
CHARSET="UTF-8"
response = sesclient.send_email(
Destination={
"ToAddresses":["dan@hamik.net"]
},
Message= {
"Body": {
"Html": {
"Charset": CHARSET,
"Data": body
}
},
"Subject": {
"Charset": CHARSET,
"Data": subject
},
},
Source='librarynotices@hamik.net'
)
allbooks=[]
mediatypes=[]
for myaccount in accounts:
account=accounts[myaccount]
print(account.name)
# Create a session
session = requests.Session()
authbody = {
'username':account.barcode,
'password':account.pin,
'client_id':'convergence',
'grant_type':'password',
}
resp=session.post(uri,authbody).json()
authtoken=resp['access_token']
tokentype=resp['token_type']
sessionstate=resp['session_state']
session.headers.update({
'iii-customer-domain':'sioux.na2.iiivega.com',
'Origin':'https://sioux.na2.iiivega.com/',
'Referrer':'https://sioux.na2.iiivega.com/',
'access_token':authtoken,
'Authorization':'Bearer ' + authtoken,
'token_type': tokentype,
'session_state': sessionstate,
'api-version':'1',
'DNT':'1',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
} )
outs = session.get(checkoutsuri)
if len(outs.json()) >=1:
for mybook in outs.json():
if mybook['vendor']=='overdrive':
continue
duedate = datetime.datetime.strptime(mybook['dueDate'].split('T')[0],'%Y-%m-%d').date()
if datetime.date.today() > duedate:
overdue=True
else:
overdue = False
thisbook = book(
mybook['id'],
mybook['renewalCount'],
datetime.datetime.strptime(mybook['checkOutDate'].split('T')[0],'%Y-%m-%d').date(),
duedate,
mybook['vendor'],
mybook['resource']['id'],
mybook['resource']['instanceId'],
mybook['resource']['title'],
mybook['resource']['materialType'],
mybook['resource']['coverUrl']['small'],
account.name,
session,
'None',
overdue
)
if thisbook.materialtype in mediatypes:
pass
else:
mediatypes.append(thisbook.materialtype)
allbooks.append(thisbook)
duesoon = [d for d in allbooks if d.duedate < datetime.date.today() + datetime.timedelta(days=10)]
for book in allbooks:
#print(datetime.date.today(),book.duedate)
if datetime.date.today() > book.duedate:
book.renew()
if len(duesoon) >=1:
#print(htmltable)
htmltable += tablestart + "<colgroup><col/><col/></colgroup><tr><th>Name"+th+"Count</th></tr>"
for type in mediatypes:
typecount = sum(b.materialtype == type for b in duesoon)
htmltable += "<tr><td>" + type + td + str(typecount) + "</td></tr>"
htmltable += "</table><br>" + tablestart + "<colgroup><col/><col/><col/><col/><col/><col/><col/><col/></colgroup><tr><th>Name"+th+"Type"+th+"Times Renewed"+th+"Checked Out"+th+"Due Date"+th+"Renewal Status" + th + "Title"+ th + "Thumbnail</th></tr>"
for item in duesoon:
htmltable +="<tr>" + td + item.person + td + item.materialtype + td + str(item.renewalcount) + td + str(item.checkoutdate) + td + str(item.duedate) + td + item.renewresult+ td + item.title + td + '<img src="'+ item.coverurl+'">' +"</td></tr>\n"
htmltable += "</table>"
htmltable += "<br>"
htmltable += tablestart + "<colgroup><col/><col/></colgroup><tr><th>Name"+th+"Count</th></tr>"
for type in mediatypes:
typecount = sum(b.materialtype == type for b in allbooks)
htmltable += "<tr><td>" + type + td + str(typecount) + "</td></tr>"
htmltable += "</table><br>"
htmltable += tablestart + "<colgroup><col/><col/><col/><col/><col/><col/><col/></colgroup><tr><th>Name"+th+"Type"+th+"Times Renewed"+th+"Checked Out"+th+"Due Date"+th+"Title" + th + "Thumbnail</th></tr>"
for item in allbooks:
htmltable += "<TR>"+td + item.person + td + item.materialtype + td + str(item.renewalcount) + td + str(item.checkoutdate) + td + str(item.duedate) + td + item.title + td + '<img src="'+ item.coverurl+'">' +"</td></tr>\n"
htmltable += "</table>"
with open("out.html","w",encoding='utf-8') as outhtml:
outhtml.write(htmltable)
sendemail(htmltable,"Library books due in the next " + str(upcomingdays) + " days " + str(datetime.date.today()))