Skip to content

Commit 7869bb5

Browse files
committed
update:ログ保持
1 parent 1d54f08 commit 7869bb5

File tree

8 files changed

+170
-1
lines changed

8 files changed

+170
-1
lines changed

.github/pull_request_template.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## PR 概要
2+
3+
xxxxx
4+
5+
## チェックリスト
6+
7+
- PR作成時に確認
8+
- [ ] コーディング規約を熟読している。
9+
- [ ] マージ先が正しく設定されている。
10+
- [ ] アサイニーを設定している。
11+
- [ ] レビューアーを設定している。
12+
- レビュー前に確認
13+
- [ ] マージ先が正しく設定されている。
14+
- [ ] 検証結果のスクショを添付している。
15+
- [ ] Lint系ツールのエラーが無くなっている。
16+
17+
## TODO
18+
- [ ] xxxx
19+
- [ ] xxxx
20+
21+
## この PR の影響範囲
22+
xxxxx(controller)
23+
yyyyy(model)
24+
25+
## 動作確認内容
26+
- [ ] 実行時にエラーが発生しないことを確認。

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,11 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
132+
133+
.DS_Store
134+
docker/mysql/data
135+
.env
136+
.bash_history
137+
.vscode

Dockerfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM python:3
2+
USER root
3+
4+
RUN apt-get update
5+
RUN apt-get -y install locales && \
6+
localedef -f UTF-8 -i ja_JP ja_JP.UTF-8
7+
ENV LANG ja_JP.UTF-8
8+
ENV LANGUAGE ja_JP:ja
9+
ENV LC_ALL ja_JP.UTF-8
10+
ENV TZ JST-9
11+
ENV TERM xterm
12+
13+
RUN apt-get install -y vim less
14+
RUN pip install --upgrade pip
15+
RUN pip install --upgrade setuptools
16+
RUN pip install --upgrade requests datetime SQLAlchemy mysqlclient

README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,30 @@
1-
# Train-RealTime-info
1+
# Train-RealTime-info
2+
3+
## 蓄積データ
4+
5+
- JR
6+
- 中央線
7+
- 中央特快
8+
9+
10+
## テーブル定義(MySQL)
11+
### Train_Logーブル
12+
| 物理名 | 論理名 || null | key | default |
13+
| -- | -- | -- | -- | -- | -- |
14+
| id | id | UNSIGN INT(11) | no | prime | |
15+
| train_id | データID | VARCHAR(255) | | | |
16+
| odpt_type | ODPTタイプ | VARCHAR(255) | | | |
17+
| dc_date | 生成時刻 | VARCHAR(255) | | | |
18+
| dct_valid | 有効期限 | VARCHAR(255) | | | |
19+
| odpt_delay | 遅延情報 | INT(10) | | | |
20+
| odpt_railway | 路線名 | VARCHAR(255) | | | |
21+
| odpt_operator | 運営会社 | VARCHAR(255) | | | |
22+
| odpt_train_number | 列車番号 | VARCHAR(255) | | | |
23+
| odpt_train_type | 種別 | VARCHAR(255) | | | |
24+
| odpt_toStation | 次駅 | VARCHAR(255) | | | |
25+
| odpt_from_station | 出発駅 | VARCHAR(255) | | | |
26+
| odpt_rail_direction | 進行方向 | VARCHAR(255) | | | |
27+
| odpt_car_composition | 編成数 | INT(10) | | | |
28+
| odpt_destination_station | 行先 | VARCHAR(255) | | | |
29+
| created_at | 作成日 | TATETIME | no | | CURRENT_TIMESTAMP |
30+
| updated_at | 更新日 | TATETIME | no | | CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP |

docker-compose.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
version: '3'
2+
services:
3+
train-python:
4+
restart: always
5+
build: .
6+
env_file: .env
7+
container_name: 'train-python'
8+
working_dir: '/root/'
9+
tty: true
10+
volumes:
11+
- .:/root
12+
links:
13+
- mysql
14+
mysql:
15+
image: mysql:5.7
16+
restart: always
17+
container_name: 'train-mysql'
18+
environment:
19+
MYSQL_ROOT_PASSWORD: root
20+
MYSQL_DATABASE: train
21+
MYSQL_USER: train-user
22+
MYSQL_PASSWORD: train-user
23+
TZ: 'Asia/Tokyo'
24+
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
25+
volumes:
26+
- ./docker/mysql/data:/var/lib/mysql
27+
- ./docker/mysql:/etc/mysql/conf.d/my.cnf
28+
- ./docker/mysql/sql:/docker-entrypoint-initdb.d
29+
ports:
30+
- 3306:3306
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---- 【drop】 ----
2+
-- DROP TABLE IF EXISTS agency;
3+
---- 【drop】 ----
4+
5+
---- 【Create】 ----
6+
create table IF not exists train_log(
7+
id INT(11) AUTO_INCREMENT,
8+
train_id VARCHAR(255) ,
9+
odpt_type VARCHAR(255) ,
10+
dc_date VARCHAR(255) ,
11+
dct_valid VARCHAR(255) ,
12+
odpt_delay INT(11) ,
13+
odpt_railway VARCHAR(255) ,
14+
odpt_operator VARCHAR(255) ,
15+
odpt_train_number VARCHAR(255) ,
16+
odpt_train_type VARCHAR(255) ,
17+
odpt_to_station VARCHAR(255) ,
18+
odpt_from_station VARCHAR(255) ,
19+
odpt_rail_direction VARCHAR(255) ,
20+
odpt_destination_station VARCHAR(255) ,
21+
created_at Datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
22+
updated_at Datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
23+
PRIMARY KEY (id)
24+
) DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
25+
---- 【Create】 ----

docker/mysql/sql/init-database.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env bash
2+
#wait for the MySQL Server to come up
3+
#sleep 90s
4+
5+
#run the setup script to create the DB and the schema in the DB
6+
mysql -u root -proot train < "/docker-entrypoint-initdb.d/001-create-tables.sql"

train-api.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import requests
2+
import os
3+
import json
4+
from sqlalchemy import create_engine, text
5+
6+
def mysql_connector():
7+
return create_engine("mysql://root:root@mysql/train?charset=utf8")
8+
9+
def api_request():
10+
url = os.environ['tokyo-open-data-challenge-url'] + "odpt:Train" + "?odpt:operator=odpt.Operator:JR-East" + "&acl:consumerKey=" + os.environ['tokyo-open-data-challenge-token']
11+
res = requests.get(url)
12+
res = json.loads(res.content)
13+
print("取得件数:"+ str(len(res)))
14+
return res
15+
16+
def mysql_insert(engine, res):
17+
for value in res:
18+
if value['odpt:railway'] == "odpt.Railway:JR-East.ChuoRapid":
19+
q = text("INSERT INFO train_log (train_id, odpt_type, dc_date, dct_valid, odpt_delay, odpt_railway, odpt_operator, odpt_train_number, odpt_train_type, odpt_toStation, odpt_from_station, odpt_rail_direction, odpt_car_composition, odpt_destination_station) VALUES (%s, %s, %s, %s, %d, %s, %s, %s, %s, %s, %s, %s, %d, %s);" % (value['@id'], value['@type'], value['dc:date'], value['dct:valid'], value['odpt:delay'], value['odpt:railway'], value['odpt:operator'], value['odpt_train_number'], value['odpt_train_type'], value['odpt_toStation'], value['odpt_from_station'], value['odpt_rail_direction'], value['odpt_car_composition'], value['odpt_destination_station']))
20+
rs = engine.execute(q)
21+
print(rs)
22+
23+
def main():
24+
engine = mysql_connector()
25+
res = api_request()
26+
mysql_insert(engine, res)
27+
28+
if __name__ == '__main__':
29+
main()

0 commit comments

Comments
 (0)