Browse Source

Initial commit

commit
dabad92585
6 changed files with 195033 additions and 0 deletions
  1. 3
    0
      .gitignore
  2. 21
    0
      LICENSE.md
  3. 17970
    0
      Plot.ipynb
  4. 23
    0
      README.md
  5. 57
    0
      bus_68_datapoints.py
  6. 176959
    0
      bus_68_datapoints.sql

+ 3
- 0
.gitignore View File

@@ -0,0 +1,3 @@
1
+*.pdf
2
+.ipynb_checkpoints
3
+*.db

+ 21
- 0
LICENSE.md View File

@@ -0,0 +1,21 @@
1
+The MIT License (MIT)
2
+
3
+Copyright (c) 2018 Phyks (Lucas Verney)
4
+
5
+Permission is hereby granted, free of charge, to any person obtaining a copy
6
+of this software and associated documentation files (the "Software"), to deal
7
+in the Software without restriction, including without limitation the rights
8
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+copies of the Software, and to permit persons to whom the Software is
10
+furnished to do so, subject to the following conditions:
11
+
12
+The above copyright notice and this permission notice shall be included in all
13
+copies or substantial portions of the Software.
14
+
15
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+SOFTWARE.

+ 17970
- 0
Plot.ipynb
File diff suppressed because it is too large
View File


+ 23
- 0
README.md View File

@@ -0,0 +1,23 @@
1
+RATP Bus 68 OpenData
2
+====================
3
+
4
+Here you will find the code I used to analyze and plot some waiting times for
5
+the bus 68 operated by RATP in Paris. Feel free to reuse and adapt with other
6
+lines.
7
+
8
+Blog post is available at [https://phyks.me/2018/12/suivi-des-passages-de-bus-a-partir-des-donnees-ouvertes-didfm.html](https://phyks.me/2018/12/suivi-des-passages-de-bus-a-partir-des-donnees-ouvertes-didfm.html#suivi-des-passages-de-bus-a-partir-des-donnees-ouvertes-didfm).
9
+
10
+## Files included
11
+
12
+* `bus_68_datapoints.py` is the data collection script (to be run by a
13
+    crontask for instance).
14
+* `bus_68_datapoints.sql` is a SQL dump of the SQLite database I have storing
15
+    the data I collected. The data was collected from [the STIF
16
+    API](https://api-lab-trone-stif.opendata.stif.info/pages/api-test-temps-reel/)
17
+    (ODbL license).
18
+* `Plot.ipynb` is a Jupyter Notebook to analyze the data.
19
+
20
+
21
+## License
22
+
23
+This code is licensed under an MIT license.

+ 57
- 0
bus_68_datapoints.py View File

@@ -0,0 +1,57 @@
1
+#!/usr/bin/env python
2
+import datetime
3
+import os
4
+import sys
5
+
6
+import peewee
7
+import requests
8
+
9
+from playhouse.db_url import connect
10
+
11
+db = connect(os.environ.get('DATABASE', 'sqlite:///bus_68_datapoints.db'))
12
+DEBUG = os.environ.get('DEBUG', False)
13
+
14
+
15
+class DataPoint(peewee.Model):
16
+    class Meta:
17
+        database = db
18
+
19
+    datetime = peewee.DateTimeField(
20
+        default=datetime.datetime.now()
21
+    )
22
+    lineDirection = peewee.CharField(max_length=255)
23
+    code = peewee.CharField(max_length=255)
24
+    time = peewee.CharField(max_length=255)
25
+
26
+
27
+if __name__ == '__main__':
28
+    db.connect()
29
+    db.create_tables([DataPoint])
30
+
31
+    LINE_ID = '100100068:68'  # Bus 68
32
+    # STOP_POINT_ID can be found using Navitia API, see
33
+    # https://canaltp.github.io/navitia-playground/play.html?request=https%3A%2F%2Fapi.navitia.io%2Fv1%2Fcoverage%2Ffr-idf%2Flines%2Fline%253AOIF%253A100100068%253A68OIF442%2Fstop_areas%3F
34
+    STOP_POINT_ID = 'stopPoint:59:5138213'  # "Maurice Arnoux" stop
35
+    API_KEY = 'PUT_YOUR_API_KEY_HERE'
36
+    r = requests.get(
37
+        'https://api-lab-trone-stif.opendata.stif.info/service/tr-vianavigo/departures?line_id=%s&stop_point_id=%s&apikey=%s'
38
+        % (LINE_ID, STOP_POINT_ID, API_KEY)
39
+    )
40
+    try:
41
+        r.raise_for_status()
42
+    except requests.exceptions.HTTPError:
43
+        if DEBUG:
44
+            raise
45
+        # Ignore
46
+        sys.exit()
47
+    j = r.json()
48
+
49
+    for item in j:
50
+        print(item)
51
+        r = DataPoint(
52
+            lineDirection=item['lineDirection'],
53
+            code=item['code'],
54
+            time=item.get('time', item.get('schedule'))
55
+        )
56
+        r.save()
57
+    db.close()

+ 176959
- 0
bus_68_datapoints.sql
File diff suppressed because it is too large
View File