Skip to main content

A python library for scraping bike sharing data

Project description

pybikes Build Status Chat on Matrix

pybikes

pybikes provides a set of tools to scrape bike sharing data from different websites and APIs, thus providing a coherent and generalized set of classes and methods to access this sort of information.

The library is distributed and intended mainly for statistics and data sharing projects. More importantly, it powers the CityBikes project, and is composed of a set of classes and a pack of data files that provide instances for all different systems.

Installation

Install from PyPI:

pip install pybikes

Install directly from GitHub:

pip install git+https://github.com/eskerda/pybikes.git

Or after downloading/cloning the source:

pip install .

The following dependencies are required (example using Ubuntu package manager):

sudo apt-get install python
sudo apt-get install python-setuptools
sudo apt-get install libxml2 libxml2-dev libxslt1-dev libgeos-dev

Usage

>>> import pybikes

# Capital BikeShare instantiation data is in bixi.json file
>>> capital_bikeshare = pybikes.get('capital-bikeshare')

# The instance contains all possible metadata regarding this system
>>> print(capital_bikeshare.meta)
{
    'name': 'Capital BikeShare',
    'city': 'Washington, DC - Arlington, VA',
    'longitude': -77.0363658,
    'system': 'Bixi',
    'company': ['PBSC'],
    'country': 'USA',
    'latitude': 38.8951118
}
# The update method retrieves the list of stations
>>> print(len(capital_bikeshare.stations))
0
>>> capital_bikeshare.update()
>>> print(len(capital_bikeshare.stations))
191
>>> print(capital_bikeshare.stations[0])
--- 31000 - 20th & Bell St ---
bikes: 7
free: 4
latlng: 38.8561,-77.0512
extra: {
    'installed': True,
    'uid': 1,
    'locked': False,
    'removalDate': '',
    'installDate': '1316059200000',
    'terminalName': '31000',
    'temporary': False,
    'name': '20th & Bell St',
    'latestUpdateTime': '1353454305589'
}

Some systems might require an API key to work (for instance, Cyclocity). In these cases, the instance factory can take an extra API key parameter.

>>> key = "This is not an API key"
>>> dublinbikes = pybikes.get('dublinbikes', key)

Note that pybikes works as an instance factory and, choicely, instances can be generated by passing the right arguments to the desired class

>>> from pybikes.cyclocity import BixiSystem
>>> capital_bikeshare = BixiSystem(
        tag = 'foo_tag',
        root_url = 'http://capitalbikeshare.com/data/stations/',
        meta = {'foo':'bar'}
    )

The way information is retrieved can be tweaked using the PyBikesScraper class included on the utils module thus allowing session reusing and niceties such as using a proxy. This class uses Requests module internally.

>>> scraper = pybikes.utils.PyBikesScraper()
>>> scraper.enableProxy()
>>> scraper.setProxies({
        "http" : "127.0.0.1:8118",
        "https": "127.0.0.1:8118"
    })
>>> scraper.setUserAgent("Walrus™ v3.0")
>>> scraper.headers['Foo'] = 'bar'
>>> capital_bikeshare.update(scraper)

Tests

Tests are separated between unit tests and integration tests with the different sources supported.

To run unit tests simply

make test

To run integration tests

make test-update

Note that some systems require authorization keys, tests expect these to be set as environment variables like:

PYBIKES_CYCLOCITY='some-api-key'
PYBIKES_DEUTSCHEBAHN_CLIENT_ID='some-client-id'
PYBIKES_DEUTSCHEBAHN_CLIENT_SECRET='some-client-secret'

# or if using an .env file
# source .env

make test-update

This project uses pytest for tests. Test a particular network by passing a filter expresson

pytest -k bicing
pytest -k gbfs

To speed up tests execution, install pytest-xdist to specify the number of CPUs to use

pytest -k gbfs -n auto

To use Makefile steps and pass along pytest arguments, append to the T_FLAGS variable

make test-update T_FLAGS+='-n 10 -k gbfs'

Integration tests can generate a json report file with all extracted data stored as geojson. Using this json report file, further useful reports can be generated like a summary of the overall health of the library or a map visualization of all the information.

For more information on reports see utils/README.md

Development

We welcome contributions from the community! The best place to get started is by diving into the codebase or checking the issues list.

Join our developer community on Matrix: #citybikes:matrix.org

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

pybikes-1.0.1.tar.gz (462.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pybikes-1.0.1-py3-none-any.whl (488.3 kB view details)

Uploaded Python 3

File details

Details for the file pybikes-1.0.1.tar.gz.

File metadata

  • Download URL: pybikes-1.0.1.tar.gz
  • Upload date:
  • Size: 462.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pybikes-1.0.1.tar.gz
Algorithm Hash digest
SHA256 ace80ae841d7cceccd6f988a40e8a9e5cfe7b410e03efda4afcf2cc4691c4c6d
MD5 d96e166527371620ccc78b47d1b42e40
BLAKE2b-256 3770a2e72a229eb728748a36b2e552b052d9e486e211fe4bf73a1602c097fea3

See more details on using hashes here.

Provenance

The following attestation bundles were made for pybikes-1.0.1.tar.gz:

Publisher: release.yml on eskerda/pybikes

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pybikes-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: pybikes-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 488.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pybikes-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fe642f32f5f482b4ea0e2ccb1eee5e93773db730c8da4ffe3bd1336535957a53
MD5 a15598d2db89ffc2d5205f65aaf034c5
BLAKE2b-256 38f8aa43493c854a71e40a7d9ed9e627734103a240022602ad1f949c78b0c9e4

See more details on using hashes here.

Provenance

The following attestation bundles were made for pybikes-1.0.1-py3-none-any.whl:

Publisher: release.yml on eskerda/pybikes

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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