def test_to_json(self): theatre_json = { "name": "Test Theatre 5", "description": "Test triplet when there's an unacceptable distance", "showtimes": [ { "name": "a", "runtime": 60, "times": ["16:00"] }, { "name": "b", "runtime": 60, "times": ["17:05"] }, { "name": "c", "runtime": 60, "times": ["19:05"] } ] } expected_output = { 'name': theatre_json['name'], 'info': theatre_json.get('info'), 'doubleDips': [ [ { 'movie': theatre_json['showtimes'][0]['name'], 'length': theatre_json['showtimes'][0]['runtime'], 'startTime': theatre_json['showtimes'][0]['times'][0], 'endTime': "17:00" }, { 'movie': theatre_json['showtimes'][1]['name'], 'length': theatre_json['showtimes'][1]['runtime'], 'startTime': theatre_json['showtimes'][1]['times'][0], 'endTime': "18:05" } ], [ { 'movie': theatre_json['showtimes'][2]['name'], 'length': theatre_json['showtimes'][2]['runtime'], 'startTime': theatre_json['showtimes'][2]['times'][0], 'endTime': "20:05" } ] ] } theatre = Theatre(name=theatre_json.get('name'), showtimes=theatre_json.get('showtimes'), info=theatre_json.get('info')) assert theatre.to_json() == expected_output
def get_doubledips(): location = request.args.get('location') days_from_now = request.args.get('days_from_now') max_waiting_time = request.args.get('max_wait_mins') max_overlap_time = request.args.get('max_overlap_mins') status = None msg = None mimetype = 'application/json' if location is None or not isinstance(location, str): status = 400 msg = "'location' is mandatory and must be a string." resp = Response(dumps({'msg': msg}), status=status, mimetype=mimetype) resp.headers['Access-Control-Allow-Origin'] = '*' return resp if days_from_now is not None: try: days_from_now = int(days_from_now) except Exception: status = 400 msg = "'days_from_now' must be a base-10 integer." resp = Response(dumps({'msg': msg}), status=status, mimetype=mimetype) resp.headers['Access-Control-Allow-Origin'] = '*' return resp else: days_from_now = 0 if max_waiting_time is not None: try: max_waiting_time = int(max_waiting_time) except Exception: status = 400 msg = "'max_waiting_time' must be a base-10 integer" resp = Response(dumps({'msg': msg}), status=status, mimetype=mimetype) resp.headers['Access-Control-Allow-Origin'] = '*' return resp else: max_waiting_time = 45 if max_overlap_time is not None: try: max_overlap_time = int(max_overlap_time) except Exception: status = 400 msg = "'max_overlap_time' must be a base-10 integer" resp = Response(dumps({'msg': msg}), status=status, mimetype=mimetype) resp.headers['Access-Control-Allow-Origin'] = '*' return resp else: max_overlap_time = 5 gms_url = 'http://google-movies-scraper.herokuapp.com/v2/movies' gms_params = { 'near': location, 'date': days_from_now, 'militaryTime': True } # should definitely build some logic to handle response code of r... r = requests.get(gms_url, params=gms_params) theatres_json = r.json() output = [] for theatre in theatres_json: try: tmp_theatre = Theatre(name=theatre.get('name'), showtimes=theatre.get('showtimes'), info=theatre.get('info')) tmp_json = tmp_theatre.to_json(max_waiting_time=max_waiting_time, max_overlap_time=max_overlap_time) output.append(tmp_json) except TypeError as e: warn(str(e)) status = 200 resp = Response(dumps(output), status=status, mimetype=mimetype) resp.headers['Access-Control-Allow-Origin'] = '*' return(resp)