최대 요청값 500 > 1,000 업데이트, API KEY는 종관기상관측과 동일하므로 병합처리, 지역 설정을 config.yaml에서 할 수 있도록 수정.
This commit is contained in:
@ -11,11 +11,11 @@ from lib import db
|
|||||||
from lib import db_schema
|
from lib import db_schema
|
||||||
|
|
||||||
|
|
||||||
def test_num_of_rows(api_key, station_name='운정', date_str=None):
|
def test_num_of_rows(api_key, station_name, date_str=None):
|
||||||
if date_str is None:
|
if date_str is None:
|
||||||
date_str = (datetime.now() - timedelta(days=1)).strftime('%Y%m%d')
|
date_str = (datetime.now() - timedelta(days=1)).strftime('%Y%m%d')
|
||||||
|
|
||||||
max_rows = 500
|
max_rows = 1000
|
||||||
min_rows = 100
|
min_rows = 100
|
||||||
|
|
||||||
while max_rows >= min_rows:
|
while max_rows >= min_rows:
|
||||||
@ -43,7 +43,7 @@ def test_num_of_rows(api_key, station_name='운정', date_str=None):
|
|||||||
print("[WARN] 최소 numOfRows 값(100)도 실패했습니다. 기본값 100 사용")
|
print("[WARN] 최소 numOfRows 값(100)도 실패했습니다. 기본값 100 사용")
|
||||||
return 100
|
return 100
|
||||||
|
|
||||||
def fetch_air_quality_data_range(start_date_str, end_date_str, api_key, num_of_rows=100, station_name='운정', page_no=1):
|
def fetch_air_quality_data_range(start_date_str, end_date_str, api_key, station_name, num_of_rows=100, page_no=1):
|
||||||
url = "http://apis.data.go.kr/B552584/ArpltnStatsSvc/getMsrstnAcctoRDyrg"
|
url = "http://apis.data.go.kr/B552584/ArpltnStatsSvc/getMsrstnAcctoRDyrg"
|
||||||
params = {
|
params = {
|
||||||
'serviceKey': api_key,
|
'serviceKey': api_key,
|
||||||
@ -122,7 +122,8 @@ def get_latest_date(conn, table):
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
config = db.load_config()
|
config = db.load_config()
|
||||||
api_key = config['air_quality_api']['service_key']
|
api_key = config['DATA_API']['serviceKey']
|
||||||
|
station_name = config['DATA_API']['air']['station_name']
|
||||||
debug = config.get('debug', False)
|
debug = config.get('debug', False)
|
||||||
force_update = config.get('force_update', False)
|
force_update = config.get('force_update', False)
|
||||||
|
|
||||||
@ -132,7 +133,7 @@ def main():
|
|||||||
with engine.connect() as conn:
|
with engine.connect() as conn:
|
||||||
trans = conn.begin()
|
trans = conn.begin()
|
||||||
try:
|
try:
|
||||||
start_date = datetime.strptime("2017-01-01", "%Y-%m-%d").date()
|
start_date = datetime.strptime("2020-01-01", "%Y-%m-%d").date()
|
||||||
end_date = (datetime.now() - timedelta(days=1)).date()
|
end_date = (datetime.now() - timedelta(days=1)).date()
|
||||||
|
|
||||||
optimal_num_rows = test_num_of_rows(api_key, date_str=start_date.strftime('%Y%m%d'))
|
optimal_num_rows = test_num_of_rows(api_key, date_str=start_date.strftime('%Y%m%d'))
|
||||||
@ -151,7 +152,7 @@ def main():
|
|||||||
if debug:
|
if debug:
|
||||||
print(f"[DEBUG] {start_str} ~ {end_str} 데이터 요청 중...")
|
print(f"[DEBUG] {start_str} ~ {end_str} 데이터 요청 중...")
|
||||||
|
|
||||||
items = fetch_air_quality_data_range(start_str, end_str, api_key, num_of_rows=optimal_num_rows)
|
items = fetch_air_quality_data_range(start_str, end_str, api_key, station_name, num_of_rows=optimal_num_rows)
|
||||||
|
|
||||||
if items:
|
if items:
|
||||||
save_data_to_db(items, conn, table, force_update=force_update, debug=debug)
|
save_data_to_db(items, conn, table, force_update=force_update, debug=debug)
|
||||||
|
|||||||
Reference in New Issue
Block a user