본문 바로가기

Django

2022-05-03 개발일기 [Django, Fullcalendar]

반응형

구글 캘린더 API 연결 시 FullCalendar에서는 본인의 key로 발급받기 때문에 나 자신이 캘린더를 이용할 시 동일한 내용이 발견되는 것을 발견했다.. docs 조금만 들춰봐도 알 수 있는 거를 ㅠ.. 그래서 공공데이터 포털 Open API를 이용해서 공휴일 데이터를 받아와 api.py에서 바로 Django DB에 save(). 하는 방법을 택했다! 캘린더 부분은 이제 끝마침을 맺었다! 기준정보도 들어와서 DB작업도 끝났으니 얼추 프로젝트 마무리가 보인다 조금만 파이팅!

 

 

개발 스크립트[API.py]

def googleAPI(url, operation, params, serviceKey):
    import urllib.parse as urlparse
    params = urlparse.urlencode(params)
    request_query = url + '/' + operation + '?' + params + '&' + 'serviceKey' + '=' + serviceKey
    return request_query

year = 2022
# 일반 인증키(Encoding)
mykey = "
for month in range(1, 13):

    if month < 10:
        month = '0' + str(month)
    else:
        month = str(month)

    url = 'http://apis.data.go.kr/B090041/openapi/service/SpcdeInfoService'
    # 공휴일 정보 조회
    operation = 'getRestDeInfo'
    params = {'solYear': year, 'solMonth': month}

    request_query = googleAPI(url, operation, params, mykey)
    get_data = requests.get(request_query)

    if True == get_data.ok:
        soup = BeautifulSoup(get_data.content, 'html.parser')

        item = soup.findAll('item')

        for i in item:
            day = int(i.locdate.string[-2:])
            datename_list = i.find('datename').text
            locdate_list = i.find('locdate').text
            startdate = datetime.strptime(locdate_list, '%Y%m%d')

            string_list = ["title", "start"]
            dictionary = {string: i for i, string in enumerate(string_list)}
            dictionary["title"] = datename_list
            dictionary["start"] = startdate

            try:
                test_set = Calendar()
                test_set.title = dictionary['title']
                test_set.start_data = dictionary['start']
                test_set.end_data = dictionary['start']
                test_set.save()
            except:
                  pass
반응형