Skip to main content

BC Ferries Python Library

Project description

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! 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. To export all available information, call any of these methods 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}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bcferries-0.0.10.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

bcferries-0.0.10.macosx-10.9-x86_64.tar.gz (14.8 kB view details)

Uploaded Source

File details

Details for the file bcferries-0.0.10.tar.gz.

File metadata

  • Download URL: bcferries-0.0.10.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for bcferries-0.0.10.tar.gz
Algorithm Hash digest
SHA256 cfee743f3c6ad56432b0e715f23e2d3534998f6cff74221010a1d0ac6cfd959f
MD5 d2338009761d2641107ff253fd752e21
BLAKE2b-256 e1b8109cc6f74a822ef667b6ea7c71d27e01964488ae09bc2c91b92ca9db430e

See more details on using hashes here.

File details

Details for the file bcferries-0.0.10.macosx-10.9-x86_64.tar.gz.

File metadata

File hashes

Hashes for bcferries-0.0.10.macosx-10.9-x86_64.tar.gz
Algorithm Hash digest
SHA256 0327d42dc8de391bdb90edb8217fb2e7c387bada9014603052998c1bd6408360
MD5 c5860ff60af5a7a11d02f4d6a5562f50
BLAKE2b-256 e975afe7ba291a8607583d9ebeb50f2904e7996415b6f2936c5a58efec929325

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page