Package: bcferries
This is the Python client library for interacting with information
published on theBC 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 likeFerryTime. The source code for this library can
be found atyasyf/bcferrieson
GitHub.Installationpip install bcferriesSetupSome 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 letbcferriesknow about this
key, set it as theGOOGLE_MAPS_API_KEYenvironment variable.
Alternatively, you can pass it as an optional keyword argument to the
constructor.frombcferriesimportBCFerriesbc=BCFerries(google_maps_api_key='xxx-xxx-xxx')Usagebc=BCFerries()Terminalsbc.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'Routesroutes=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# 0Crossingscrossing=r.crossings()['10:45pm']# BCFerriesCrossing(Tsawwassen to Duke Point at 5:45pm)crossing.capacity# BCFerriesCapacity(18% Full)Schedulesschedule=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()# Falseschedule.is_departed()# TrueFuzzy ResultsAll returned dictionaries have fuzzy string matching on they keys.routes['Tsawwassen to Duke Point']==routes['Tsaw to DP']# TrueThere 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)datetimeobjects can also be used as keys.fromdatetimeimportdatetimedatetime.datetime.now()# datetime.datetime(2014, 12, 28, 10, 42, 35, 630996)schedule[datetime.datetime.now()]# BCFerriesScheduledCrossing(Coastal Renaissance at 10:40 AM)Cachingbcferriescaches 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 aBCFerriesobject.importbcferriesimportdatetimebcferries.set_cache_size(16)bcferries.set_cache_timeout(datetime.timedelta(minutes=5))You can also pass any function theignore_cachekeyword argument to
bypass the cache, or call theflush_cachemethod onBCFerriesto
clear the entire cache.terminals=bc.terminals()# initial call takes multiple secondsterminals=bc.terminals()# repeated call returns almost instantlyterminals=bc.terminals(ignore_cache=True)# takes multiple seconds to returnbc.flush_cache()# wipes the cacheExportYou can export any subset of information with a call toto_dicton
any object. You can also useto_fuzzy_dictandto_jsonas
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 theshallowkeyword argument. To export all available
information, do this on aBCFerriesinstance, 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()# quickbc.to_dict(shallow=False)# takes all day