Exemplo n.º 1
0
 def test_network_json(self):
     responses.add(responses.GET,
                   'https://api.citybik.es/v2/networks/foo',
                   json=mockresponses.network)
     client = citybikes.Client()
     network = citybikes.Network(client, uid='foo')
     assert json.dumps(network, cls=citybikes.resource.JSONEncoder)
Exemplo n.º 2
0
 def test_network_by_uid(self):
     responses.add(responses.GET,
                   'https://api.citybik.es/v2/networks/foo',
                   json=mockresponses.network)
     client = citybikes.Client()
     foo = citybikes.Network(client, uid='foo')
     assert foo.data == mockresponses.network['network']
Exemplo n.º 3
0
    def state_search(self, user, msg, args):
        '''
        The search state function. Use the location in the args to search the 
        nearest bike station. Send the information of the bike station to the 
        user and return the enum of the end state function.
        '''

        location_s = args[0]
        lat = args[1]['latitude']
        lon = args[1]['longitude']
        posi1 = (lat, lon)

        try:
            client = citybikes.Client()
            net, dist = next(iter(client.networks.near(lat, lon)))
            sts = net.stations.near(lat, lon)
        except:
            print("Error accessing citybikes information")
            self.tb.send_message(
                user, "Sorry the citybikes network currently"
                "is not operating. Can't access the bike station information. :("
            )
            return [Default.END, None]

        for stai in sts:
            if msg['text'] == 'PickUp' and stai[0]['free_bikes'] != 0:
                self.tb.send_location(user, \
                    stai[0]['latitude'], stai[0]['longitude'])

                posi2 = (stai[0]['latitude'], stai[0]['longitude'])
                distval = vincenty(posi1, posi2).meters
                distS = "{:1.1f}".format(distval)
                num = stai[0]['free_bikes']
                emoji = u"\U0001F6B2" * min(10, num)
                self.tb.send_message(user, "The station {name} has {count} "\
                    "free bikes.\n{bikes}\nIt is {dist} meters away from {targetloca}."\
                    .format(name=stai[0]['name'], count=num,\
                     dist=distS, targetloca=location_s, bikes=emoji))

                return [Default.END, None]

            if msg['text'] == 'DropOff' and stai[0]['empty_slots'] != 0:
                self.tb.send_location(user, \
                    stai[0]['latitude'], stai[0]['longitude'])

                posi2 = (stai[0]['latitude'], stai[0]['longitude'])
                distval = vincenty(posi1, posi2).meters
                distS = "{:.1f}".format(distval)
                num = stai[0]['empty_slots']
                emoji = u"\U0001F17F" * min(10, num)
                self.tb.send_message(user, "The station {name} has {count}"\
                    " empty slots.\n{slots}\nIt is {dist} meters away from {targetloca}."\
                    .format(name=stai[0]['name'], count=stai[0]['empty_slots'],\
                     dist=distS, targetloca=location_s, slots=emoji))

                return [Default.END, None]

        self.tb.send_message(user, "Sorry no available stations.")

        return [Default.END, None]
Exemplo n.º 4
0
 def test_stations(self):
     responses.add(responses.GET,
                   'https://api.citybik.es/v2/networks/foo',
                   json=mockresponses.network)
     client = citybikes.Client()
     foo = citybikes.Network(client, uid='foo')
     stations = list(foo.stations)
     for s in stations:
         assert isinstance(s, citybikes.Station)
Exemplo n.º 5
0
 def test_networks(self):
     responses.add(responses.GET,
                   'https://api.citybik.es/v2/networks',
                   json=mockresponses.networks)
     client = citybikes.Client()
     networks = list(client.networks)
     assert len(networks) == 3
     assert client.networks.data == mockresponses.networks
     for n in networks:
         assert isinstance(n, citybikes.Network)
Exemplo n.º 6
0
 def test_near_networks(self):
     responses.add(responses.GET,
                   'https://api.citybik.es/v2/networks',
                   json=mockresponses.networks)
     client = citybikes.Client()
     battery = [
         (client.networks.near(0.0, 0.0), ['baz', 'bar', 'foo']),
         (client.networks.near(30.0, 1.0), ['bar', 'baz', 'foo']),
         (client.networks.near(100.0, 100.0), ['foo', 'bar', 'baz']),
     ]
     for nets, expected in battery:
         assert [n['id'] for n, dist in nets] == expected
Exemplo n.º 7
0
 def test_near_stations(self):
     responses.add(responses.GET,
                   'https://api.citybik.es/v2/networks/foo',
                   json=mockresponses.network)
     client = citybikes.Client()
     network = citybikes.Network(client, uid='foo')
     battery = [
         (network.stations.near(0.0, 0.0), [1, 2, 3]),
         (network.stations.near(25.0, 25.0), [2, 3, 1]),
         (network.stations.near(100.0, 100.0), [3, 2, 1]),
     ]
     for nets, expected in battery:
         assert [n['id'] for n, dist in nets] == expected
Exemplo n.º 8
0
 def test_requests_saved(self):
     responses.add(responses.GET,
                   'https://api.citybik.es/v2/networks',
                   json=mockresponses.networks)
     nets_url_re = re.compile(
         r'https:\/\/api\.citybik\.es\/v2\/networks\/.+')
     responses.add(responses.GET, nets_url_re, json=mockresponses.network)
     client = citybikes.Client()
     list(client.networks)
     list(client.networks)
     assert len(responses.calls) == 1
     net = next(iter(client.networks))
     assert len(responses.calls) == 1
     list(net.stations)
     list(net.stations)
     list(net.stations)
     assert len(responses.calls) == 2
Exemplo n.º 9
0
# Payload as JSON
#

import citybikes
import paho.mqtt.publish as publish
import paho.mqtt.client as mqtt
import simplejson as json
from datetime import datetime

mqtt_host = "localhost"
MQTT_AUTH = {'username': '******', 'password': '******'}

# Check City Bikes API docs
networkuid = 'ford-gobike'

client = citybikes.Client()
network = citybikes.Network(client, uid=networkuid)
network = network['stations']

myStations = ["8302e5d11a6c84bac27a774de41474f9", \
               "c2648d0ffed4d1f710ccaf79e9a304fa", \
               "3f5c0437929a0f6dbc32f116850a4196", \
               "36aa8ab83177d5f5beb83685c7f645d3"]

for station in network:
    for id in myStations:
        if id == station['id']:
            percentFull = int(((station['free_bikes'] / float(
                (station['free_bikes'] + station['empty_slots']))) * 100))
            topic = "CityBikes/" + station['name']
            payload = json.dumps({  'free_bikes':station['free_bikes'], \
Exemplo n.º 10
0
 def test_network_json(self):
     responses.add(responses.GET,
                   'https://api.citybik.es/v2/networks',
                   json=mockresponses.networks)
     client = citybikes.Client()
     assert json.dumps(client.networks, cls=citybikes.resource.JSONEncoder)
Exemplo n.º 11
0
 def test_user_agent(self):
     responses.add(responses.GET, 'http://example.com')
     client = citybikes.Client()
     resp = client.request('http://example.com', method='GET')
     assert client.user_agent in resp.request.headers['User-Agent']
Exemplo n.º 12
0
 def test_custom(self):
     client = citybikes.Client(endpoint='http://foobar.com',
                               user_agent='Walrus 9000')
     assert client.endpoint == 'http://foobar.com'
     assert client.user_agent == 'Walrus 9000'
Exemplo n.º 13
0
 def test_defaults(self):
     client = citybikes.Client()
     assert client.endpoint == client.DEFAULT_ENDPOINT
     assert client.user_agent == client.USER_AGENT
     client.networks
Exemplo n.º 14
0
    def state_search(self, user, msg, args):
        '''
        The search state function. If the user replied 'WrongAddress', send 
        message to request the address again and return to the enum of the 
        respond state. Otherwise, search the nearest bike station and send the 
        information to the user. Return the enum of the end state.
        '''

        if msg['text'] == 'WrongAddress':
            self.tb.send_message(user, "Please enter the address.")
            return [Address.RESPOND, None]

        try:
            client = citybikes.Client()
            net, dist = next(
                iter(client.networks.near(args['latitude'],
                                          args['longitude'])))
            posi1 = (args['latitude'], args['longitude'])
            sts = net.stations.near(args['latitude'], args['longitude'])
        except:
            print("Error accessing citybikes information")
            self.tb.send_message(
                user, "Sorry the citybikes network currently"
                "is not operating. Can't access the bike station information. :("
            )
            return [Address.END, None]

        for stai in sts:
            if msg['text'] == 'DropOff' and stai[0]['empty_slots'] != 0:
                self.tb.send_location(\
                    user, stai[0]['latitude'], stai[0]['longitude'])

                posi2 = (stai[0]['latitude'], stai[0]['longitude'])
                distval = vincenty(posi1, posi2).meters
                distS = "{:.1f}".format(distval)
                num = stai[0]['empty_slots']
                emoji = u"\U0001F17F" * min(10, num)
                self.tb.send_message(user, "The station {name} has {count} "\
                    "empty slots.\n{bikes}\nIt is {dist} meters away from the address."\
                    .format(name=stai[0]['name'], count=stai[0]['empty_slots'],\
                     dist=distS, bikes=emoji))
                return [Address.END, None]

            if msg['text'] == 'PickUp' and stai[0]['free_bikes'] != 0:
                self.tb.send_location(\
                    user, stai[0]['latitude'], stai[0]['longitude'])

                posi2 = (stai[0]['latitude'], stai[0]['longitude'])
                distval = vincenty(posi1, posi2).meters
                distS = "{:.1f}".format(distval)
                num = stai[0]['free_bikes']
                emoji = u"\U0001F6B2" * min(10, num)
                self.tb.send_message(user, "The station {name} has {count} "\
                    "free bikes.\n{slots}\nIt is {dist} meters away from the address."\
                    .format(name=stai[0]['name'], count=stai[0]['free_bikes'],\
                     dist=distS, slots=emoji))
                return [Address.END, None]

        self.tb.send_message(user, "Sorry no available stations.")

        return [Address.END, None]