Skip to content

Commit 5a74a42

Browse files
committed
update:ループ処理の実装&テーブルカラムの修正
1 parent f310c13 commit 5a74a42

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

docker/mysql/sql/001-create-tables.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ create table IF not exists train_log(
1717
odpt_to_station VARCHAR(255) ,
1818
odpt_from_station VARCHAR(255) ,
1919
odpt_rail_direction VARCHAR(255) ,
20+
odpt_car_composition VARCHAR(255),
2021
odpt_destination_station VARCHAR(255) ,
2122
created_at Datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
2223
updated_at Datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

train-api.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import requests
22
import os
33
import json
4+
import time
5+
import datetime
46
from sqlalchemy import create_engine, text
57

68
def mysql_connector():
@@ -16,14 +18,20 @@ def api_request():
1618
def mysql_insert(engine, res):
1719
for value in res:
1820
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']))
21+
# print(value)
22+
q = text("INSERT INTO train_log(train_id, odpt_type, dc_date, dct_valid, odpt_delay, odpt_railway, odpt_operator, odpt_train_number, odpt_train_type, odpt_to_station, 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:trainNumber'], value['odpt:trainType'], value['odpt:toStation'], value['odpt:fromStation'], value['odpt:railDirection'], value['odpt:carComposition'], "a"))
2023
rs = engine.execute(q)
21-
print(rs)
24+
print("Insert Success!\n")
2225

2326
def main():
2427
engine = mysql_connector()
25-
res = api_request()
26-
mysql_insert(engine, res)
28+
value = 0
29+
while True:
30+
print("開始時間:"+str(datetime.datetime.now())+"__"+str(value+1)+"回目")
31+
res = api_request()
32+
mysql_insert(engine, res)
33+
value += 1
34+
time.sleep(10)
2735

2836
if __name__ == '__main__':
2937
main()

0 commit comments

Comments
 (0)