Created
May 30, 2017 16:43
-
-
Save SupaHam/4fb2cd2375c523edf07d5e0ca06169ff to your computer and use it in GitHub Desktop.
Update University of Portsmouth library loans
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from bs4 import BeautifulSoup | |
import re, getpass, requests, os, sys | |
ACCOUNT_URL = "https://capitadiscovery.co.uk/port/account" | |
SESSION_URL = "https://capitadiscovery.co.uk/port/sessions" | |
LOANS_URL = "https://capitadiscovery.co.uk/port/account/loans" | |
FAKE_USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" | |
def get_account_number(): | |
if len(sys.argv) > 1: | |
return sys.argv[1] | |
return os.getenv('LIB_ACCOUNT_NUMBER') | |
def get_all_items(content): | |
soup = BeautifulSoup(content, "lxml") | |
form = soup.find('form', {'class': 'renewForm' }) | |
inputs = form.select('input') | |
items = [] | |
for input in inputs: | |
if input.get('name') == 'loan_ids[]': | |
items.append(input.get('value')) | |
return items | |
def main(): | |
session = requests.Session() | |
session.headers.update({ 'User-Agent': FAKE_USER_AGENT }) | |
library_number = get_account_number() | |
if not library_number: | |
raise ValueError, "Please specify a library_number in script first argument or LIB_ACCOUNT_NUMBER env" | |
sesh_req = session.post(SESSION_URL,data={ 'barcode': library_number }) | |
items = get_all_items(sesh_req.content) | |
data = list(('loan_ids[]', item) for item in items) | |
loans_req = session.post(LOANS_URL, data=data) | |
print "All done!" | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment