apparentlymart / python-nextbus

A Python library for connecting to the NextBus public API
18 stars 7 forks source link

Confusion on using classes #4

Closed IgnorantGeek closed 5 years ago

IgnorantGeek commented 5 years ago

I know this is a very old repo, and I read that you were currently busy, but I am confused on how to use the classes that you have made. I can print the results of get_predictions_for_stop to the command line but I don't know how to pull the relevant information from the returned result. I tried to scan it as you would with an xml but the get_predictions method returns a instance of Predictions and not a string. I am pretty new to working with python, I usually use Java or C but I wanted to expand my exposure to different languages. If you could help me figure out my confusion that would be great as I have been stuck for quite some time. I really just want to display the arrival times of a bus stop near me to some screen, but again I don't know how to manipulate the data returned by get_predictions. Thanks in advance and thank you for your code!

apparentlymart commented 5 years ago

Hi @IgnorantGeek,

As you noted, this is some very old code that I've not looked at for a long time, so I'm not sure of the details myself, but I spent a little time re-learning it by writing a simple script that prints out some details from the NextBus API:

import nextbus
from pprint import pprint

print "# Agencies\n"
agencies = nextbus.get_all_agencies()
for agency in agencies:
    print "- {0} in {1} has tag {2!r}".format(agency.title, agency.region_title, agency.tag)

print "\n# Routes for Portland Streetcar\n"
routes = nextbus.get_all_routes_for_agency('portland-sc')
for route in routes:
    print "- Route {0} has tag {1!r}".format(route.title, route.tag)

print "\n# Stops on the Portland Streetcar B Loop\n"
route_config = nextbus.get_route_config('portland-sc', '195')
for stop in route_config.stops:
    print "- Stop at {0} has tag {1!r}".format(stop.title, stop.tag)

print "\n# Predicted Arrivals at NE Grand & Broadway\n"

predictions = nextbus.get_predictions_for_stop('portland-sc', '13617')
for prediction in predictions.predictions:
    route_title = prediction.direction.route.title
    if prediction.is_departing:
        print "- Route {0} is departing now".format(route_title)
    else:
        print "- Route {0} will arrive in {1} minutes".format(route_title, prediction.minutes)

For me, that printed out the following:

# Agencies

- AC Transit in California-Northern has tag 'actransit'
- APL in Maryland has tag 'jhu-apl'
- Asheville Redefines Transit in North Carolina has tag 'art'
- Atlanta Streetcar - Beta in Georgia has tag 'atlanta-sc'
- Big Blue Bus in California-Southern has tag 'bigbluebus'
- Brockton Area Transit Authority in Massachusetts has tag 'brockton'
- Camarillo Area (CAT) in California-Southern has tag 'camarillo'
- Cape Cod Regional Transit Authority in Massachusetts has tag 'ccrta'
- Chapel Hill Transit in North Carolina has tag 'chapel-hill'
- Charm City Circulator in Maryland has tag 'charm-city'
- City College NYC in New York has tag 'ccny'
- City of Oxford in Mississippi has tag 'oxford-ms'
- City of West Hollywood in California-Southern has tag 'west-hollywood'
- Config Stuff in Other has tag 'configdev'
- CyRide in Iowa has tag 'cyride'
- DC Circulator in District of Columbia has tag 'dc-circulator'
- DC Streetcar in District of Columbia has tag 'dc-streetcar'
- Downtown Connection in New York has tag 'da'
- Downtown Connection in New York has tag 'dta'
- Dumbarton Express in California-Northern has tag 'dumbarton'
- EZRide - Charles River TMA in Massachusetts has tag 'charles-river'
- East Carolina University in North Carolina has tag 'ecu'
- Escalon eTrans in California-Northern has tag 'escalon'
- FAST in California-Northern has tag 'fast'
- Fairfax (CUE) in Virginia has tag 'fairfax'
- Foothill Transit in California-Southern has tag 'foothill'
- Glendale Beeline in California-Southern has tag 'glendale'
- Gold Coast Transit in California-Southern has tag 'south-coast'
- Indianapolis International Airport in Indiana has tag 'indianapolis-air'
- JFK International Airport in New York has tag 'jfk'
- Jacksonville Transportation Authority in Florida has tag 'jtafla'
- LaGuardia Airport Employee Shuttle in New York has tag 'laguardia'
- LaGuardia Airport Shuttle in New York has tag 'lga'
- Los Angeles Metro in California-Southern has tag 'lametro'
- Los Angeles Rail in California-Southern has tag 'lametro-rail'
- Manteca Transit in California-Northern has tag 'manteca-transit'
- Massachusetts Institute of Technology in Massachusetts has tag 'mit'
- Moorpark Transit in California-Southern has tag 'moorpark'
- Newark Liberty International Airport in New Jersey has tag 'ewr'
- Nova Southeastern University in Florida has tag 'nova-se'
- Omnitrans in California-Southern has tag 'omnitrans'
- Palos Verdes Transit in California-Southern has tag 'pvpta'
- Pensacola Beach (SRIA) in Florida has tag 'sria'
- Plymouth State University in New Hampshire has tag 'psu'
- Portland Streetcar in Oregon has tag 'portland-sc'
- Prince Georges County in Maryland has tag 'pgc'
- RTC RIDE, Reno in Nevada has tag 'reno'
- Radford Transit in Virginia has tag 'radford'
- Roosevelt Island in New York has tag 'roosevelt'
- Rutgers Univ. Newark College Town Shuttle in New Jersey has tag 'rutgers-newark'
- Rutgers University in New Jersey has tag 'rutgers'
- San Francisco Muni in California-Northern has tag 'sf-muni'
- Seattle Streetcar in Washington has tag 'seattle-sc'
- Simi Valley (SVT) in California-Southern has tag 'simi-valley'
- Societe de transport de Laval in Quebec has tag 'stl'
- Sonoma County Transit in California-Northern has tag 'sct'
- Spokane International Airport in Washington has tag 'geg'
- Tahoe Truckee Area Regional Transit in California-Northern has tag 'tahoe'
- Thousand Oaks Transit (TOT) in California-Southern has tag 'thousand-oaks'
- Toronto Transit Commission in Ontario has tag 'ttc'
- Trinity Metro in Texas has tag 'ft-worth'
- UCLA in California-Southern has tag 'bruinbus'
- UCLA in California-Southern has tag 'ucla'
- Unitrans ASUCD/City of Davis in California-Northern has tag 'unitrans'
- University of California Berkeley in California-Northern has tag 'ucb'
- University of Maryland in Maryland has tag 'umd'
- Vacaville City Coach in California-Northern has tag 'vacaville'
- Ventura Intercity (VCTC) in California-Southern has tag 'vista'
- Western Kentucky University in Kentucky has tag 'wku'
- Winston-Salem in North Carolina has tag 'winston-salem'

# Routes for Portland Streetcar

- Route NS North/South has tag '193'
- Route NS Yard has tag '193Yard'
- Route A Loop has tag '194'
- Route B Loop has tag '195'
- Route B NW has tag '195NW'
- Route B Yard has tag '195Yard'

# Stops on the Portland Streetcar B Loop

- Stop at SW 5th & Market has tag '10762'
- Stop at NW 11th & Marshall has tag '13620_ar'
- Stop at NE Grand & Multnomah has tag '9343'
- Stop at SE Grand & Belmont has tag '11484'
- Stop at SE Grand & Taylor has tag '11483'
- Stop at SW 11th & Taylor has tag '9633'
- Stop at NE Broadway & 2nd has tag '13618'
- Stop at SW 3rd & Harrison has tag '12375'
- Stop at SW Harrison Street has tag '12377'
- Stop at SW River Pkwy & Moody has tag '12378'
- Stop at SE Water/OMSI has tag '13615'
- Stop at SE Grand & Hawthorne has tag '13616'
- Stop at NE Grand & Broadway has tag '13617'
- Stop at SW Park & Market has tag '11011'
- Stop at SW 11th & Clay has tag '10760'
- Stop at SW 5th & Montgomery has tag '10763'
- Stop at SE Grand & Mill has tag '2171'
- Stop at NE Grand & Holladay has tag '2175'
- Stop at SW 11th & Alder has tag '9600'
- Stop at NE Broadway & Ross has tag '13619'
- Stop at SE Grand & Stark has tag '13597'
- Stop at SW Moody & Meade South has tag '13601'
- Stop at NW 11th & Marshall has tag '13620'
- Stop at NW 10th & Northrup has tag '13604'
- Stop at SW 11th & Jefferson has tag '10759'
- Stop at NE Grand & Hoyt has tag '2169'
- Stop at SE Grand & E Burnside has tag '2167'
- Stop at NW 11th & Johnson has tag '10753'
- Stop at NW 11th & Glisan has tag '10754'
- Stop at NW 11th & Couch has tag '10756'

# Predicted Arrivals at NE Grand & Broadway

- Route B Loop will arrive in 6 minutes
- Route B Loop will arrive in 23 minutes
- Route B Loop will arrive in 46 minutes
- Route B Loop will arrive in 64 minutes
- Route B Loop will arrive in 82 minutes

For your goal of printing out the predictions at a single stop, you can generally just hard-code the agency and stop id once you have learned them, and so you'll only need the last past of this that prints out the predicted arrivals.

To understand what is available on each object, the easiest thing is to use the interactive Python interpreter and call the help(...) function, like this:

$ python
Python 2.7.12 (default, Nov 12 2018, 14:36:49) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import nextbus
>>> routes = nextbus.get_all_routes_for_agency('portland-sc')
>>> help(routes[0])
Help on instance of Route in module nextbus:

class Route
 |  Methods defined here:
 |  
 |  __init__ = auto_init(self, **kwargs)
 |  
 |  __repr__ = _standard_repr(self)
 |  
 |  ----------------------------------------------------------------------
 |  Class methods defined here:
 |  
 |  from_elem(cls, elem) from __builtin__.classobj
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  tag = None
 |  
 |  title = None

For this nextbus module in particular, the data is generally exposed just as attributes, so you can see the names under "Data and other attributes defined here". I didn't document these well because I wrote this library primarily for my own use in implementing something a lot like what you are trying to do: a sign in my house to show me departures for nearby stops. However, you're welcome to use it if you find it useful, of course!

I won't be able to help any more than this because I no longer live in an area served by NextBus and so it's been several years since I've worked with it. You may find it easier to use one of the other NextBus libraries on PyPI; I don't know what their quality level is like, but the fact that their authors chose to publish them on PyPI suggests that they intended them for others to use, vs. my code here which I wrote primarily for myself.

I hope that helps. Good luck with your project!

IgnorantGeek commented 5 years ago

Wow! This is way more than I could have ever expected. This should help me greatly as it gives me some examples to learn from. I really appreciate you taking the time to respond to my thread, I have been pulling my hair out trying to understand what exactly I am confused about. This is going to make my implementation very easy, so I can't thank you enough for your help!!