使用谷歌日历 API 时,“TypeError: combine() argument 1 must be datetime.date, not int”

时间:2021-03-07 07:57:45

标签: python python-3.x google-calendar-api typeerror

我对编码和尝试创建语音助手非常陌生。一切正常,但我不知道为什么会出现此错误

Python 版本:3.9 64 位
Windows 版本:10 专业版 64 位
我住在印度,我们遵循的时区是亚洲,加尔各答(只是告诉它是否是造成它的原因)

所以我做了两个功能:
第一个函数 get_date 将接受一个语音到文本转换的字符串,然后尝试找到我们正在谈论的日期。

def get_date(text):
    '''
    This fuction will return date value in the form of MM/DD/YYY
    it will convert any phrases passes to a date value if it contains the required data
    :param text:
    :return:
    '''
    text = text.lower()
    today = datetime.date.today()

    # if the query contains 'today' simply return today's date
    if text.count('today') > 0:
        return today
    # if the query contains 'tomorrow' simply return tomorrow's date
    if text.count('tomorrow') > 0:
        return today + datetime.timedelta(1)

    month = -1
    day_of_week = -1
    day = -1
    year = today.year

    # looping over the given phrase
    for word in text.split():
        # if the phrase contains name of month find its value from the list
        if word in MONTHS:
            month = MONTHS.index(word) + 1
        # if the phrase contains day of teh week return day of the week from the list
        if word in DAYS:
            day_of_week = DAYS.index(word)
        # if the phrase contains the digit itself then convert it from str to int
        if word.isdigit():
            day = int(word)
        # again run a loop to get values of DAY_EXTENSIONS and then check if we have them in the word
        for ext in DAY_EXTENSIONS:
            x = word.find(ext)
            if x > 0:
                try:
                    day = int(word[:x])
                except:
                    pass

    # if given month is passed add to the year
    if month < today.month and month != -1:
        year = year+1

    # if only day is given then finding if month is this or the next
    if month == -1 and day != -1:
        if day < today.day:
            month = today.month + 1
        else:
            month = today.month

    # if only day of the week is provided then find the date
    if day_of_week != -1 and month == -1:
        current_day_of_week = today.weekday()
        diff = day_of_week - current_day_of_week

        if diff < 0:
            diff += 7
            if text.count('next') >= 1:
                diff += 7

        return today + datetime.timedelta(diff)
    if day != -1:
        return datetime.date(month=month, day=day, year=year)

然后我有第二个函数,它使用谷歌日历 API 来获取日历中的事件 我对此知之甚少,但我正在关注 youtube 上的一个人。 这是函数

def get_events(day, service):
    # the below four lines convert the date we provide in terms of utctime format
    # If you know what they really mean tell me, please ?
    date = datetime.datetime.combine(day, datetime.datetime.min.time())
    end = datetime.datetime.combine(day, datetime.datetime.max.time())
    utc = pytz.UTC
    date = date.astimezone(utc)
    end = end.astimezone(utc)

    events_result = service.events().list(calendarId='primary', timeMin=date.isoformat(),
                                          timeMax=end.isoformat(), singleEvents=True,
                                          orderBy='startTime').execute()
    events = events_result.get('items', [])

    if not events:
        print('No upcoming events found.')
    for event in events:
        start = event['start'].get('dateTime', event['start'].get('date'))
        print(start, event['summary'])

这里是我如何使用这些功能:

query = take_command().lower()
for phrases in GET_DATE_STRINGS:
    if phrases in query.lower():
        day = datetime.date(get_date(query))
        get_events(day, service)

然后这里是错误:

<googleapiclient.discovery.Resource object at 0x00000189C9F60AC0>

Traceback (most recent call last):
  File "D:\Coding\jarvis\main.py", line 248, in <module>
    get_events(3, service)
  File "D:\Coding\jarvis\main.py", line 157, in get_events
    date = datetime.datetime.combine(day, datetime.datetime.min.time())
TypeError: combine() argument 1 must be datetime.date, not int

现在据我所知,错误是我提供给 get_events() 函数的日期输入是 int 但正如我在 get_date( 中看到的返回函数)返回一个 {{1 }} 对象不是整数。 如果您知道修复方法,请帮忙。

1 个答案:

答案 0 :(得分:0)

如果 get_date 返回一个 date 对象,你为什么要再次在这里创建一个 date 对象:
day = datetime.date(get_date(query)) ?

你可以这样做:

day = get_date(query)
get_events(day, service)