Making Python api using aws lamdba

Asked 2 years ago, Updated 2 years ago, 47 views

Hello, I'm a beginner who has been coding for about two months. I'm making a Kakao Talk chatbot! One of the functions is to call the bus route and arrival time at a particular stop You have to put url in the post method.

So I'm trying to make an api with aws lamdba, but I can't figure out how to fix it even if I keep trying to fix the code.

How do I write code to output those functions a1 and a2? Please!

import json
import requests
from bs4 import BeautifulSoup as akk

def lambda_handler(event, context):
    service_key = 'kxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    params= '&cityCode=33010&nodeId=CJB283000059&routeId=CJB270012000'

    open_api = ' http://openapi.tago.go.kr/openapi/service/ArvlInfoInqireService/getSttnAcctoSpcifyRouteBusArvlPrearngeInfoList?ServiceKey='+service_key+params

    res= requests.get(open_api)
    soup = akk(res.content,'html.parser')

    arrtimes = soup.find('arrtime')
    pres = soup.find('arrprevstationcnt')
    arrive = int(arrtimes.get_text())
    pre = int(pres.get_text())

    def a1():
        if arrive > 420:
            print ("You have more than 6 minutes to arrive")
        elif arrive > 361 and arrive <= 420:
            print (6 minutes left until arrival)
        elif arrive >300 and arrive <= 360:
            print("5 minutes to arrive")
        elif arrive > 240 and arrive <= 300:
            print("4 minutes to arrive")
        elif arrive > 180 and arrive <= 240:
            print("3 minutes left until arrival")
        elif arrive > 120 and arrive <= 180:
            print("2 minutes left until arrival
        elif arrive > 60 and arrive <= 120:
            print("1 minute left until arrival")
        elif arrive <= 60:
            print("You have less than one minute to arrive."")
        else :
            print ("Data does not yet exist")

    def a2():    
        if pre > 0:
            print(pre), the station is left.")
        else:
            print ("Data does not yet exist")


    return {
        'statusCode': 200,
        'body': json.dumps(a1,a2)
    }

When you run this, the error is as follows:

If you write your NAVER ID in the comments, I'll send you 5,000 points with gratitude ㅜ<

python server aws

2022-09-21 12:12

1 Answers

import json

def main():
    arrive = 420
    pre = 3

    def a1():
        if arrive > 420:
            answer1 = 6
        elif arrive > 361 and arrive <= 420:
            answer1 = 6
        elif arrive >300 and arrive <= 360:
            answer1 = 5
        elif arrive > 240 and arrive <= 300:
            answer1 = 4
        elif arrive > 180 and arrive <= 240:
            answer1 = 3
        elif arrive > 120 and arrive <= 180:
            answer1 = 2
        elif arrive > 60 and arrive <= 120:
            answer1 = 1
        elif arrive <= 60:
            answer1 = 1
        else :
            answer1 = 'not exist'
        return answer1

    def a2():
        if pre > 0:
            return pre
        else:
            return 'not exist'
    dic = {}
    dic['remain'] = a1()
    dic['pre'] = a2()

    return {
        'statusCode': 200,
        'body': json.dumps(dic)
    }

print(main())

//Result
{'statusCode': 200, 'body': '{"remain": 6, "pre": 3}'}
[Finished in 0.382s]


2022-09-21 12:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.