diff --git a/irail/api.py b/irail/api.py index 9141e5c..2ec7adc 100644 --- a/irail/api.py +++ b/irail/api.py @@ -20,10 +20,10 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. -import urllib2 -from model import * -from format import * -from exception import * +import urllib.request, urllib.error, urllib.parse +from .model import * +from .format import * +from .exception import * #curl 'api.irail.be/liveboard/?station=gentbruggee&format=json' @@ -65,11 +65,11 @@ def do_request(self, method, args=None): url += "?format=" + str(self.format()) url += "&lang=" + self.lang() if args: - for key in args.keys(): + for key in list(args.keys()): url += "&" + key + "=" + args[key] try: - return urllib2.urlopen(url) - except urllib2.HTTPError as e: + return urllib.request.urlopen(url) + except urllib.error.HTTPError as e: if e.code >= 400 and e.code < 500: raise ClientError(e) elif e.code >= 500 and e.code < 500: diff --git a/irail/format.py b/irail/format.py index 2d30726..94baabc 100644 --- a/irail/format.py +++ b/irail/format.py @@ -22,7 +22,7 @@ import json -from model import * +from .model import * class JsonFormat: @@ -55,7 +55,7 @@ def __convert_schedule_list(self, dict): def __convert_schedule(self, dict): departure = self.__convert_departure(dict['departure']) - if dict.has_key('vias'): + if 'vias' in dict: vias = self.__convert_vias(dict['vias']) else: vias = None @@ -71,7 +71,7 @@ def __convert_connection_event(self, dict, clazz): time = dict['time'] delay = dict['delay'] vehicle = dict['vehicle'] - direction = self.__convert_station(dict['direction']) + direction = Station(dict['direction']['name'], *[None]*4) # Direction is not a real station return clazz(station, platform, time, delay, vehicle, direction) def __convert_arrival(self, dict): diff --git a/irail/model.py b/irail/model.py index 752559d..29b7f16 100644 --- a/irail/model.py +++ b/irail/model.py @@ -32,7 +32,7 @@ def __new__(cls, d): return [ObjectFactory(e) for e in d] #TODO add tuple, set, frozenset elif isinstance(d, dict): obj = object.__new__(cls) - for k, v in d.iteritems(): + for k, v in d.items(): setattr(obj, k, ObjectFactory(v)) return obj else: @@ -40,7 +40,7 @@ def __new__(cls, d): def __repr__(self): return '<%s>' % str('\n '.join('%s : %s' % - (k, repr(v)) for (k, v) in self.__dict__.iteritems())) + (k, repr(v)) for (k, v) in self.__dict__.items())) class ResultList: def __init__(self, timestamp, version): diff --git a/test.py b/test.py index 93a3df5..4c07e24 100755 --- a/test.py +++ b/test.py @@ -27,35 +27,35 @@ def test_getstations(): api = iRailAPI() r = api.get_stations() - print "Version: " + r.version() - print "Timestamp: " + r.timestamp() + print("Version: " + r.version()) + print("Timestamp: " + r.timestamp()) for station in r.stations(): - print station + print(station) def test_searchstations(): api = iRailAPI() stations = api.search_stations("bru") - print "--- Stations starting with bru ---" + print("--- Stations starting with bru ---") for station in stations: - print station + print(station) def test_getschedules(): api = iRailAPI() schedules = api.get_schedules_by_names("Courtrai", "Leuven") for schedule in schedules.connections(): - print schedule + print(schedule) def test_getliveboard(): api = iRailAPI() schedule = api.get_liveboard_by_name("Gentbrugge") - print schedule + print(schedule) #schedule = api.get_liveboard_by_id("BE.NMBS.008893179") #print schedule def test_getvehicle(): api = iRailAPI() schedule = api.get_vehicle_by_id("BE.NMBS.L584") - print schedule + print(schedule) if __name__=="__main__": # test_getstations()