Skip to main content

SFPL

Python Package for accessing account, book, and author data from the SFPL Website.

Status

python-test static-analysis pypi

Installation

From pip:

$ pip install sfpl

From source:

$ pip install git+git://github.com/kajchang/SFPL.git

Or clone / download this repository and $ python setup.py install or $ pip install .

Frameworks Used

requests - Used for getting data from the SFPL website and managing login cookies.

bs4 + lxml - Used for parsing information from HTML.

Features

  • Managing current checkouts and holds for your SFPL library account.

  • Searching for books by keyword, title, author, subject, and tag and searching for user-created book lists.

  • Following other library users and viewing their book lists.

  • Getting libary branch hours.

TODO

  • Better Book Status Messages

How to Use

Searching for books on Python:

>>> from sfpl import Search
>>> python_search = Search('Python')
>>> results = python_search.getResults(pages=2) # .getResults is a generator that yields / streams pages of results
>>> for page in results:
		print(page)
[Python by Donaldson, Toby, Python by Johansen, Andrew, Python! by Moses, Brian, Python by McGrath, Mike, Python by Vo. T. H, Phuong]
[Python by Romano, Fabrizio, Python by Phillips, Dusty, Python by Joshi, Prateek, Python by Lassoff, Mark, Python by Wayani, Rafiq]

Searching for books by J.K. Rowling:

>>> from sfpl import Search
>>> jk_search = Search('J.K. Rowling', _type='author')
>>> results = jk_search.getResults()
>>> first_page = next(results).getBooks()
>>> first_page[0].title
"Harry Potter and the Deathly Hallows"
>>> first_page[0].getDetails()["brief"]["description"]
"Harry discovers what fate truly has in store for him as he inevitably makes his way to the final meeting with Voldemort. Book #7"

Searching for book lists related to San Francisco:

>>> from sfpl import Search
>>> list_search = Search('San Francisco', _type='list')
>>> results = list_search.getResults()
>>> first_page = next(results)
>>> first_page[0].title
'Made in SF - San Francisco love for young readers'
>>> for book in first_page[0].getBooks():
		print(book)
Al Capone Does My Shirts by Choldenko, Gennifer
Book Scavenger by Bertman, Jennifer Chambliss
...

Getting all your books on hold:

>>> from sfpl import Account
>>> my_account = Account('barcode', 'pin') # Replace with your barcode and pin.
>>> my_holds = my_account.getHolds()
>>> for book in my_holds:
		print(book.title)
'Python for Data Analysis'
'Automate the Boring Stuff With Python'
>>> for book in my_holds:
		print(book.status)
'#4 on 6 copies'
'#7 on 3 copies'
>>> for book in my_holds:
		print(book.author)
'McKinney, Wes'
'Sweigart, Al'

Searching for books by J.K. Rowling but not about Harry Potter:

>>> from sfpl import AdvancedSearch
>>> search = AdvancedSearch(includeauthor='J.K. Rowling', excludekeyword='Harry Potter') # Search for books by J.K. Rowling but don't include 'Harry Potter'
>>> results = search.getResults()
>>> for book in results:
		print(book.title)
'Fantastic Beasts and Where to Find Them'
'Fantastic Beasts and Where to Find Them : The Original Screenplay'
'The Casual Vacancy'
'Very Good Lives'
'Una vacante imprevista'

Getting hours for a library branch:

>>> from sfpl import Branch
>>> branch = Branch('anza')
>>> branch.getHours()
{'Sun': '1 - 5', 'Mon': '12 - 6', 'Tue': '10 - 9', 'Wed': '1 - 9', 'Thu': '10 - 6', 'Fri': '1 - 6', 'Sat': '10 - 6'}

Command-Line Interface

Installing the package provides an sfpl command. You can also run it directly with pipx run sfpl or uvx sfpl. The same interface is available through python -m sfpl:

$ sfpl --help

Search

Search books or media by keyword, title, author, subject, or tag:

$ sfpl search "J.K. Rowling" --type author --pages 2
$ sfpl search "climate fiction" --type subject
$ sfpl search "music" --format LP --sort newly_acquired --on-order
$ sfpl search "music" --format LP --no-on-order

Use --type list to search user-created lists:

$ sfpl search "San Francisco" --type list

Advanced search accepts repeatable --include FIELD=TERM and --exclude FIELD=TERM filters, as well as --format and --sort options. At least one --include filter is required. Included filters match all terms by default; use --match any to match any included term. The same field cannot be repeated within --include or --exclude.

$ sfpl advanced-search \
    --include "author=J. K. Rowling" \
    --include "keyword=magic" \
    --exclude "title=Harry Potter" \
    --format BK \
    --sort relevance \
    --match all \
    --pages 2

Valid advanced-search fields are keyword, author, title, subject, series, award, identifier, region, genre, publisher, and callnumber.

Pass --details to sfpl search or sfpl advanced-search to include full metadata (format, publication date, description, etc.) for each result item:

$ sfpl search "vegan" --sort newly_acquired --no-on-order --details

Book Details

Look up detailed metadata for a specific item by catalog ID:

$ sfpl details 4564247093

Branch Hours

$ sfpl branch-hours west portal
$ sfpl branch-hours "main library"

Account Holds and Checkouts

$ sfpl account holds --barcode "your library card barcode"
$ sfpl account checkouts --barcode "your library card barcode"

The command prompts for your PIN without displaying it.

Alternatively, provide both credentials through environment variables:

$ export SFPL_BARCODE="your library card barcode"
$ export SFPL_PIN="your library account PIN"
$ sfpl account holds

Release files for sfpl 1.6.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sfpl 1.6.2
File Size Uploaded
sfpl-1.6.2.tar.gz 22.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sfpl 1.6.2
File Interpreter ABI Platform
sfpl-1.6.2-py3-none-any.whl Python 3 none any Details

Total release size: 39.2 kB

Release files / sfpl-1.6.2.tar.gz

Download URL sfpl-1.6.2.tar.gz
Size 22.2 kB
Tags Source
SHA-256 checksum
How to use checksums
8cd6f4dacfb61da5d4980f1b6fb6a1b77e3c1e343cb574e511137158f5421922
BLAKE2b-256 checksum
How to use checksums
ff0b8864da1407131178736f2929f7f492fed0c58410919f8831f5c4f11e800a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.0 CPython/3.10.9

Release files / sfpl-1.6.2-py3-none-any.whl

Download URL sfpl-1.6.2-py3-none-any.whl
Size 17.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f19af1cd50520c2425b92915e628e32615a4e1ff209b369d1e73a376e7a442af
BLAKE2b-256 checksum
How to use checksums
d67c14f6331fcbe0f9912f9ea7d244ecf780a3869d4ae7ec490c9f611e6d8fe1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.0 CPython/3.10.9

Release history Release notifications | RSS feed

This release

1.6.2 This release

2 release files

1.6.1

2 release files

1.6.0

2 release files

1.5.7

2 release files

1.5.6

1 release file

1.5.5

1 release file

1.5.4

1 release file

1.5.3

1 release file

1.5.2

2 release files

1.5.1

2 release files

1.5

2 release files

1.4

2 release files

1.3

2 release files

1.2

2 release files

1.1.13

2 release files

1.1.11

2 release files

1.1.10

2 release files

1.1.9

2 release files

1.1.8

2 release files

1.1.7

1 release file

1.1.6

2 release files

1.1.5

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.0.2

2 release files

1.0.1

2 release files

1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page