Skip to content

Commit

Permalink
Merge pull request #34 from WooilJeong/develop
Browse files Browse the repository at this point in the history
Add: Kosis 공유서비스 Open API 추가
  • Loading branch information
WooilJeong authored Oct 21, 2022
2 parents 3c41d54 + 9d44d1c commit 2b607f8
Show file tree
Hide file tree
Showing 33 changed files with 2,453 additions and 153 deletions.
Binary file modified .DS_Store
Binary file not shown.
6 changes: 3 additions & 3 deletions PublicDataReader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
from .config.info import __version__, __author__, __contact__, __github__

__all__ = [
"__version__", "__author__", "__contact__", "__github__",
"Transaction", "Building", "StoreInfo", "Transportation", "code_bdong"
]
"__version__", "__author__", "__contact__", "__github__",
"Transaction", "Building", "StoreInfo", "Transportation", "Kosis", "code_bdong"
]
4 changes: 2 additions & 2 deletions PublicDataReader/config/info.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "1.0.1"
__version__ = "1.0.2"
__author__ = "정우일(Wooil Jeong)"
__contact__ = "[email protected]"
__github__ = "https://github.com/WooilJeong/PublicDataReader"
__github__ = "https://github.com/WooilJeong/PublicDataReader"
5 changes: 4 additions & 1 deletion PublicDataReader/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@
# 서울시 지하철호선별 역별 승하차 인원 정보 Open API
from PublicDataReader.Seoul.transportation import Transportation

# KOSIS Open API
from PublicDataReader.kosis.kosis import Kosis

# 법정동코드 데이터 조회
from PublicDataReader.utils.code import code_bdong
from PublicDataReader.utils.code import code_bdong
Empty file.
108 changes: 108 additions & 0 deletions PublicDataReader/kosis/kosis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
"""
KOSIS Open API Python Module
"""

import requests
import pandas as pd


class Kosis:
"""KOSIS 공유서비스 클래스
KOSIS 공유서비스에서 발급받은 사용자 인증키를 입력받아 초기화합니다.
Parameters
----------
apiKey : str
KOSIS 공유서비스에서 발급받은 사용자 인증키
serviceName : str
KOSIS 공유서비스 서비스명
Examples
--------
>>> import PublicDataReader as pdr
>>> # KOSIS 공유서비스 OPEN API 인스턴스 생성
>>> apiKey = "YOUR_API_KEY"
>>> serviceName = "KOSIS통합검색" # (예시) KOSIS통합검색, 통계설명, 통계표설명, 통계목록, 통계자료
>>> kosis = pdr.Kosis(apiKey, serviceName)
"""

def __init__(self, apiKey, serviceName):
self.apiKey = apiKey
url_dict = {
"KOSIS통합검색": "https://kosis.kr/openapi/statisticsSearch.do?method=getList",
"통계설명": "https://kosis.kr/openapi/statisticsExplData.do?method=getList",
"통계표설명": "https://kosis.kr/openapi/statisticsData.do?method=getMeta",
"통계목록": "https://kosis.kr/openapi/statisticsList.do?method=getList",
"통계자료": "https://kosis.kr/openapi/Param/statisticsParameterData.do?method=getList",
}
self.url = url_dict.get(serviceName)

def get_data(self, **kwargs):
"""API 호출
KOSIS 공유서비스 API를 호출하여 데이터를 반환합니다.
Parameters
----------
**kwargs : dict
API 호출에 필요한 파라미터
Returns
-------
DataFrame
API 호출 결과를 DataFrame 형태로 반환합니다.
Examples
--------
>>> import PublicDataReader as pdr
>>> # KOSIS 공유서비스 OPEN API 인스턴스 생성
>>> apiKey = "YOUR_API_KEY"
>>> serviceName = "KOSIS통합검색"
>>> kosis = pdr.Kosis(apiKey, serviceName)
>>> # 파라미터 설정
>>> orgId = "101"
>>> tblId = "DT_1B040A3"
>>> metaItm = "ALL"
>>> # 데이터 조회
>>> df = kosis.get_data(orgId=orgId, tblId=tblId, metaItm=metaItm)
"""
try:
kwargs["apiKey"] = self.apiKey
kwargs["format"] = "json"
kwargs["jsonVD"] = "Y"
kwargs["jsonMVD"] = "Y"
if kwargs.get("detailServiceName"):
type_dict = {
"통계표명칭": "TBL",
"기관명칭": "ORG",
"수록정보": "PRD",
"분류항목": "ITM",
"주석": "CMMT",
"단위": "UNIT",
"출처": "SOURCE",
"가중치": "WGT",
"자료갱신일": "NCD",
}
kwargs['type'] = type_dict.get(kwargs.get("detailServiceName"))
kwargs.pop("detailServiceName")
except:
print("Incorrect Value!")
return None

try:
res = requests.get(self.url, params=kwargs).json()
except:
print("Request Failed!")
return None

try:
if type(res) == dict:
if res.get("errMsg"):
print(res.get("errMsg"))
return None
else:
return pd.DataFrame(res)
except:
print("Data Frame Failed!")
return None
140 changes: 0 additions & 140 deletions develop_test.ipynb

This file was deleted.

1 change: 1 addition & 0 deletions docs/PublicDataReader.PublicDataPortal.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<li class="toctree-l4 current"><a class="current reference internal" href="#">PublicDataReader.PublicDataPortal package</a></li>
<li class="toctree-l4"><a class="reference internal" href="PublicDataReader.Seoul.html">PublicDataReader.Seoul package</a></li>
<li class="toctree-l4"><a class="reference internal" href="PublicDataReader.config.html">PublicDataReader.config package</a></li>
<li class="toctree-l4"><a class="reference internal" href="PublicDataReader.kosis.html">PublicDataReader.kosis package</a></li>
<li class="toctree-l4"><a class="reference internal" href="PublicDataReader.utils.html">PublicDataReader.utils package</a></li>
</ul>
</li>
Expand Down
1 change: 1 addition & 0 deletions docs/PublicDataReader.Seoul.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<li class="toctree-l4"><a class="reference internal" href="PublicDataReader.PublicDataPortal.html">PublicDataReader.PublicDataPortal package</a></li>
<li class="toctree-l4 current"><a class="current reference internal" href="#">PublicDataReader.Seoul package</a></li>
<li class="toctree-l4"><a class="reference internal" href="PublicDataReader.config.html">PublicDataReader.config package</a></li>
<li class="toctree-l4"><a class="reference internal" href="PublicDataReader.kosis.html">PublicDataReader.kosis package</a></li>
<li class="toctree-l4"><a class="reference internal" href="PublicDataReader.utils.html">PublicDataReader.utils package</a></li>
</ul>
</li>
Expand Down
5 changes: 3 additions & 2 deletions docs/PublicDataReader.config.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<script src="_static/js/theme.js"></script>
<link rel="index" title="색인" href="genindex.html" />
<link rel="search" title="검색" href="search.html" />
<link rel="next" title="PublicDataReader.utils package" href="PublicDataReader.utils.html" />
<link rel="next" title="PublicDataReader.kosis package" href="PublicDataReader.kosis.html" />
<link rel="prev" title="PublicDataReader.Seoul package" href="PublicDataReader.Seoul.html" />
</head>

Expand All @@ -46,6 +46,7 @@
<li class="toctree-l4"><a class="reference internal" href="PublicDataReader.PublicDataPortal.html">PublicDataReader.PublicDataPortal package</a></li>
<li class="toctree-l4"><a class="reference internal" href="PublicDataReader.Seoul.html">PublicDataReader.Seoul package</a></li>
<li class="toctree-l4 current"><a class="current reference internal" href="#">PublicDataReader.config package</a></li>
<li class="toctree-l4"><a class="reference internal" href="PublicDataReader.kosis.html">PublicDataReader.kosis package</a></li>
<li class="toctree-l4"><a class="reference internal" href="PublicDataReader.utils.html">PublicDataReader.utils package</a></li>
</ul>
</li>
Expand Down Expand Up @@ -102,7 +103,7 @@ <h2>Submodules<a class="headerlink" href="#submodules" title="이 표제에 대
</div>
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
<a href="PublicDataReader.Seoul.html" class="btn btn-neutral float-left" title="PublicDataReader.Seoul package" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="PublicDataReader.utils.html" class="btn btn-neutral float-right" title="PublicDataReader.utils package" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
<a href="PublicDataReader.kosis.html" class="btn btn-neutral float-right" title="PublicDataReader.kosis package" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>

<hr/>
Expand Down
Loading

0 comments on commit 2b607f8

Please sign in to comment.