繁体   English   中英

如何格式化日期时间数据以适应谷歌日历 api 的日期字段?

[英]How do I format datetime data to fit google calendar api's date field?

目标:创建一个 Google 日历活动。

拦截器:日期格式。


背景:我正在尝试创建一个膳食计划器,它从给定的数据库中获取食谱并在谷歌日历中创建带有他们名字的事件。 数据库如下所示:

d = {'recipe_id': ['carrot salad', 'leek fritters'], 'meal_date': ['2020-05-28 22:28:01.204464+00:00', '2020-05-29 22:28:01.204464+00:00']}    
df = pd.DataFrame(data=d)

用餐日期是两个的乘积

today_date = datetime.datetime.utcnow().isoformat() + 'Z'
df['menu_date'] = today_date
df['menu_date'] = pd.to_datetime(df['menu_date'])
df['meal_date'] = df['menu_date'] + df['meal'].apply(pd.offsets.Day)

其中 'meal' 只是一个数字(1、2 等),最后一个命令只是将今天的日期移动该数量。

当我使用以下代码上传到谷歌日历时,出现错误:

def myconverter(o):
    '''
    call the __str__ method of the datetime object that will return a string representation of the value
    ___

    shoutout: https://code-maven.com/serialize-datetime-object-as-json-in-python
    '''
    if isinstance(o, datetime.datetime):
        return o.__str__()

def add_to_calendar(df, calendar_id):
    """Shows basic usage of the Google Calendar API.
    Prints the start and name of the next 10 events on the user's calendar.
    """
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('calendar', 'v3', credentials=creds)

    # Call the Calendar API
    now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
    print('Adding meals to calendar')

    for i, r in df.iterrows():
        event = {
        'summary': r.name,
        'description': r.meal_period,
        'start': {
            'date': json.dumps(r.meal_date, default=myconverter),
            'timeZone': 'America/Los_Angeles'
        },
        'end': {
            'date': json.dumps(r.meal_date, default=myconverter),
            'timeZone': 'America/Los_Angeles'
        }
        }
        event = service.events().insert(calendarId=calendar_id, body=event).execute()

运行此代码,我收到以下错误:

HttpError: <HttpError 400 when requesting https://www.googleapis.com/calendar/v3/calendars/CALENDAR_ID/events?alt=json returned "Invalid value for: Invalid format: ""2020-05-28 22:28:01.204464+00:00""">

其中 CALENDAR_ID 是我的谷歌日历 ID。

如果有人知道如何使用 python 代码解决此日期问题,这将非常有帮助。

这个答案怎么样?

修改点:

  • 如果您想使用start.dateend.date作为全天事件,格式要求为yyyy-mm-dd 在这种情况下,不需要 timeZone。
  • 如果要使用start.dateTimeend.dateTime作为全天事件,格式要求为 RFC3339。 在这种情况下, timeZone 是必需的。

从以上情况来看,当你的脚本被修改时,下面的模式怎么样?

模式一:

在此模式中,使用start.dateend.date 为此,请进行如下修改。

从:

'start': {
    'date': json.dumps(r.meal_date, default=myconverter),
    'timeZone': 'America/Los_Angeles'
},
'end': {
    'date': json.dumps(r.meal_date, default=myconverter),
    'timeZone': 'America/Los_Angeles'
}

至:

'start': {
    'date': 'date': parse(r.meal_date).strftime("%Y-%m-%d"),
},
'end': {
    'date': 'date': parse(r.meal_date).strftime("%Y-%m-%d"),
}

模式二:

在此模式中,使用start.dateTimeend.dateTime 为此,请进行如下修改。

从:

'start': {
    'date': json.dumps(r.meal_date, default=myconverter),
    'timeZone': 'America/Los_Angeles'
},
'end': {
    'date': json.dumps(r.meal_date, default=myconverter),
    'timeZone': 'America/Los_Angeles'
}

至:

'start': {
    'dateTime': parse(r.meal_date).isoformat(),
    'timeZone': 'America/Los_Angeles'
},
'end': {
    'dateTime': parse(r.meal_date).isoformat(),
    'timeZone': 'America/Los_Angeles'
}

笔记:

  • 在此修改中,使用from dateutil.parser import parse
  • 从您的脚本中,我看不到您正在使用的 scope。 所以如果出现与 scope 相关的错误,请使用https://www.googleapis.com/auth/calendar作为 scope。 届时请删除token.pickle文件,重新授权scope。 请注意这一点。

参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM