GithubHelp home page GithubHelp logo

westjr's Introduction

WestJR

Python Versions PyPI pre-commit.ci status GitHubActions

JR西日本列車走行位置 非公式API Pythonライブラリ

  • 列車走行位置取得 (/api/v3/{LINE}.json)
  • メンテナンス予定取得 (/api/v3/area_{AREA}_maintenance.json)
  • 路線名取得 (/api/v3/area_{AREA}_master.json)
  • 駅一覧取得 (/api/v3/{LINE}_st.json)
  • 運行情報取得 (/api/v3/area_{AREA}_trafficinfo.json)
  • 列車環境取得 (/api/v3/trainmonitorinfo.json)
  • 列車走行位置駅名,列車停車種別の変換

Notice

  • 動作を完全には確認していません.

Installation

pip install WestJR

Usage

Wiki に情報があります.

import westjr
jr = westjr.WestJR()

# あらかじめ area や line をセットする
jr = westjr.WestJR(line="kobesanyo", area="kinki")

Example

列車走行位置取得

print(jr.get_trains())
# TrainPos(update='2023-03-21T16:54:54.612Z', trains=[TrainsItem(no='502C', ...

メンテナンス予定取得

print(jr.get_maintenance())
# 平常時:
# AreaMaintenance(status=0, notification=Notification(groupId=0, text='', duration=''), ...
# 異常時:
# AreaMaintenance(status=1, notification=Notification(groupId=2023012802, text='1月24日から1月31日を, ...

路線一覧取得

print(jr.get_lines())
# AreaMaster(lines={'ako': Line(name='赤穂線', range='相生〜播州赤穂', relatelines=None, st='...

駅一覧取得

print(jr.get_stations())
# Stations(stations=[StationsItem(info=Info(name='新大阪', code='0415', stopTrains=[1, 2, 5], typeNotice=None, ...

運行情報取得

print(jr.get_traffic_info())
# 平常時:
# TrainInfo(lines={}, express={})
# 異常時:
# TrainInfo(lines={'bantan': Info_LineItem(...)}, express={'bantan': Info_ExpressItem(...)})

エリア名一覧表示

print(jr.areas)
# ['hokuriku', 'kinki', 'okayama', 'hiroshima', 'sanin']

路線名一覧表示

print(jr.lines)
# ['hokuriku', 'kobesanyo', 'hokurikubiwako', 'kyoto', 'ako', 'kosei', 'kusatsu', 'nara', 'sagano', 'sanin1', 'sanin2', 'osakahigashi', 'takarazuka']

列車環境取得

print(jr.get_train_monitor_info().trains["3489M"][0].cars[0].congestion)
# 26(%)
print(jr.get_train_monitor_info().trains["3489M"][0].cars[0].temp)
# 23(°C)

駅に停車する種別を id から名称に変換する

station = jr.get_stations(line="kyoto").stations[0]

print(station.info.name)
# 山科

print(jr.convert_stopTrains(station.info.stopTrains))
# ['新快速', '快速', '特急']

列車走行位置の場所を前駅と次駅の名前に変換する

train = jr.get_trains(line="kobesanyo").trains
tr = train[0]
prev, next = jr.convert_pos(train=tr)
print(prev)
# 塚本

Contribution

  • develop ブランチにお願いします
  • インストールしていなければ poetry をインストールしてください
  • 初回は poetry install && poetry run pre-commit install を実行することで poetrypre-commit のセットアップが行えます
  • poetry shell を実行すると仮想環境で開発することができます

westjr's People

Contributors

unyacat avatar pre-commit-ci[bot] avatar eggplants avatar dependabot[bot] avatar

Stargazers

 avatar Azarashi avatar yumizu avatar  avatar nisylvania avatar patapata avatar  avatar pokoyakazan avatar  avatar Anise avatar  avatar  avatar Tetsuji Ochiai avatar TAMIYANOMAR avatar Kaito Sugimoto avatar みっきー avatar Lesmiscore avatar tsukumi avatar  avatar Takehiro Ogura avatar donabe8898 avatar  avatar  avatar

Watchers

 avatar

westjr's Issues

WestJR.get_stations実行時のValidationError

概要

WestJR.get_stations 実行時にValidationErrorが発生する

再現手順

import westjr

jrw = westjr.WestJR()
print(jrw.get_stations(line="osakaloop")) 

発生したエラー

Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\concurrent\futures\_base.py", line 458, in result
    return self.__get_result()
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\concurrent\futures\_base.py", line 403, in __get_result        
    raise self._exception
  File "<console>", line 1, in <module>
  File "C:\Users\midori\IdeaProjects\async-westjr\westjr\api.py", line 64, in _request
    return model.parse_obj(data)
  File "pydantic\main.py", line 526, in pydantic.main.BaseModel.parse_obj
  File "pydantic\main.py", line 341, in pydantic.main.BaseModel.__init__
pydantic.error_wrappers.ValidationError: 2 validation errors for Stations
stations -> 0 -> design -> upside -> 0 -> line
  none is not an allowed value (type=type_error.none.not_allowed)
stations -> 18 -> design -> downside -> 0 -> line
  none is not an allowed value (type=type_error.none.not_allowed)

期待する動作

get_stationsでStationsが返る

動作環境

OS: Windows 11 Home
Python: 3.10.11 (tags/v3.10.11:7d4cc5a, Apr 5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)]
Lib: 0.4

pydanticを用いたjsonレスポンスの検証を行う

現在、レスポンスの TypedDict による型付けは typing.cast でやっていますが、これだと JSON が TypedDict の定義に妥当でなくとも型が付いてしまいます。

TS/JSでいう zod のように、 pydantic ライブラリでランタイム時にデータが型ヒントに対して妥当か検証することができるので、そちらを使うよう変更したいです。

参照: https://docs.pydantic.dev/usage/models/#model-creation-from-namedtuple-or-typeddict

pre-commit.ci

https://pre-commit.ciにこのリポジトリを登録すると、pre-commitを速く、簡単に回せます。ご検討ください。

また有効化されましたら、ここを削除してください。

Poetryへ移行する

dependabotは setup.cfg のパッケージ更新を見てくれないので、poetryに移行したいです。

poetry install && poetry shell で仮装環境が容易に使用できたり、tomlで設定ファイルが書けたりと開発体験が向上します。

`area_kinki_trafficinfo`: `ValidationError: 2 validation errors for TrainInfo`

Ref: #51

area_kinki_trafficinfoTrainInfofrom_Optional にする必要がある。


https://github.com/unyacat/westjr/actions/runs/5860277526/job/15888001740?pr=59

westjr/api.py:46: ValidationError
=========================== short test summary info ============================
FAILED tests/test_example.py::test_get_traffic_info - pydantic_core._pydantic_core.ValidationError: 2 validation errors for TrainInfo
lines.kinokuni.section.from
  Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.1/v/string_type
lines.wakayama1.section.from
  Input should be a valid string [type=string_type, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.1/v/string_type

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.