Last active
November 29, 2017 18:38
-
-
Save eskerda/bbd65539048a53eadfccc5d535ad777e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
❯ termbike.py "36 Maple St, NY" | |
396 - Lefferts Pl & Franklin Ave | |
🚲 🚲 🚲 🚲 🚲 🚲 🚲 🚲 🚲 🚲 🚲 🚲 🚲 🚲 ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ | |
365 - Fulton St & Grand Ave | |
🚲 🚲 🚲 🚲 🚲 ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ | |
436 - Hancock St & Bedford Ave | |
🚲 🚲 🚲 🚲 🚲 ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ | |
437 - Macon St & Nostrand Ave | |
🚲 ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ | |
384 - Fulton St & Washington Ave | |
🚲 🚲 🚲 🚲 🚲 🚲 🚲 🚲 🚲 🚲 🚲 🚲 🚲 🚲 🚲 🚲 ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ ☮ | |
""" | |
import argparse | |
import itertools | |
from math import hypot | |
from googlegeocoder import GoogleGeocoder | |
import pybikes | |
geocoder = GoogleGeocoder() | |
argparser = argparse.ArgumentParser() | |
argparser.add_argument('address', metavar='address') | |
args = argparser.parse_args() | |
def geocode(address): | |
info = geocoder.get(args.address, language="en") | |
if not len(info): | |
raise Exception('Got no address like that') | |
return (info[0].geometry.location.lat, info[0].geometry.location.lng) | |
def dist(latlng, latlng2): | |
return hypot(latlng[0] - latlng2[0], latlng[1] - latlng2[1]) | |
def dist_sort(xy, locations, getter): | |
res = sorted( | |
map(lambda loc: [loc, dist(xy, getter(loc))], locations), | |
key=lambda loc_dst: loc_dst.pop(-1) | |
) | |
return list(itertools.chain(*res)) | |
def main(): | |
lat, lng = geocode(args.address) | |
instances = list(pybikes.get_instances()) | |
_, instance_data = dist_sort( | |
[lat, lng], instances, | |
lambda i: (i[1]['meta']['latitude'], i[1]['meta']['longitude']) | |
)[0] | |
instance = pybikes.get(instance_data['tag']) | |
instance.update() | |
s_stations = dist_sort( | |
[lat, lng], instance.stations, | |
lambda s: (s.latitude, s.longitude) | |
)[:5] | |
for station in s_stations: | |
print station.name | |
print "🚲 " * station.bikes + " ☮" * station.free | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice! I suggest you use vincenty instead of the hypothenuse, much more accurate.