반응형

class BUS():

        def __init__(self):

                self.key = "KQdls/1w4hLXiAkOO8N46/1rvqV26sMnIvRYj1cg75yCl5aR9yRRJtMkxeOJ8Zx2MlKIVajs4ZVJMrNEgnGOUA=="; # 창원시 버스 API홈페이지 샘플 key사용.

                self.url = "http://openapi.changwon.go.kr/rest/bis/"

# 문서로 저장된 기반정보 열기

                BusI2N = open("./howie/BusID-NM.txt","r") 

                BusN2I = open("./howie/BusNM-ID.txt","r")

                StatI2N = open("./howie/StationID-NM.txt","r")

                StatN2I = open("./howie/StationNM-ID.txt","r")


                self.BusI2N_dic = {}


                self.BusN2I_dic = {}


                self.StatI2N_dic = {}


                self.StatN2I_dic = {}




                for BIN in BusI2N:

                        tmp = BIN.split(':')

                        self.BusI2N_dic[tmp[0]] = tmp[1].replace('\n','')


                for BNI in BusN2I:

                        tmp = BNI.split(':')

                        self.BusN2I_dic[tmp[0]] = tmp[1].replace('\n','')


                for SIN in StatI2N:

                        tmp = SIN.split(':')

                        self.StatI2N_dic[tmp[0]] = tmp[1].replace('\n','')


                for SNI in StatN2I:

                        tmp = SNI.split(':')

                        self.StatN2I_dic[tmp[0]] = tmp[1].replace('\n','')



        def station(self,station):

                station = station.replace(" ","")

                if station not in self.StatN2I_dic.keys():

                        print "no station"

                        return


                station = self.StatN2I_dic[station]

                full = self.url + "BusArrives/?serviceKey=" + self.key + "&station="+station       # 정류장 도착정보 url

 data = requests.get(full).content                                                                        # requests라이브러리를 사용하여 xml 데이터 획득


                temp = minidom.parseString(data)                                                                     #  minidom라이브러리를 사용하여 스트링 파싱


                row = temp.getElementsByTagName('row') # row요소를 찾는다


                arrive_set = set()


                for el in row:

                        TMP = el.getElementsByTagName('ROUTE_ID')[0].childNodes[0].data        # row안 ROUTE_ID를 찾아 저장된 값을 얻는다.

                        if TMP in self.BusI2N_dic.keys():

                                arrive_set.add(self.BusI2N_dic[TMP])


                return arrive_set

반응형

'프로그래밍 > Python' 카테고리의 다른 글

[Python]sqlalchemy 사용법  (0) 2022.12.07
[PYTHON] 파이썬 함수 매개변수  (0) 2022.08.11
[Python] flask_restful fields  (0) 2021.10.08
Single Linked list 구현  (0) 2018.03.22

+ Recent posts