V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
stephenroger
V2EX  ›  分享创造

简单做了一个上海公交信息 API 查询

  •  
  •   stephenroger · 2019-01-31 11:43:48 +08:00 · 4049 次点击
    这是一个创建于 2124 天前的主题,其中的信息可能已经有所发展或是发生改变。

    github: https://github.com/gyfstephen/shanghai-bus

    上海发布里查询准确度不错,但是查询体验不是很好,会经常卡住,所以就简单抓了下上海发布查公交的接口自己集成了实现一个 API,现在应该只支持查询车辆名为纯数字的线路

    数据来源:上海发布

    API 接口:

    • 查询车辆站台信息

      /api/v1/stations/<string:stop_type>/<int:number>/
      
    • 查询车辆到站信息

      /api/v1/stops/<string:stop_type>/<int:number>/
      

    示例:

    • 查询公交 100 上行站台信息 /api/v1/stations/up/110/
    {
        "data": {
            "1": "吉浦路仁德路", 
            "2": "武川路武东路", 
            "3": "武川路政立路", 
            "4": "政立路国权北路", 
            "5": "政立路逸仙路", 
            "6": "逸仙路场中路", 
            "7": "江湾(逸仙路)", 
            "8": "逸仙路纪念路", 
            "9": "大柏树(曲阳路)", 
            "10": "曲阳路中山北二路", 
            "11": "大连西路曲阳路", 
            "12": "欧阳路大连西路", 
            "13": "欧阳路祥德路", 
            "14": "欧阳路四达路", 
            "15": "欧阳路吉祥路", 
            "16": "四平路溧阳路", 
            "17": "吴淞路海宁路", 
            "18": "吴淞路天潼路"
        }, 
        "error": 0, 
        "msg": ""
    }
    
    • 查询公交 100 上行到站信息 /api/v1/stops/up/110/
    {
        "data": [
            {
                "distance": 0, 
                "error": 0, 
                "plate": "沪 A-00737D", 
                "station_name": "吉浦路仁德路", 
                "stop_dis": 1, 
                "stop_id": 1, 
                "time": 929
            }, 
            {
                "distance": 1296, 
                "error": 0, 
                "plate": "沪 A-00737D", 
                "station_name": "武川路武东路", 
                "stop_dis": 1, 
                "stop_id": 2, 
                "time": 1048
            }, 
            {
                "distance": 1548, 
                "error": 0, 
                "plate": "沪 A-00737D", 
                "station_name": "武川路政立路", 
                "stop_dis": 2, 
                "stop_id": 3, 
                "time": 1110
            }, 
            {
                "distance": 2203, 
                "error": 0, 
                "plate": "沪 A-00737D", 
                "station_name": "政立路国权北路", 
                "stop_dis": 3, 
                "stop_id": 4, 
                "time": 1289
            }, 
            {
                "distance": 576, 
                "error": 0, 
                "plate": "沪 A-01825D", 
                "station_name": "政立路逸仙路", 
                "stop_dis": 1, 
                "stop_id": 5, 
                "time": 88
            }, 
            {
                "distance": 1175, 
                "error": 0, 
                "plate": "沪 A-01825D", 
                "station_name": "逸仙路场中路", 
                "stop_dis": 2, 
                "stop_id": 6, 
                "time": 208
            }, 
            {
                "distance": 1590, 
                "error": 0, 
                "plate": "沪 A-01825D", 
                "station_name": "江湾(逸仙路)", 
                "stop_dis": 3, 
                "stop_id": 7, 
                "time": 269
            }, 
            {
                "distance": 1882, 
                "error": 0, 
                "plate": "沪 A-01825D", 
                "station_name": "逸仙路纪念路", 
                "stop_dis": 4, 
                "stop_id": 8, 
                "time": 388
            }, 
            {
                "distance": 3142, 
                "error": 0, 
                "plate": "沪 A-01825D", 
                "station_name": "大柏树(曲阳路)", 
                "stop_dis": 5, 
                "stop_id": 9, 
                "time": 628
            }, 
            {
                "distance": 293, 
                "error": 0, 
                "plate": "沪 A-07566D", 
                "station_name": "曲阳路中山北二路", 
                "stop_dis": 1, 
                "stop_id": 10, 
                "time": 31
            }, 
            {
                "distance": 1642, 
                "error": 0, 
                "plate": "沪 A-07566D", 
                "station_name": "大连西路曲阳路", 
                "stop_dis": 2, 
                "stop_id": 11, 
                "time": 331
            }, 
            {
                "distance": 2170, 
                "error": 0, 
                "plate": "沪 A-07566D", 
                "station_name": "欧阳路大连西路", 
                "stop_dis": 3, 
                "stop_id": 12, 
                "time": 449
            }, 
            {
                "distance": 2579, 
                "error": 0, 
                "plate": "沪 A-07566D", 
                "station_name": "欧阳路祥德路", 
                "stop_dis": 4, 
                "stop_id": 13, 
                "time": 569
            }, 
            {
                "distance": 2867, 
                "error": 0, 
                "plate": "沪 A-07566D", 
                "station_name": "欧阳路四达路", 
                "stop_dis": 5, 
                "stop_id": 14, 
                "time": 630
            }, 
            {
                "distance": 3196, 
                "error": 0, 
                "plate": "沪 A-07566D", 
                "station_name": "欧阳路吉祥路", 
                "stop_dis": 6, 
                "stop_id": 15, 
                "time": 689
            }, 
            {
                "distance": 4019, 
                "error": 0, 
                "plate": "沪 A-07566D", 
                "station_name": "四平路溧阳路", 
                "stop_dis": 7, 
                "stop_id": 16, 
                "time": 870
            }, 
            {
                "distance": 728, 
                "error": 0, 
                "plate": "沪 A-05870D", 
                "station_name": "吴淞路海宁路", 
                "stop_dis": 1, 
                "stop_id": 17, 
                "time": 90
            }, 
            {
                "distance": 1329, 
                "error": 0, 
                "plate": "沪 A-05870D", 
                "station_name": "吴淞路天潼路", 
                "stop_dis": 2, 
                "stop_id": 18, 
                "time": 330
            }
        ], 
        "error": 0, 
        "msg": ""
    }
    

    TODO:

    • [ ] 支持查询非数字的线路
    • [ ] 缓存时间优化
    • [ ] 支持查询特定站到站时间
    • [ ] 支持查询同一站的多条线路
    4 条回复    2019-02-02 12:57:36 +08:00
    zli
        1
    zli  
       2019-01-31 13:00:01 +08:00
    为什么不用巴士通的 API,

    那个接口不是更直接更全面?
    exceloo
        2
    exceloo  
       2019-01-31 13:16:13 +08:00
    这个擅自搭建 api 获取公共信息有没有什么风险?就像自己画地图被抓起来那样...
    Senventise
        3
    Senventise  
       2019-01-31 13:47:38 +08:00 via Android
    之前也写过一个,话说 sid 我记得好像就是线路名的 md5 啊,为什么查不了非数字线路?
    btw,上海公交官方 app 也不错,用的是同一套 api
    kimown
        4
    kimown  
       2019-02-02 12:57:36 +08:00 via Android
    @exceloo

    有家公司,产品叫 车来了,而且融资了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2665 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 05:41 · PVG 13:41 · LAX 21:41 · JFK 00:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.