diff --git a/doc/API_exploration.ipynb b/doc/API_exploration.ipynb index 2bb7269..edbb688 100644 --- a/doc/API_exploration.ipynb +++ b/doc/API_exploration.ipynb @@ -10,10 +10,8 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": { - "collapsed": false - }, + "execution_count": 1, + "metadata": {}, "outputs": [], "source": [ "import requests #to handle http requests to the API\n", @@ -22,10 +20,8 @@ }, { "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": true - }, + "execution_count": 2, + "metadata": {}, "outputs": [], "source": [ "stationid = 3 \n", @@ -36,10 +32,8 @@ }, { "cell_type": "code", - "execution_count": 4, - "metadata": { - "collapsed": true - }, + "execution_count": 3, + "metadata": {}, "outputs": [], "source": [ "# The url for the request\n", @@ -48,10 +42,8 @@ }, { "cell_type": "code", - "execution_count": 6, - "metadata": { - "collapsed": false - }, + "execution_count": 4, + "metadata": {}, "outputs": [], "source": [ "# Our query parameters for this API request\n", @@ -75,84 +67,95 @@ }, { "cell_type": "code", - "execution_count": 7, - "metadata": { - "collapsed": false - }, + "execution_count": 5, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "{'allStations': 'success',\n", + "{'allLineXStations': None,\n", + " 'allStations': 'success',\n", " 'data': None,\n", " 'defaultDirection': [['YKD1', 'Southbound
To Union', 'YUS'],\n", - " ['YKD2', 'Northbound
To Downsview', 'YUS']],\n", + " ['YKD2', 'Northbound
To Vaughan Metropolitan Centre', 'YUS']],\n", " 'limit': 3,\n", - " 'ntasData': [{'createDate': '2017-01-31T20:28:21',\n", - " 'id': 12175559601,\n", + " 'ntasData': [{'createDate': '2018-04-10T20:46:16',\n", + " 'id': 20947050944,\n", " 'stationDirectionText': 'Southbound
To Union',\n", " 'stationId': 'YKD1',\n", " 'subwayLine': 'YUS',\n", " 'systemMessageType': 'Normal',\n", - " 'timeInt': 0.29609142857142856,\n", - " 'timeString': '00.30',\n", + " 'timeInt': 1.039222857142857,\n", + " 'timeString': '02.00',\n", + " 'trainDestination': 'FIN1',\n", + " 'trainDestinationStation': 'Finch',\n", " 'trainDirection': 'North',\n", - " 'trainId': 145,\n", + " 'trainId': 121,\n", " 'trainMessage': 'Arriving'},\n", - " {'createDate': '2017-01-31T20:28:21',\n", - " 'id': 12175559602,\n", + " {'createDate': '2018-04-10T20:46:16',\n", + " 'id': 20947050945,\n", " 'stationDirectionText': 'Southbound
To Union',\n", " 'stationId': 'YKD1',\n", " 'subwayLine': 'YUS',\n", " 'systemMessageType': 'Normal',\n", - " 'timeInt': 4.534429241904761,\n", - " 'timeString': '04.53',\n", + " 'timeInt': 4.534429333333333,\n", + " 'timeString': '05.00',\n", + " 'trainDestination': 'FIN1',\n", + " 'trainDestinationStation': 'Finch',\n", " 'trainDirection': 'North',\n", - " 'trainId': 122,\n", - " 'trainMessage': 'Delayed'},\n", - " {'createDate': '2017-01-31T20:28:21',\n", - " 'id': 12175559603,\n", + " 'trainId': 136,\n", + " 'trainMessage': 'Arriving'},\n", + " {'createDate': '2018-04-10T20:46:16',\n", + " 'id': 20947050946,\n", " 'stationDirectionText': 'Southbound
To Union',\n", " 'stationId': 'YKD1',\n", " 'subwayLine': 'YUS',\n", " 'systemMessageType': 'Normal',\n", - " 'timeInt': 4.93227686095238,\n", - " 'timeString': '04.93',\n", + " 'timeInt': 8.620152952380952,\n", + " 'timeString': '09.00',\n", + " 'trainDestination': 'FIN1',\n", + " 'trainDestinationStation': 'Finch',\n", " 'trainDirection': 'North',\n", - " 'trainId': 126,\n", + " 'trainId': 122,\n", " 'trainMessage': 'Arriving'},\n", - " {'createDate': '2017-01-31T20:28:21',\n", - " 'id': 12175559586,\n", - " 'stationDirectionText': 'Northbound
To Downsview',\n", + " {'createDate': '2018-04-10T20:46:16',\n", + " 'id': 20947050906,\n", + " 'stationDirectionText': 'Southbound
To Union',\n", " 'stationId': 'YKD2',\n", " 'subwayLine': 'YUS',\n", " 'systemMessageType': 'Normal',\n", " 'timeInt': 0.0,\n", " 'timeString': '00.00',\n", + " 'trainDestination': 'VMC2',\n", + " 'trainDestinationStation': 'Vaughan Metropolitan Centre',\n", " 'trainDirection': 'South',\n", - " 'trainId': 154,\n", + " 'trainId': 137,\n", " 'trainMessage': 'AtStation'},\n", - " {'createDate': '2017-01-31T20:28:21',\n", - " 'id': 12175559587,\n", - " 'stationDirectionText': 'Northbound
To Downsview',\n", + " {'createDate': '2018-04-10T20:46:16',\n", + " 'id': 20947050907,\n", + " 'stationDirectionText': 'Northbound
To Vaughan Metropolitan Centre',\n", " 'stationId': 'YKD2',\n", " 'subwayLine': 'YUS',\n", " 'systemMessageType': 'Normal',\n", - " 'timeInt': 1.3254445714285714,\n", - " 'timeString': '01.33',\n", + " 'timeInt': 2.0768331428571427,\n", + " 'timeString': '03.00',\n", + " 'trainDestination': 'WIL2',\n", + " 'trainDestinationStation': 'Wilson',\n", " 'trainDirection': 'South',\n", - " 'trainId': 155,\n", + " 'trainId': 138,\n", " 'trainMessage': 'Arriving'},\n", - " {'createDate': '2017-01-31T20:28:21',\n", - " 'id': 12175559588,\n", - " 'stationDirectionText': 'Northbound
To Downsview',\n", + " {'createDate': '2018-04-10T20:46:16',\n", + " 'id': 20947050908,\n", + " 'stationDirectionText': 'Southbound
To Union',\n", " 'stationId': 'YKD2',\n", " 'subwayLine': 'YUS',\n", " 'systemMessageType': 'Normal',\n", - " 'timeInt': 2.2986114285714283,\n", - " 'timeString': '02.30',\n", + " 'timeInt': 3.4726560000000006,\n", + " 'timeString': '04.00',\n", + " 'trainDestination': 'VMC2',\n", + " 'trainDestinationStation': 'Vaughan Metropolitan Centre',\n", " 'trainDirection': 'South',\n", - " 'trainId': 157,\n", + " 'trainId': 140,\n", " 'trainMessage': 'Arriving'}],\n", " 'searchCriteria': '',\n", " 'searchCriteriaMatch': None,\n", @@ -173,7 +176,7 @@ " 'Davisville Station',\n", " 'Don Mills Station',\n", " 'Donlands Station',\n", - " 'Downsview Station',\n", + " 'Downsview Park Station',\n", " 'Dufferin Station',\n", " 'Dundas Station',\n", " 'Dundas West Station',\n", @@ -181,9 +184,11 @@ " 'Eglinton Station',\n", " 'Eglinton West Station',\n", " 'Finch Station',\n", + " 'Finch West Station',\n", " 'Glencairn Station',\n", " 'Greenwood Station',\n", " 'High Park Station',\n", + " 'Highway 407 Station',\n", " 'Islington Station',\n", " 'Jane Station',\n", " 'Keele Station',\n", @@ -201,11 +206,13 @@ " 'Osgoode Station',\n", " 'Ossington Station',\n", " 'Pape Station',\n", + " 'Pioneer Village Station',\n", " 'Queen Station',\n", " 'Queen’s Park Station',\n", " 'Rosedale Station',\n", " 'Royal York Station',\n", " 'Runnymede Station',\n", + " 'Sheppard West Station',\n", " 'Sheppard-Yonge Station',\n", " 'Sherbourne Station',\n", " 'Spadina Station',\n", @@ -216,16 +223,18 @@ " 'St Patrick Station',\n", " 'Summerhill Station',\n", " 'Union Station',\n", + " 'Vaughan Metropolitan Centre Station',\n", " 'Victoria Park Station',\n", " 'Warden Station',\n", " 'Wellesley Station',\n", " 'Wilson Station',\n", " 'Woodbine Station',\n", " 'York Mills Station',\n", + " 'York University Station',\n", " 'Yorkdale Station']}" ] }, - "execution_count": 7, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -236,10 +245,8 @@ }, { "cell_type": "code", - "execution_count": 11, - "metadata": { - "collapsed": true - }, + "execution_count": 6, + "metadata": {}, "outputs": [], "source": [ "data = r.json()" @@ -247,18 +254,16 @@ }, { "cell_type": "code", - "execution_count": 12, - "metadata": { - "collapsed": false - }, + "execution_count": 7, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'2017-01-31T20:28:21'" + "'2018-04-10T20:46:16'" ] }, - "execution_count": 12, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -269,86 +274,175 @@ }, { "cell_type": "code", - "execution_count": 5, - "metadata": { - "collapsed": false - }, + "execution_count": 8, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "{'allStations': 'success',\n", + "{'allLineXStations': None,\n", + " 'allStations': 'success',\n", " 'data': None,\n", " 'defaultDirection': [['SGU1', 'Southbound
To Union', 'YUS'],\n", - " ['SGU2', 'Northbound
To Downsview', 'YUS'],\n", + " ['SGU2', 'Northbound
To Vaughan Metropolitan Centre', 'YUS'],\n", " ['SGL1', 'Eastbound
To Kennedy', 'BD'],\n", " ['SGL2', 'Westbound
To Kipling', 'BD']],\n", " 'limit': 3,\n", - " 'ntasData': [{'createDate': '2017-02-05T16:25:30',\n", - " 'id': 12265037214,\n", - " 'stationDirectionText': 'Northbound
To Downsview',\n", + " 'ntasData': [{'createDate': '2018-04-10T20:46:21',\n", + " 'id': 20947052333,\n", + " 'stationDirectionText': 'Northbound
To Vaughan Metropolitan Centre',\n", " 'stationId': 'SGU2',\n", " 'subwayLine': 'YUS',\n", " 'systemMessageType': 'Normal',\n", - " 'timeInt': 0.9045222222222222,\n", - " 'timeString': '00.90',\n", + " 'timeInt': 0.0,\n", + " 'timeString': '00.00',\n", + " 'trainDestination': 'VMC2',\n", + " 'trainDestinationStation': 'Vaughan Metropolitan Centre',\n", " 'trainDirection': 'South',\n", - " 'trainId': 124,\n", - " 'trainMessage': 'Arriving'},\n", - " {'createDate': '2017-02-05T16:25:30',\n", - " 'id': 12265037215,\n", - " 'stationDirectionText': 'Northbound
To Downsview',\n", + " 'trainId': 145,\n", + " 'trainMessage': 'AtStation'},\n", + " {'createDate': '2018-04-10T20:46:21',\n", + " 'id': 20947052334,\n", + " 'stationDirectionText': 'Northbound
To Vaughan Metropolitan Centre',\n", " 'stationId': 'SGU2',\n", " 'subwayLine': 'YUS',\n", " 'systemMessageType': 'Normal',\n", - " 'timeInt': 4.51758,\n", - " 'timeString': '04.52',\n", + " 'timeInt': 0.9303657142857144,\n", + " 'timeString': '01.00',\n", + " 'trainDestination': 'WIL2',\n", + " 'trainDestinationStation': 'Wilson',\n", " 'trainDirection': 'South',\n", - " 'trainId': 125,\n", + " 'trainId': 146,\n", " 'trainMessage': 'Arriving'},\n", - " {'createDate': '2017-02-05T16:25:30',\n", - " 'id': 12265037216,\n", - " 'stationDirectionText': 'Northbound
To Downsview',\n", + " {'createDate': '2018-04-10T20:46:21',\n", + " 'id': 20947052335,\n", + " 'stationDirectionText': 'Northbound
To Vaughan Metropolitan Centre',\n", " 'stationId': 'SGU2',\n", " 'subwayLine': 'YUS',\n", " 'systemMessageType': 'Normal',\n", - " 'timeInt': 6.747313333333333,\n", - " 'timeString': '06.75',\n", + " 'timeInt': 2.4766857142857144,\n", + " 'timeString': '03.00',\n", + " 'trainDestination': 'VMC2',\n", + " 'trainDestinationStation': 'Vaughan Metropolitan Centre',\n", " 'trainDirection': 'South',\n", - " 'trainId': 126,\n", + " 'trainId': 148,\n", " 'trainMessage': 'Arriving'},\n", - " {'createDate': '2017-02-05T16:25:30',\n", - " 'id': 12265037271,\n", + " {'createDate': '2018-04-10T20:46:20',\n", + " 'id': 20947051898,\n", + " 'stationDirectionText': 'Eastbound
To Kennedy',\n", + " 'stationId': 'SGL1',\n", + " 'subwayLine': 'BD',\n", + " 'systemMessageType': 'Normal',\n", + " 'timeInt': 1.5856777777777777,\n", + " 'timeString': '02.00',\n", + " 'trainDestination': 'KEN1',\n", + " 'trainDestinationStation': 'Kennedy',\n", + " 'trainDirection': 'East',\n", + " 'trainId': 235,\n", + " 'trainMessage': 'Arriving'},\n", + " {'createDate': '2018-04-10T20:46:20',\n", + " 'id': 20947051899,\n", + " 'stationDirectionText': 'Eastbound
To Kennedy',\n", + " 'stationId': 'SGL1',\n", + " 'subwayLine': 'BD',\n", + " 'systemMessageType': 'Normal',\n", + " 'timeInt': 3.229928888888889,\n", + " 'timeString': '04.00',\n", + " 'trainDestination': 'KEN1',\n", + " 'trainDestinationStation': 'Kennedy',\n", + " 'trainDirection': 'East',\n", + " 'trainId': 238,\n", + " 'trainMessage': 'Arriving'},\n", + " {'createDate': '2018-04-10T20:46:20',\n", + " 'id': 20947051900,\n", + " 'stationDirectionText': 'Eastbound
To Kennedy',\n", + " 'stationId': 'SGL1',\n", + " 'subwayLine': 'BD',\n", + " 'systemMessageType': 'Normal',\n", + " 'timeInt': 4.372895955555555,\n", + " 'timeString': '05.00',\n", + " 'trainDestination': 'KEN1',\n", + " 'trainDestinationStation': 'Kennedy',\n", + " 'trainDirection': 'East',\n", + " 'trainId': 239,\n", + " 'trainMessage': 'Arriving'},\n", + " {'createDate': '2018-04-10T20:46:21',\n", + " 'id': 20947052413,\n", " 'stationDirectionText': 'Southbound
To Union',\n", " 'stationId': 'SGU1',\n", " 'subwayLine': 'YUS',\n", " 'systemMessageType': 'Normal',\n", - " 'timeInt': 2.130237777777778,\n", - " 'timeString': '02.13',\n", + " 'timeInt': 0.0,\n", + " 'timeString': '00.00',\n", + " 'trainDestination': 'FIN1',\n", + " 'trainDestinationStation': 'Finch',\n", " 'trainDirection': 'North',\n", - " 'trainId': 114,\n", - " 'trainMessage': 'Arriving'},\n", - " {'createDate': '2017-02-05T16:25:30',\n", - " 'id': 12265037272,\n", + " 'trainId': 110,\n", + " 'trainMessage': 'AtStation'},\n", + " {'createDate': '2018-04-10T20:46:21',\n", + " 'id': 20947052414,\n", " 'stationDirectionText': 'Southbound
To Union',\n", " 'stationId': 'SGU1',\n", " 'subwayLine': 'YUS',\n", " 'systemMessageType': 'Normal',\n", - " 'timeInt': 7.999484444444445,\n", - " 'timeString': '08.00',\n", + " 'timeInt': 10.037435428571428,\n", + " 'timeString': '11.00',\n", + " 'trainDestination': 'FIN1',\n", + " 'trainDestinationStation': 'Finch',\n", " 'trainDirection': 'North',\n", - " 'trainId': 115,\n", + " 'trainId': 11,\n", " 'trainMessage': 'Arriving'},\n", - " {'createDate': '2017-02-05T16:25:30',\n", - " 'id': 12265037273,\n", + " {'createDate': '2018-04-10T20:46:21',\n", + " 'id': 20947052415,\n", " 'stationDirectionText': 'Southbound
To Union',\n", " 'stationId': 'SGU1',\n", " 'subwayLine': 'YUS',\n", " 'systemMessageType': 'Normal',\n", - " 'timeInt': 12.377904444444443,\n", - " 'timeString': '12.38',\n", + " 'timeInt': 12.815018285714286,\n", + " 'timeString': '13.00',\n", + " 'trainDestination': 'FIN1',\n", + " 'trainDestinationStation': 'Finch',\n", " 'trainDirection': 'North',\n", - " 'trainId': 116,\n", + " 'trainId': 118,\n", + " 'trainMessage': 'Arriving'},\n", + " {'createDate': '2018-04-10T20:46:20',\n", + " 'id': 20947051985,\n", + " 'stationDirectionText': 'Westbound
To Kipling',\n", + " 'stationId': 'SGL2',\n", + " 'subwayLine': 'BD',\n", + " 'systemMessageType': 'Normal',\n", + " 'timeInt': 1.9141966666666668,\n", + " 'timeString': '02.00',\n", + " 'trainDestination': 'KIP2',\n", + " 'trainDestinationStation': 'Kipling',\n", + " 'trainDirection': 'West',\n", + " 'trainId': 211,\n", + " 'trainMessage': 'Arriving'},\n", + " {'createDate': '2018-04-10T20:46:20',\n", + " 'id': 20947051986,\n", + " 'stationDirectionText': 'Westbound
To Kipling',\n", + " 'stationId': 'SGL2',\n", + " 'subwayLine': 'BD',\n", + " 'systemMessageType': 'Normal',\n", + " 'timeInt': 4.608596666666667,\n", + " 'timeString': '05.00',\n", + " 'trainDestination': 'KIP2',\n", + " 'trainDestinationStation': 'Kipling',\n", + " 'trainDirection': 'West',\n", + " 'trainId': 213,\n", + " 'trainMessage': 'Arriving'},\n", + " {'createDate': '2018-04-10T20:46:20',\n", + " 'id': 20947051987,\n", + " 'stationDirectionText': 'Westbound
To Kipling',\n", + " 'stationId': 'SGL2',\n", + " 'subwayLine': 'BD',\n", + " 'systemMessageType': 'Normal',\n", + " 'timeInt': 7.6898903999999995,\n", + " 'timeString': '08.00',\n", + " 'trainDestination': 'KIP2',\n", + " 'trainDestinationStation': 'Kipling',\n", + " 'trainDirection': 'West',\n", + " 'trainId': 215,\n", " 'trainMessage': 'Arriving'}],\n", " 'searchCriteria': '',\n", " 'searchCriteriaMatch': None,\n", @@ -369,7 +463,7 @@ " 'Davisville Station',\n", " 'Don Mills Station',\n", " 'Donlands Station',\n", - " 'Downsview Station',\n", + " 'Downsview Park Station',\n", " 'Dufferin Station',\n", " 'Dundas Station',\n", " 'Dundas West Station',\n", @@ -377,9 +471,11 @@ " 'Eglinton Station',\n", " 'Eglinton West Station',\n", " 'Finch Station',\n", + " 'Finch West Station',\n", " 'Glencairn Station',\n", " 'Greenwood Station',\n", " 'High Park Station',\n", + " 'Highway 407 Station',\n", " 'Islington Station',\n", " 'Jane Station',\n", " 'Keele Station',\n", @@ -397,11 +493,13 @@ " 'Osgoode Station',\n", " 'Ossington Station',\n", " 'Pape Station',\n", + " 'Pioneer Village Station',\n", " 'Queen Station',\n", " 'Queen’s Park Station',\n", " 'Rosedale Station',\n", " 'Royal York Station',\n", " 'Runnymede Station',\n", + " 'Sheppard West Station',\n", " 'Sheppard-Yonge Station',\n", " 'Sherbourne Station',\n", " 'Spadina Station',\n", @@ -412,16 +510,18 @@ " 'St Patrick Station',\n", " 'Summerhill Station',\n", " 'Union Station',\n", + " 'Vaughan Metropolitan Centre Station',\n", " 'Victoria Park Station',\n", " 'Warden Station',\n", " 'Wellesley Station',\n", " 'Wilson Station',\n", " 'Woodbine Station',\n", " 'York Mills Station',\n", + " 'York University Station',\n", " 'Yorkdale Station']}" ] }, - "execution_count": 5, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -437,91 +537,180 @@ }, { "cell_type": "code", - "execution_count": 8, - "metadata": { - "collapsed": false - }, + "execution_count": 9, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "{'allStations': 'success',\n", + "{'allLineXStations': None,\n", + " 'allStations': 'success',\n", " 'data': None,\n", " 'defaultDirection': [['SGL1', 'Eastbound
To Kennedy', 'BD'],\n", " ['SGL2', 'Westbound
To Kipling', 'BD'],\n", " ['SGU1', 'Southbound
To Union', 'YUS'],\n", - " ['SGU2', 'Northbound
To Downsview', 'YUS']],\n", + " ['SGU2', 'Northbound
To Vaughan Metropolitan Centre', 'YUS']],\n", " 'limit': 3,\n", - " 'ntasData': [{'createDate': '2017-02-05T16:30:13',\n", - " 'id': 12265113796,\n", + " 'ntasData': [{'createDate': '2018-04-10T20:46:21',\n", + " 'id': 20947052333,\n", + " 'stationDirectionText': 'Northbound
To Vaughan Metropolitan Centre',\n", + " 'stationId': 'SGU2',\n", + " 'subwayLine': 'YUS',\n", + " 'systemMessageType': 'Normal',\n", + " 'timeInt': 0.0,\n", + " 'timeString': '00.00',\n", + " 'trainDestination': 'VMC2',\n", + " 'trainDestinationStation': 'Vaughan Metropolitan Centre',\n", + " 'trainDirection': 'South',\n", + " 'trainId': 145,\n", + " 'trainMessage': 'AtStation'},\n", + " {'createDate': '2018-04-10T20:46:21',\n", + " 'id': 20947052334,\n", + " 'stationDirectionText': 'Northbound
To Vaughan Metropolitan Centre',\n", + " 'stationId': 'SGU2',\n", + " 'subwayLine': 'YUS',\n", + " 'systemMessageType': 'Normal',\n", + " 'timeInt': 0.9303657142857144,\n", + " 'timeString': '01.00',\n", + " 'trainDestination': 'WIL2',\n", + " 'trainDestinationStation': 'Wilson',\n", + " 'trainDirection': 'South',\n", + " 'trainId': 146,\n", + " 'trainMessage': 'Arriving'},\n", + " {'createDate': '2018-04-10T20:46:21',\n", + " 'id': 20947052335,\n", + " 'stationDirectionText': 'Northbound
To Vaughan Metropolitan Centre',\n", + " 'stationId': 'SGU2',\n", + " 'subwayLine': 'YUS',\n", + " 'systemMessageType': 'Normal',\n", + " 'timeInt': 2.4766857142857144,\n", + " 'timeString': '03.00',\n", + " 'trainDestination': 'VMC2',\n", + " 'trainDestinationStation': 'Vaughan Metropolitan Centre',\n", + " 'trainDirection': 'South',\n", + " 'trainId': 148,\n", + " 'trainMessage': 'Arriving'},\n", + " {'createDate': '2018-04-10T20:46:21',\n", + " 'id': 20947052590,\n", " 'stationDirectionText': 'Eastbound
To Kennedy',\n", " 'stationId': 'SGL1',\n", " 'subwayLine': 'BD',\n", " 'systemMessageType': 'Normal',\n", - " 'timeInt': 1.5897975232198145,\n", - " 'timeString': '01.59',\n", + " 'timeInt': 1.5856777777777777,\n", + " 'timeString': '02.00',\n", + " 'trainDestination': 'KEN1',\n", + " 'trainDestinationStation': 'Kennedy',\n", " 'trainDirection': 'East',\n", - " 'trainId': 203,\n", + " 'trainId': 235,\n", " 'trainMessage': 'Arriving'},\n", - " {'createDate': '2017-02-05T16:30:13',\n", - " 'id': 12265113797,\n", + " {'createDate': '2018-04-10T20:46:21',\n", + " 'id': 20947052591,\n", " 'stationDirectionText': 'Eastbound
To Kennedy',\n", " 'stationId': 'SGL1',\n", " 'subwayLine': 'BD',\n", " 'systemMessageType': 'Normal',\n", - " 'timeInt': 5.93598838885449,\n", - " 'timeString': '05.94',\n", + " 'timeInt': 3.229928888888889,\n", + " 'timeString': '04.00',\n", + " 'trainDestination': 'KEN1',\n", + " 'trainDestinationStation': 'Kennedy',\n", " 'trainDirection': 'East',\n", - " 'trainId': 204,\n", + " 'trainId': 238,\n", " 'trainMessage': 'Arriving'},\n", - " {'createDate': '2017-02-05T16:30:13',\n", - " 'id': 12265113798,\n", + " {'createDate': '2018-04-10T20:46:21',\n", + " 'id': 20947052592,\n", " 'stationDirectionText': 'Eastbound
To Kennedy',\n", " 'stationId': 'SGL1',\n", " 'subwayLine': 'BD',\n", " 'systemMessageType': 'Normal',\n", - " 'timeInt': 11.029528450773993,\n", - " 'timeString': '11.03',\n", + " 'timeInt': 4.372895955555555,\n", + " 'timeString': '05.00',\n", + " 'trainDestination': 'KEN1',\n", + " 'trainDestinationStation': 'Kennedy',\n", " 'trainDirection': 'East',\n", - " 'trainId': 205,\n", + " 'trainId': 239,\n", " 'trainMessage': 'Arriving'},\n", - " {'createDate': '2017-02-05T16:30:13',\n", - " 'id': 12265113703,\n", + " {'createDate': '2018-04-10T20:46:22',\n", + " 'id': 20947052697,\n", + " 'stationDirectionText': 'Southbound
To Union',\n", + " 'stationId': 'SGU1',\n", + " 'subwayLine': 'YUS',\n", + " 'systemMessageType': 'Normal',\n", + " 'timeInt': 0.0,\n", + " 'timeString': '00.00',\n", + " 'trainDestination': 'FIN1',\n", + " 'trainDestinationStation': 'Finch',\n", + " 'trainDirection': 'North',\n", + " 'trainId': 110,\n", + " 'trainMessage': 'AtStation'},\n", + " {'createDate': '2018-04-10T20:46:22',\n", + " 'id': 20947052698,\n", + " 'stationDirectionText': 'Southbound
To Union',\n", + " 'stationId': 'SGU1',\n", + " 'subwayLine': 'YUS',\n", + " 'systemMessageType': 'Normal',\n", + " 'timeInt': 10.037435428571428,\n", + " 'timeString': '11.00',\n", + " 'trainDestination': 'FIN1',\n", + " 'trainDestinationStation': 'Finch',\n", + " 'trainDirection': 'North',\n", + " 'trainId': 11,\n", + " 'trainMessage': 'Arriving'},\n", + " {'createDate': '2018-04-10T20:46:22',\n", + " 'id': 20947052699,\n", + " 'stationDirectionText': 'Southbound
To Union',\n", + " 'stationId': 'SGU1',\n", + " 'subwayLine': 'YUS',\n", + " 'systemMessageType': 'Normal',\n", + " 'timeInt': 12.815018285714286,\n", + " 'timeString': '13.00',\n", + " 'trainDestination': 'FIN1',\n", + " 'trainDestinationStation': 'Finch',\n", + " 'trainDirection': 'North',\n", + " 'trainId': 118,\n", + " 'trainMessage': 'Arriving'},\n", + " {'createDate': '2018-04-10T20:46:21',\n", + " 'id': 20947052499,\n", " 'stationDirectionText': 'Westbound
To Kipling',\n", " 'stationId': 'SGL2',\n", " 'subwayLine': 'BD',\n", " 'systemMessageType': 'Normal',\n", - " 'timeInt': 3.0209197523219817,\n", - " 'timeString': '03.02',\n", + " 'timeInt': 1.9141966666666668,\n", + " 'timeString': '02.00',\n", + " 'trainDestination': 'KIP2',\n", + " 'trainDestinationStation': 'Kipling',\n", " 'trainDirection': 'West',\n", - " 'trainId': 214,\n", + " 'trainId': 211,\n", " 'trainMessage': 'Arriving'},\n", - " {'createDate': '2017-02-05T16:30:13',\n", - " 'id': 12265113704,\n", + " {'createDate': '2018-04-10T20:46:21',\n", + " 'id': 20947052500,\n", " 'stationDirectionText': 'Westbound
To Kipling',\n", " 'stationId': 'SGL2',\n", " 'subwayLine': 'BD',\n", " 'systemMessageType': 'Normal',\n", - " 'timeInt': 8.047886964705883,\n", - " 'timeString': '08.05',\n", + " 'timeInt': 4.228725555555556,\n", + " 'timeString': '05.00',\n", + " 'trainDestination': 'KIP2',\n", + " 'trainDestinationStation': 'Kipling',\n", " 'trainDirection': 'West',\n", - " 'trainId': 215,\n", + " 'trainId': 213,\n", " 'trainMessage': 'Arriving'},\n", - " {'createDate': '2017-02-05T16:30:13',\n", - " 'id': 12265113705,\n", + " {'createDate': '2018-04-10T20:46:21',\n", + " 'id': 20947052501,\n", " 'stationDirectionText': 'Westbound
To Kipling',\n", " 'stationId': 'SGL2',\n", " 'subwayLine': 'BD',\n", " 'systemMessageType': 'Normal',\n", - " 'timeInt': 12.080311237151706,\n", - " 'timeString': '12.08',\n", + " 'timeInt': 7.6898903999999995,\n", + " 'timeString': '08.00',\n", + " 'trainDestination': 'KIP2',\n", + " 'trainDestinationStation': 'Kipling',\n", " 'trainDirection': 'West',\n", - " 'trainId': 216,\n", + " 'trainId': 215,\n", " 'trainMessage': 'Arriving'}],\n", " 'searchCriteria': '',\n", " 'searchCriteriaMatch': None,\n", " 'stationId': '48',\n", - " 'subwayLine': '1',\n", + " 'subwayLine': None,\n", " 'subwayLine2': '1',\n", " 'subwayStations': ['Bathurst Station',\n", " 'Bay Station',\n", @@ -537,7 +726,7 @@ " 'Davisville Station',\n", " 'Don Mills Station',\n", " 'Donlands Station',\n", - " 'Downsview Station',\n", + " 'Downsview Park Station',\n", " 'Dufferin Station',\n", " 'Dundas Station',\n", " 'Dundas West Station',\n", @@ -545,9 +734,11 @@ " 'Eglinton Station',\n", " 'Eglinton West Station',\n", " 'Finch Station',\n", + " 'Finch West Station',\n", " 'Glencairn Station',\n", " 'Greenwood Station',\n", " 'High Park Station',\n", + " 'Highway 407 Station',\n", " 'Islington Station',\n", " 'Jane Station',\n", " 'Keele Station',\n", @@ -565,11 +756,13 @@ " 'Osgoode Station',\n", " 'Ossington Station',\n", " 'Pape Station',\n", + " 'Pioneer Village Station',\n", " 'Queen Station',\n", " 'Queen’s Park Station',\n", " 'Rosedale Station',\n", " 'Royal York Station',\n", " 'Runnymede Station',\n", + " 'Sheppard West Station',\n", " 'Sheppard-Yonge Station',\n", " 'Sherbourne Station',\n", " 'Spadina Station',\n", @@ -580,16 +773,18 @@ " 'St Patrick Station',\n", " 'Summerhill Station',\n", " 'Union Station',\n", + " 'Vaughan Metropolitan Centre Station',\n", " 'Victoria Park Station',\n", " 'Warden Station',\n", " 'Wellesley Station',\n", " 'Wilson Station',\n", " 'Woodbine Station',\n", " 'York Mills Station',\n", + " 'York University Station',\n", " 'Yorkdale Station']}" ] }, - "execution_count": 8, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -606,14 +801,12 @@ { "cell_type": "code", "execution_count": 10, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'2017-02-05 16:30:13'" + "'2018-04-10 20:46:21'" ] }, "execution_count": 10, @@ -645,10 +838,8 @@ }, { "cell_type": "code", - "execution_count": 24, - "metadata": { - "collapsed": true - }, + "execution_count": 11, + "metadata": {}, "outputs": [], "source": [ "lines = {1: range(1, 33), #max value must be 1 greater\n", @@ -658,26 +849,71 @@ }, { "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": true - }, + "execution_count": 12, + "metadata": {}, "outputs": [], "source": [ - "def get_API_response(line_id, station_id):\n", - " payload = {\"subwayLine\":line_id,\n", - " \"stationId\":station_id,\n", - " \"searchCriteria\":''}\n", + "def get_API_response(*args):\n", + " baseurl = \"http://www.ttc.ca/Subway/loadNtas.action\"\n", + " if len(args) > 1:\n", + " line_id = args[0]\n", + " station_id = args[2]\n", + " payload = {\"subwayLine\":line_id,\n", + " \"stationId\":station_id,\n", + " \"searchCriteria\":''}\n", + " else:\n", + " station_id = args[0]\n", + " payload = {\"stationId\":station_id,\n", + " \"searchCriteria\":''}\n", " r = requests.get(baseurl, params = payload) \n", " return r.json()\n", "\n", - "def query_all_stations():\n", + "def insert_request_info(con, data, line_id, station_id):\n", + " request_row = {}\n", + " request_row['data_'] = data['data']\n", + " request_row['stationid'] = station_id\n", + " request_row['lineid'] = line_id\n", + " request_row['all_stations'] = data['allStations']\n", + " request_row['create_date'] = data['ntasData'][0]['createDate'].replace( 'T', ' ')\n", + " cursor = con.cursor()\n", + " cursor.execute(\"INSERT INTO public.requests(data_, stationid, lineid, all_stations, create_date)\"\n", + " \"VALUES(%(data_)s, %(stationid)s, %(lineid)s, %(all_stations)s, %(create_date)s)\"\n", + " \"RETURNING requestid\", request_row)\n", + " request_id = cursor.fetchone()[0]\n", + " con.commit()\n", + " return request_id\n", + "\n", + "def insert_ntas_data(con, ntas_data, request_id):\n", + " cursor = con.cursor()\n", + " sql = \"\"\"INSERT INTO public.ntas_data(\n", + " requestid, id, station_char, subwayline, system_message_type, \n", + " timint, traindirection, trainid, train_message)\n", + " VALUES (%(requestid)s, %(id)s, %(station_char)s, %(subwayline)s, %(system_message_type)s, \n", + " %(timint)s, %(traindirection)s, %(trainid)s, %(train_message)s);\n", + " \"\"\"\n", + " for record in ntas_data:\n", + " record_row ={}\n", + " record_row['requestid'] = request_id\n", + " record_row['id'] = record['id']\n", + " record_row['station_char'] = record['stationId']\n", + " record_row['subwayline'] = record['subwayLine']\n", + " record_row['system_message_type'] = record['systemMessageType']\n", + " record_row['timint'] = record['timeInt']\n", + " record_row['traindirection'] = record['trainDirection']\n", + " record_row['trainid'] = record['trainId']\n", + " record_row['train_message'] = record['trainMessage']\n", + " cursor.execute(sql, record_row)\n", + " con.commit()\n", + " cursor.close()\n", + "\n", + "def query_stations(con, lines):\n", " data = {}\n", " for line_id, stations in lines.items():\n", - " data[line_id] = {}\n", " for station_id in stations:\n", - " data[line_id][station_id] = get_API_response(line_id, station_id)\n", - " return data" + " data = get_API_response(station_id)\n", + " request_id = insert_request_info(con, data, line_id, station_id)\n", + " insert_ntas_data(con, data['ntasData'], request_id)\n", + " return data, request_id\n" ] }, { @@ -685,20 +921,18 @@ "metadata": {}, "source": [ "## Database schema \n", - "Looking at the response above. I've written up a basic schema of two tables to store the responses to the API. it's in [`create_tables.sql`](create_tables.sql)." + "Looking at the response above. I've written up a basic schema of two tables to store the responses to the API. it's in [`create_tables.sql`](create_tables.sql). Use this file to setup of PostgreSQL database either using terminal (Linux/OSX) or command line (Windows). Alternately, you can download PgAdmin v3 or v4 (depending on your platform) which will provide you with a GUI to setup and manage the database. In the latter case, the default database name is 'postgres' and use 'postgres' as the password as well when setting up the server." ] }, { "cell_type": "code", - "execution_count": 10, - "metadata": { - "collapsed": false - }, + "execution_count": 13, + "metadata": {}, "outputs": [], "source": [ "dbsettings = {'database':'ttc',\n", - " 'user':'rad'}\n", - "# 'host':'localhost'}\n", + " 'user':'postgres'}\n", + "# 'host':'localhost'} \n", "con = connect(database = dbsettings['database'],\n", " user = dbsettings['user'])\n", " #host = dbsettings['host'])\n" @@ -706,155 +940,244 @@ }, { "cell_type": "code", - "execution_count": 19, - "metadata": { - "collapsed": false - }, + "execution_count": 14, + "metadata": {}, "outputs": [], "source": [ - "response = query_all_stations() " + "data = query_stations(con, lines) # be patient, this command can take a while to complete" ] }, { "cell_type": "code", - "execution_count": 23, - "metadata": { - "collapsed": false - }, - "outputs": [], + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "({'allLineXStations': None,\n", + " 'allStations': 'success',\n", + " 'data': None,\n", + " 'defaultDirection': [['LES1', 'Eastbound
to Don Mills', 'SHE'],\n", + " ['LES2', 'Westbound
To Sheppard-Yonge', 'SHE']],\n", + " 'limit': 3,\n", + " 'ntasData': [{'createDate': '2018-04-10T20:47:04',\n", + " 'id': 20947068027,\n", + " 'stationDirectionText': 'Westbound
To Sheppard-Yonge',\n", + " 'stationId': 'LES1',\n", + " 'subwayLine': 'SHEP',\n", + " 'systemMessageType': 'Normal',\n", + " 'timeInt': 2.4838590604026844,\n", + " 'timeString': '03.00',\n", + " 'trainDestination': 'DML1',\n", + " 'trainDestinationStation': 'Don Mills',\n", + " 'trainDirection': 'East',\n", + " 'trainId': 461,\n", + " 'trainMessage': 'Arriving'}],\n", + " 'searchCriteria': '',\n", + " 'searchCriteriaMatch': None,\n", + " 'stationId': '67',\n", + " 'subwayLine': None,\n", + " 'subwayLine2': None,\n", + " 'subwayStations': ['Bathurst Station',\n", + " 'Bay Station',\n", + " 'Bayview Station',\n", + " 'Bessarion Station',\n", + " 'Bloor-Yonge Station',\n", + " 'Broadview Station',\n", + " 'Castle Frank Station',\n", + " 'Chester Station',\n", + " 'Christie Station',\n", + " 'College Station',\n", + " 'Coxwell Station',\n", + " 'Davisville Station',\n", + " 'Don Mills Station',\n", + " 'Donlands Station',\n", + " 'Downsview Park Station',\n", + " 'Dufferin Station',\n", + " 'Dundas Station',\n", + " 'Dundas West Station',\n", + " 'Dupont Station',\n", + " 'Eglinton Station',\n", + " 'Eglinton West Station',\n", + " 'Finch Station',\n", + " 'Finch West Station',\n", + " 'Glencairn Station',\n", + " 'Greenwood Station',\n", + " 'High Park Station',\n", + " 'Highway 407 Station',\n", + " 'Islington Station',\n", + " 'Jane Station',\n", + " 'Keele Station',\n", + " 'Kennedy Station',\n", + " 'King Station',\n", + " 'Kipling Station',\n", + " 'Lansdowne Station',\n", + " 'Lawrence Station',\n", + " 'Lawrence West Station',\n", + " 'Leslie Station',\n", + " 'Main Street Station',\n", + " 'Museum Station',\n", + " 'North York Centre Station',\n", + " 'Old Mill Station',\n", + " 'Osgoode Station',\n", + " 'Ossington Station',\n", + " 'Pape Station',\n", + " 'Pioneer Village Station',\n", + " 'Queen Station',\n", + " 'Queen’s Park Station',\n", + " 'Rosedale Station',\n", + " 'Royal York Station',\n", + " 'Runnymede Station',\n", + " 'Sheppard West Station',\n", + " 'Sheppard-Yonge Station',\n", + " 'Sherbourne Station',\n", + " 'Spadina Station',\n", + " 'St Andrew Station',\n", + " 'St Clair Station',\n", + " 'St Clair West Station',\n", + " 'St George Station',\n", + " 'St Patrick Station',\n", + " 'Summerhill Station',\n", + " 'Union Station',\n", + " 'Vaughan Metropolitan Centre Station',\n", + " 'Victoria Park Station',\n", + " 'Warden Station',\n", + " 'Wellesley Station',\n", + " 'Wilson Station',\n", + " 'Woodbine Station',\n", + " 'York Mills Station',\n", + " 'York University Station',\n", + " 'Yorkdale Station']},\n", + " 213)" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "insert_ntas_data(con, data['ntasData'], request_id)" + "data" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Querying data from database\n", + "Now we will pull the data we've inserted in the Postgre database" ] }, { "cell_type": "code", - "execution_count": 28, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "IndexError", - "evalue": "list index out of range", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", - "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mquery_all_stations\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcon\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", - "\u001b[0;32m\u001b[0m in \u001b[0;36mquery_all_stations\u001b[0;34m(con)\u001b[0m\n\u001b[1;32m 47\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mstation_id\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mstations\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 48\u001b[0m \u001b[0mdata\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_API_response\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mstation_id\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 49\u001b[0;31m \u001b[0mrequest_id\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minsert_request_info\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcon\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mline_id\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mstation_id\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 50\u001b[0m \u001b[0minsert_ntas_data\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcon\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'ntasData'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mrequest_id\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 51\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", - "\u001b[0;32m\u001b[0m in \u001b[0;36minsert_request_info\u001b[0;34m(con, data, line_id, station_id)\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0mrequest_row\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'lineid'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mline_id\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0mrequest_row\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'all_stations'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'allStations'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 13\u001b[0;31m \u001b[0mrequest_row\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'create_date'\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'ntasData'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'createDate'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreplace\u001b[0m\u001b[0;34m(\u001b[0m \u001b[0;34m'T'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m' '\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 14\u001b[0m \u001b[0mcursor\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcon\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcursor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m cursor.execute(\"INSERT INTO public.requests(data_, stationid, lineid, all_stations, create_date)\"\n", - "\u001b[0;31mIndexError\u001b[0m: list index out of range" - ] - } - ], + "execution_count": 16, + "metadata": {}, + "outputs": [], "source": [ - "query_all_stations(con)" + "lines = {1: [3]}# station_id = 3 (St. George), line_id = 1 (YUS)" ] }, { "cell_type": "code", - "execution_count": 9, - "metadata": { - "collapsed": false - }, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "invalid syntax (, line 4)", - "output_type": "error", - "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m4\u001b[0m\n\u001b[0;31m if time.now() -\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" - ] - } - ], + "execution_count": 17, + "metadata": {}, + "outputs": [], "source": [ - "last_poll = time.now()\n", - "poll_frequency = timedelta(minutes = 1)\n", - "while time.now() < time():\n", - " if time.now() - last_poll >= poll_frequency:\n", - " " + "data, request_id = query_stations(con, lines) " ] }, { "cell_type": "code", "execution_count": 18, - "metadata": { - "collapsed": false - }, + "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "{'allStations': 'success',\n", + "{'allLineXStations': None,\n", + " 'allStations': 'success',\n", " 'data': None,\n", " 'defaultDirection': [['YKD1', 'Southbound
To Union', 'YUS'],\n", - " ['YKD2', 'Northbound
To Downsview', 'YUS']],\n", + " ['YKD2', 'Northbound
To Vaughan Metropolitan Centre', 'YUS']],\n", " 'limit': 3,\n", - " 'ntasData': [{'createDate': '2017-02-02T23:22:42',\n", - " 'id': 12215713332,\n", + " 'ntasData': [{'createDate': '2018-04-10T20:47:16',\n", + " 'id': 20947072057,\n", " 'stationDirectionText': 'Southbound
To Union',\n", " 'stationId': 'YKD1',\n", " 'subwayLine': 'YUS',\n", " 'systemMessageType': 'Normal',\n", - " 'timeInt': 1.3716,\n", - " 'timeString': '01.37',\n", + " 'timeInt': 0.0,\n", + " 'timeString': '00.00',\n", + " 'trainDestination': 'FIN1',\n", + " 'trainDestinationStation': 'Finch',\n", " 'trainDirection': 'North',\n", - " 'trainId': 111,\n", - " 'trainMessage': 'Arriving'},\n", - " {'createDate': '2017-02-02T23:22:42',\n", - " 'id': 12215713333,\n", + " 'trainId': 121,\n", + " 'trainMessage': 'AtStation'},\n", + " {'createDate': '2018-04-10T20:47:16',\n", + " 'id': 20947072058,\n", " 'stationDirectionText': 'Southbound
To Union',\n", " 'stationId': 'YKD1',\n", " 'subwayLine': 'YUS',\n", " 'systemMessageType': 'Normal',\n", - " 'timeInt': 6.069130281481481,\n", - " 'timeString': '06.07',\n", + " 'timeInt': 4.534429333333333,\n", + " 'timeString': '05.00',\n", + " 'trainDestination': 'FIN1',\n", + " 'trainDestinationStation': 'Finch',\n", " 'trainDirection': 'North',\n", - " 'trainId': 113,\n", + " 'trainId': 136,\n", " 'trainMessage': 'Arriving'},\n", - " {'createDate': '2017-02-02T23:22:42',\n", - " 'id': 12215713334,\n", + " {'createDate': '2018-04-10T20:47:16',\n", + " 'id': 20947072059,\n", " 'stationDirectionText': 'Southbound
To Union',\n", " 'stationId': 'YKD1',\n", " 'subwayLine': 'YUS',\n", " 'systemMessageType': 'Normal',\n", - " 'timeInt': 11.632546577777777,\n", - " 'timeString': '11.63',\n", + " 'timeInt': 7.379200571428571,\n", + " 'timeString': '08.00',\n", + " 'trainDestination': 'FIN1',\n", + " 'trainDestinationStation': 'Finch',\n", " 'trainDirection': 'North',\n", - " 'trainId': 117,\n", + " 'trainId': 122,\n", " 'trainMessage': 'Arriving'},\n", - " {'createDate': '2017-02-02T23:22:42',\n", - " 'id': 12215713509,\n", - " 'stationDirectionText': 'Northbound
To Downsview',\n", + " {'createDate': '2018-04-10T20:47:16',\n", + " 'id': 20947072228,\n", + " 'stationDirectionText': 'Northbound
To Vaughan Metropolitan Centre',\n", " 'stationId': 'YKD2',\n", " 'subwayLine': 'YUS',\n", " 'systemMessageType': 'Normal',\n", - " 'timeInt': 5.21,\n", - " 'timeString': '05.21',\n", + " 'timeInt': 1.3254445714285714,\n", + " 'timeString': '02.00',\n", + " 'trainDestination': 'WIL2',\n", + " 'trainDestinationStation': 'Wilson',\n", " 'trainDirection': 'South',\n", - " 'trainId': 118,\n", + " 'trainId': 138,\n", " 'trainMessage': 'Arriving'},\n", - " {'createDate': '2017-02-02T23:22:42',\n", - " 'id': 12215713510,\n", - " 'stationDirectionText': 'Northbound
To Downsview',\n", + " {'createDate': '2018-04-10T20:47:16',\n", + " 'id': 20947072229,\n", + " 'stationDirectionText': 'Southbound
To Union',\n", " 'stationId': 'YKD2',\n", " 'subwayLine': 'YUS',\n", " 'systemMessageType': 'Normal',\n", - " 'timeInt': 13.797917777777778,\n", - " 'timeString': '13.80',\n", + " 'timeInt': 2.687013714285714,\n", + " 'timeString': '03.00',\n", + " 'trainDestination': 'VMC2',\n", + " 'trainDestinationStation': 'Vaughan Metropolitan Centre',\n", " 'trainDirection': 'South',\n", - " 'trainId': 157,\n", + " 'trainId': 140,\n", " 'trainMessage': 'Arriving'},\n", - " {'createDate': '2017-02-02T23:22:42',\n", - " 'id': 12215713511,\n", - " 'stationDirectionText': 'Northbound
To Downsview',\n", + " {'createDate': '2018-04-10T20:47:16',\n", + " 'id': 20947072230,\n", + " 'stationDirectionText': 'Northbound
To Vaughan Metropolitan Centre',\n", " 'stationId': 'YKD2',\n", " 'subwayLine': 'YUS',\n", " 'systemMessageType': 'Normal',\n", - " 'timeInt': 16.613955555555556,\n", - " 'timeString': '16.61',\n", + " 'timeInt': 4.395184,\n", + " 'timeString': '05.00',\n", + " 'trainDestination': 'WIL2',\n", + " 'trainDestinationStation': 'Wilson',\n", " 'trainDirection': 'South',\n", - " 'trainId': 159,\n", + " 'trainId': 141,\n", " 'trainMessage': 'Arriving'}],\n", " 'searchCriteria': '',\n", " 'searchCriteriaMatch': None,\n", @@ -875,7 +1198,7 @@ " 'Davisville Station',\n", " 'Don Mills Station',\n", " 'Donlands Station',\n", - " 'Downsview Station',\n", + " 'Downsview Park Station',\n", " 'Dufferin Station',\n", " 'Dundas Station',\n", " 'Dundas West Station',\n", @@ -883,9 +1206,11 @@ " 'Eglinton Station',\n", " 'Eglinton West Station',\n", " 'Finch Station',\n", + " 'Finch West Station',\n", " 'Glencairn Station',\n", " 'Greenwood Station',\n", " 'High Park Station',\n", + " 'Highway 407 Station',\n", " 'Islington Station',\n", " 'Jane Station',\n", " 'Keele Station',\n", @@ -903,11 +1228,13 @@ " 'Osgoode Station',\n", " 'Ossington Station',\n", " 'Pape Station',\n", + " 'Pioneer Village Station',\n", " 'Queen Station',\n", " 'Queen’s Park Station',\n", " 'Rosedale Station',\n", " 'Royal York Station',\n", " 'Runnymede Station',\n", + " 'Sheppard West Station',\n", " 'Sheppard-Yonge Station',\n", " 'Sherbourne Station',\n", " 'Spadina Station',\n", @@ -918,12 +1245,14 @@ " 'St Patrick Station',\n", " 'Summerhill Station',\n", " 'Union Station',\n", + " 'Vaughan Metropolitan Centre Station',\n", " 'Victoria Park Station',\n", " 'Warden Station',\n", " 'Wellesley Station',\n", " 'Wilson Station',\n", " 'Woodbine Station',\n", " 'York Mills Station',\n", + " 'York University Station',\n", " 'Yorkdale Station']}" ] }, @@ -938,91 +1267,79 @@ }, { "cell_type": "code", - "execution_count": 27, - "metadata": { - "collapsed": true - }, + "execution_count": 19, + "metadata": {}, "outputs": [], "source": [ - "def get_API_response(station_id):\n", - " payload = {\"stationId\":station_id,\n", - " \"searchCriteria\":''}\n", - " r = requests.get(base_url, params = payload) \n", - " return r.json()\n", - "\n", - "def insert_request_info(con, data, line_id, station_id):\n", - " request_row = {}\n", - " request_row['data_'] = data['data']\n", - " request_row['stationid'] = station_id\n", - " request_row['lineid'] = line_id\n", - " request_row['all_stations'] = data['allStations']\n", - " request_row['create_date'] = data['ntasData'][0]['createDate'].replace( 'T', ' ')\n", - " cursor = con.cursor()\n", - " cursor.execute(\"INSERT INTO public.requests(data_, stationid, lineid, all_stations, create_date)\"\n", - " \"VALUES(%(data_)s, %(stationid)s, %(lineid)s, %(all_stations)s, %(create_date)s)\"\n", - " \"RETURNING requestid\", request_row)\n", - " request_id = cursor.fetchone()[0]\n", - " con.commit()\n", - " return request_id\n", - "\n", - "def insert_ntas_data(con, ntas_data, request_id):\n", - " cursor = con.cursor()\n", - " sql = \"\"\"INSERT INTO public.ntas_data(\n", - " requestid, id, station_char, subwayline, system_message_type, \n", - " timint, traindirection, trainid, train_message)\n", - " VALUES (%(requestid)s, %(id)s, %(station_char)s, %(subwayline)s, %(system_message_type)s, \n", - " %(timint)s, %(traindirection)s, %(trainid)s, %(train_message)s);\n", - " \"\"\"\n", - " for record in ntas_data:\n", - " record_row ={}\n", - " record_row['requestid'] = request_id\n", - " record_row['id'] = record['id']\n", - " record_row['station_char'] = record['stationId']\n", - " record_row['subwayline'] = record['subwayLine']\n", - " record_row['system_message_type'] = record['systemMessageType']\n", - " record_row['timint'] = record['timeInt']\n", - " record_row['traindirection'] = record['trainDirection']\n", - " record_row['trainid'] = record['trainId']\n", - " record_row['train_message'] = record['trainMessage']\n", - " cursor.execute(sql, record_row)\n", - " con.commit()\n", - "\n", - "def query_all_stations(con):\n", - " data = {}\n", - " for line_id, stations in lines.items():\n", - " for station_id in stations:\n", - " data = get_API_response(station_id)\n", - " request_id = insert_request_info(con, data, line_id, station_id)\n", - " insert_ntas_data(con, data['ntasData'], request_id)\n" - ] - }, + "cursor = con.cursor()" + ] + }, { "cell_type": "code", - "execution_count": 15, - "metadata": { - "collapsed": false - }, + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "cursor.execute('''SELECT timint FROM ntas_data WHERE requestid = ''' + str(request_id) + ''' limit 10''')" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "rows = cursor.fetchall()" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "'Fest'" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "[(Decimal('0.0'),), (Decimal('4.534429333333333'),), (Decimal('7.379200571428571'),), (Decimal('1.3254445714285714'),), (Decimal('2.687013714285714'),), (Decimal('4.395184'),)]\n" + ] + } + ], + "source": [ + "print(rows)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.386878698412698233333333333\n" + ] } ], "source": [ - "test_string.replace('T','F')" + "print(numpy.mean(rows)) # Average (expected) wait time at St. George. Note this is not the true wait time." ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { - "display_name": "Python [default]", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -1036,7 +1353,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.5.2" + "version": "3.6.0" } }, "nbformat": 4, diff --git a/requirements.txt b/requirements.txt index 599c3f9..c4b376d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -psycopg2==2.6.2 +psycopg2 requests==2.12.4 chardet==2.3.0 aiodns==1.1.1