This is the Python client library for interacting with information published on the BC Ferries mobile site. It is essentially a wrapper around a BeautifulSoup-powered scraper. Better documentation and tests are still in the works; feel free to contribute!
This library is used to power project like FerryTime. The source code for this library can be found at yasyf/bcferries on GitHub.
Installation
pip install bcferries
Setup
Some functions require interaction with a geocoding service; the Google Maps API is used for this. In order to prevent severe rate limiting, you’ll want to acquire an API key. To let bcferries know about this key, set it as the GOOGLE_MAPS_API_KEY environment variable. Alternatively, you can pass it as an optional keyword argument to the constructor.
from bcferries import BCFerries
bc = BCFerries(google_maps_api_key='xxx-xxx-xxx')
Usage
bc = BCFerries()
Terminals
bc.nearest_terminal('Qualicum Beach')
# BCFerriesTerminal(Nanaimo (Duke Pt))
terminals = bc.terminals()
# {u'Horseshoe Bay': BCFerriesTerminal(Horseshoe Bay), u'Tsawwassen': BCFerriesTerminal(Tsawwassen)}
t = terminals['Tsawwassen']
# BCFerriesTerminal(Tsawwassen)
t.updated_at()
# datetime.datetime(2014, 12, 29, 0, 4)
t.next_crossing()
# BCFerriesCrossing(Tsawwassen to Duke Point at 5:15am)
t.location().address
# u'Ferry Causeway, Delta, BC V4M, Canada'
Routes
routes = t.routes()
# {u'Tsawwassen to Duke Point': BCFerriesRoute(Tsawwassen to Duke Point)}
r = routes['Tsawwassen to Duke Point']
# BCFerriesRoute(Tsawwassen to Duke Point)
r.from_, r.to
# (BCFerriesTerminal(Tsawwassen), BCFerriesTerminal(Nanaimo (Duke Pt)))
r.distance()
# Distance(61.9591068557)
r.car_waits
# 0
Crossings
crossing = r.crossings()['10:45pm']
# BCFerriesCrossing(Tsawwassen to Duke Point at 5:45pm)
crossing.capacity
# BCFerriesCapacity(18% Full)
Schedules
schedule = r.scheduled('12:45 PM')
# BCFerriesScheduledCrossing(Queen of Alberni at 12:45 PM)
schedule.status
# u'On Time'
schedule.sailing_time
# datetime.timedelta(0, 7200)
schedule.is_late()
# False
schedule.is_departed()
# True
Fuzzy Results
All returned dictionaries have fuzzy string matching on they keys.
routes['Tsawwassen to Duke Point'] == routes['Tsaw to DP']
# True
There is also fuzzy time matching on keys that represent a nearby time.
r = routes['HBay to DBay']
schedule = r.schedule()
schedule['6:12 PM']
# BCFerriesScheduledCrossing(Queen of Cowichan at 6:30 PM)
datetime objects can also be used as keys.
from datetime import datetime
datetime.datetime.now()
# datetime.datetime(2014, 12, 28, 10, 42, 35, 630996)
schedule[datetime.datetime.now()]
# BCFerriesScheduledCrossing(Coastal Renaissance at 10:40 AM)
Caching
bcferries caches the 16 most used API calls for up to five minutes by default. You can change this behavior as below. This must be done before creating a BCFerries object.
import bcferries
import datetime
bcferries.set_cache_size(16)
bcferries.set_cache_timeout(datetime.timedelta(minutes=5))
You can also pass any function the ignore_cache keyword argument to bypass the cache, or call the flush_cache method on BCFerries to clear the entire cache.
terminals = bc.terminals() # initial call takes multiple seconds
terminals = bc.terminals() # repeated call returns almost instantly
terminals = bc.terminals(ignore_cache=True) # takes multiple seconds to return
bc.flush_cache() # wipes the cache
Export
You can export any subset of information with a call to to_dict on any object. You can also use to_fuzzy_dict and to_json as needed.
By default, complex objects which require further API calls will not be created, and only their names will be returned. You can disable this behavior with the shallow keyword argument. To export all available information, do this on a BCFerries instance, and be prepared to wait a while.
crossing.capacity
# BCFerriesCapacity(18% Full)
crossing.capacity.to_dict()
# {'passenger_filled': 32, 'mixed_filled': 4, 'name': '18% Full', 'filled': 18}
bc.to_dict() # quick
bc.to_dict(shallow=False) # takes all day
Metadata
Release files for bcferries 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| bcferries-0.1.2.tar.gz | 8.4 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| bcferries-0.1.2.macosx-10.9-x86_64.tar.gz | Details |
Total release size: 24.3 kB
Release files / bcferries-0.1.2.tar.gz
| Download URL | bcferries-0.1.2.tar.gz |
|---|---|
| Size | 8.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6dc06009fe870e49b0e39e786d1c2f06d59efd03f4139caa00b905d2f4c17cd0
|
|
BLAKE2b-256 checksum How to use checksums |
6aafb3e2041a5c1bb8674a3db85a6fcf8397ac31b99f4beed91d2a2ca923947f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / bcferries-0.1.2.macosx-10.9-x86_64.tar.gz
| Download URL | bcferries-0.1.2.macosx-10.9-x86_64.tar.gz |
|---|---|
| Size | 15.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c93209a750d7b23465fe4e4f3e307c0822cccfbb007102a26ca242c26f4f08eb
|
|
BLAKE2b-256 checksum How to use checksums |
545782bbe3d01ccbba3252081752ac107d43cb913cedf30a2be5513944e04f16
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for bcferries 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.