Track realtime schedules of bus 68 in Paris.

bus_68_datapoints.py 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env python
  2. import datetime
  3. import os
  4. import sys
  5. import peewee
  6. import requests
  7. from playhouse.db_url import connect
  8. db = connect(os.environ.get('DATABASE', 'sqlite:///bus_68_datapoints.db'))
  9. DEBUG = os.environ.get('DEBUG', False)
  10. class DataPoint(peewee.Model):
  11. class Meta:
  12. database = db
  13. datetime = peewee.DateTimeField(
  14. default=datetime.datetime.now()
  15. )
  16. lineDirection = peewee.CharField(max_length=255)
  17. code = peewee.CharField(max_length=255)
  18. time = peewee.CharField(max_length=255)
  19. if __name__ == '__main__':
  20. db.connect()
  21. db.create_tables([DataPoint])
  22. LINE_ID = '100100068:68' # Bus 68
  23. # STOP_POINT_ID can be found using Navitia API, see
  24. # 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
  25. STOP_POINT_ID = 'stopPoint:59:5138213' # "Maurice Arnoux" stop
  26. API_KEY = 'PUT_YOUR_API_KEY_HERE'
  27. r = requests.get(
  28. 'https://api-lab-trone-stif.opendata.stif.info/service/tr-vianavigo/departures?line_id=%s&stop_point_id=%s&apikey=%s'
  29. % (LINE_ID, STOP_POINT_ID, API_KEY)
  30. )
  31. try:
  32. r.raise_for_status()
  33. except requests.exceptions.HTTPError:
  34. if DEBUG:
  35. raise
  36. # Ignore
  37. sys.exit()
  38. j = r.json()
  39. for item in j:
  40. print(item)
  41. r = DataPoint(
  42. lineDirection=item['lineDirection'],
  43. code=item['code'],
  44. time=item.get('time', item.get('schedule'))
  45. )
  46. r.save()
  47. db.close()