def update_data(which_to_update): print('update_data called on : ' + str(which_to_update)) result = {} for value in which_to_update: print('going to get info on ' + value) try: bikeshare = pybikes.get(value) bikeshare.update() result[value] = { 'city': bikeshare.meta['city'], 'stations': {x.name: x for x in bikeshare.stations}, 'with_bikes': [ station for station in bikeshare.stations if station.bikes and station.bikes > 0 ], 'with_slots': [ station for station in bikeshare.stations if station.free and station.free > 0 ] } except Exception as e: print('something bad while getting info for ' + value + ': ' + str(e) + '\n, discarding this city') which_to_update.remove(value) return result """
def _test_instance_fields(self, _instance, schema, _class): self.longMessage = True # Some fields may be defined as class attributes and then passed into # instance meta, so we need to instantiate it # We do not really need keys here msg = 'File: %r' % schema self.assertIn('tag', _instance, msg=msg) self.assertIn('meta', _instance, msg=msg) self.assertRegexpMatches( _instance['tag'], r'^[a-z0-9\-]+$', msg="Instance tag should only contain lowercase alphanumeric " "characters and hypens: %s in %s" % (_instance['tag'], schema)) instance = pybikes.get(_instance['tag'], key='foobar') meta = instance.meta msg = 'instance {!r}. File: {}'.format(meta, schema) # Test bare minimum definitions of networks for field in ['latitude', 'longitude']: self.assertIn(field, meta, msg=('Missing %r on ' % field) + msg) self.assertIsInstance(meta[field], float, msg=('Error in %r on ' % field) + msg) for field in ['city', 'country', 'name']: self.assertIn(field, meta, msg=('Missing %r on ' % field) + msg) self.assertIsInstance(meta[field], basestring, msg=('Error in %r on ' % field) + msg) self.assertTrue(-90 <= meta['latitude'] <= 90, msg='Latitude outside -90, 90 range %s' % msg) self.assertTrue(-180 <= meta['longitude'] <= 180, msg='Longitude outside -180, 180 range %s' % msg)
def _test_instance_fields(self, _instance, schema, _class): self.longMessage = True # Some fields may be defined as class attributes and then passed into # instance meta, so we need to instantiate it # We do not really need keys here msg = 'File: %r' % schema self.assertIn('tag', _instance, msg=msg) self.assertIn('meta', _instance, msg=msg) self.assertTrue(_instance['tag'].islower()) instance = pybikes.get(_instance['tag'], key='foobar') meta = instance.meta msg = 'instance {!r}. File: {}'.format(meta, schema) # Test bare minimum definitions of networks for field in ['latitude', 'longitude']: self.assertIn(field, meta, msg=('Missing %r on ' % field) + msg) self.assertIsInstance(meta[field], float, msg=('Error in %r on ' % field) + msg) for field in ['city', 'country', 'name']: self.assertIn(field, meta, msg=('Missing %r on ' % field) + msg) self.assertIsInstance(meta[field], basestring, msg=('Error in %r on ' % field) + msg)
def _test_instance_fields(self, _instance, schema, _class): self.longMessage = True # Some fields may be defined as class attributes and then passed into # instance meta, so we need to instantiate it # We do not really need keys here msg = 'File: %r' % schema self.assertIn('tag', _instance, msg=msg) self.assertIn('meta', _instance, msg=msg) self.assertRegexpMatches( _instance['tag'], r'^[a-z0-9\-]+$', msg="Instance tag should only contain lowercase alphanumeric " "characters and hypens: %s in %s" % (_instance['tag'], schema) ) instance = pybikes.get(_instance['tag'], key='foobar') meta = instance.meta msg = 'instance {!r}. File: {}'.format( meta, schema ) # Test bare minimum definitions of networks for field in ['latitude', 'longitude']: self.assertIn(field, meta, msg=('Missing %r on ' % field) + msg) self.assertIsInstance(meta[field], float, msg=('Error in %r on ' % field) + msg) for field in ['city', 'country', 'name']: self.assertIn(field, meta, msg=('Missing %r on ' % field) + msg) self.assertIsInstance(meta[field], basestring, msg=('Error in %r on ' % field) + msg) self.assertTrue(-90 <= meta['latitude'] <= 90, msg='Latitude outside -90, 90 range %s' % msg) self.assertTrue(-180 <= meta['longitude'] <= 180, msg='Longitude outside -180, 180 range %s' % msg)
def run(self): print threading.current_thread() try: if self.provider == "enjoy": self.url_home = 'https://enjoy.eni.com/it/' + self.city + '/map/' self.start_session(self.url_home) self.url_data = 'https://enjoy.eni.com/ajax/retrieve_vehicles' elif self.provider == "car2go": self.url = 'https://www.car2go.com/api/v2.1/vehicles?oauth_consumer_key=car2gowebsite&format=json&loc='\ + self.city self.start_session(self.url) elif self.provider == "tobike": self.toBike = pybikes.get('to-bike') self.toBike.update() except: print "Session error!" while (True): try: self.get_data() write_log(self.provider + " " + self.city + ": state successfully loaded") except: write_log(self.provider + " " + self.city + ": HTTP error!") try: DBinsert(self.city, self.last_state) write_log(self.provider + " " + self.city + ": state successfully inserted") except: write_log(self.provider + " " + self.city + ": Database error!") try: with open(self.city + "_last_state.json", "w+") as outfile: outfile.write(json.dumps(self.last_state)) write_log(self.provider + " " + self.city + ": state successfully written") except: write_log(self.provider + " " + self.city + ": File write error!") time.sleep(300)
def _test_instance_fields(self, _instance, schema, _class): self.longMessage = True # Some fields may be defined as class attributes and then passed into # instance meta, so we need to instantiate it # We do not really need keys here msg = "File: %r" % schema self.assertIn("tag", _instance, msg=msg) self.assertIn("meta", _instance, msg=msg) instance = pybikes.get(_instance["tag"], key="foobar") meta = instance.meta msg = "instance {!r}. File: {}".format(meta, schema) # Test bare minimum definitions of networks for field in ["latitude", "longitude"]: self.assertIn(field, meta, msg=("Missing %r on " % field) + msg) self.assertIsInstance(meta[field], float, msg=("Error in %r on " % field) + msg) for field in ["city", "country", "name"]: self.assertIn(field, meta, msg=("Missing %r on " % field) + msg) self.assertIsInstance(meta[field], basestring, msg=("Error in %r on " % field) + msg)
def _test_system(self, tag, key=None): """ Tests okayness of a system: - Test if system can be updated - Tests okayness of 5 stations on the system """ p_sys = pybikes.get(tag, key) self._test_update(p_sys) station_string = "" if len(p_sys.stations) < 5: t_range = len(p_sys.stations) else: t_range = 5 for i in range(t_range): station_string += unichr(ord(u'▚') + i) sys.stdout.flush() sys.stdout.write('\r[%s] testing %d' % (station_string, i+1)) sys.stdout.flush() self._test_station_ok(p_sys, p_sys.stations[i]) sys.stdout.flush() sys.stdout.write('\r↑ stations look ok ↑ \n\n') sys.stdout.flush()
def get_data(self): if self.provider in ["enjoy", "car2go"]: request = self.session.get(self.url_data) current_state = json.loads(request.text) self.last_state = current_state elif self.provider in ["tobike"]: self.toBike = pybikes.get('to-bike') self.toBike.update() l = {} stations_dict = {} for i in range(len(self.toBike.stations)): l["name"] = self.toBike.stations[i].name l["extra"] = self.toBike.stations[i].extra l["timestamp"] = str(self.toBike.stations[i].timestamp) l["free"] = self.toBike.stations[i].free l["bikes"] = self.toBike.stations[i].bikes l["longitude"] = self.toBike.stations[i].longitude l["latitude"] = self.toBike.stations[i].latitude stations_dict[i] = l l = {} self.last_state = json.dumps(stations_dict)
def _test_system(self, tag, key=None): """ Tests okayness of a system: - Test if system can be updated - Tests okayness of 5 stations on the system """ p_sys = pybikes.get(tag, key) print(u"Testing {!r}, {!r}".format(p_sys.meta["name"], p_sys.meta.get("city"))) self._test_update(p_sys) station_string = "" if len(p_sys.stations) < 5: t_range = len(p_sys.stations) else: t_range = 5 for i in range(t_range): station_string += unichr(ord(u"▚") + i) sys.stdout.flush() sys.stdout.write("\r[%s] testing %d" % (station_string, i + 1)) sys.stdout.flush() self._test_station_ok(p_sys, p_sys.stations[i]) sys.stdout.flush() sys.stdout.write("\r↑ stations look ok ↑ \n\n") sys.stdout.flush()
def _test_instance_fields(self, _instance, schema, _class): self.longMessage = True # Some fields may be defined as class attributes and then passed into # instance meta, so we need to instantiate it # We do not really need keys here msg = 'File: %r' % schema self.assertIn('tag', _instance, msg=msg) self.assertIn('meta', _instance, msg=msg) instance = pybikes.get(_instance['tag'], key='foobar') meta = instance.meta msg = 'instance {!r}. File: {}'.format( meta, schema ) # Test bare minimum definitions of networks for field in ['latitude', 'longitude']: self.assertIn(field, meta, msg=('Missing %r on ' % field) + msg) self.assertIsInstance(meta[field], float, msg=('Error in %r on ' % field) + msg) for field in ['city', 'country', 'name']: self.assertIn(field, meta, msg=('Missing %r on ' % field) + msg) self.assertIsInstance(meta[field], basestring, msg=('Error in %r on ' % field) + msg)
# PARSE KNOWN NETWORKS FROM FILE # networks_dot_json = os.path.join(os.path.dirname(__file__), "fixtures/citybikes_api/get_networks.json") with open(networks_dot_json) as json_file: station_id = 0 known_networks = json.load(json_file) for known_network in known_networks: network_tag = known_network["tag"].encode() # # GET NETWORK .JSON # try: response = pybikes.get(network_tag) except: continue # workaround for `Exception: System Cyclocity needs a key to work` try: network_city_name = response.meta["city"].encode() except UnicodeEncodeError: network_city_name = "#UNENCODABLE" try: network_name = response.meta["name"].encode() except UnicodeEncodeError: network_name = "#UNENCODABLE" except UnicodeDecodeError: network_name = "#UNDECODABLE"
import pybikes import pandas as pd import time import datetime import requests bicing = pybikes.get('bicing') print(bicing.meta) print(len(bicing.stations)) bicing.update() url = 'https://www.bicing.barcelona/get-stations' print(requests.get(url).status_code) print(len(bicing.stations)) print(type(bicing.stations[0])) today = str(datetime.date.today()) print(today) file_name = "bicing" + today + ".csv" print(file_name) stations = [] bicing.update() for station in bicing.stations: name = station.name lat = station.latitude lon = station.longitude uid = station.extra['uid'] bikes = station.bikes free_slots = station.free
def getBikeData(): bixi = pybikes.get('bixi-montreal') bixi.update() return bixi
d2 = (user_position['latitude']-val.latitude) **2 + (user_position['longitude']-val.longitude) **2 if d2 < distance_sq: distance_sq = d2 best = idx return results_set[best] # load the token from file with open('../tokens.json') as tokens_file: data = json.load(tokens_file) telegram_token = data['telegram'] # TODO enable this fro nlp stuff. Now only dealing with fixed queries #nlp = spacy.load('en') torino_bikeshare = pybikes.get('to-bike') torino_bikeshare.update() torino_stations = {x.name:x for x in torino_bikeshare.stations} stations_with_bikes = [station for station in torino_bikeshare.stations if station.bikes>0] stations_with_free = [station for station in torino_bikeshare.stations if station.free>0] # TODO persistency user_positions = {} bot = telepot.Bot(telegram_token) pprint(bot.getMe()) bot.message_loop({'chat': on_chat_message}) while 1: # keep updating the bike-sharing data every 5 min TODO more fine-grained update time.sleep(60*5)