python 使用Google日历事件在网页上显示,使用gdata库

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 使用Google日历事件在网页上显示,使用gdata库相关的知识,希望对你有一定的参考价值。

try:
    from xml.etree import ElementTree  # for Python 2.5 users
except ImportError:
    from elementtree import ElementTree
import gdata.calendar.service
import gdata.service
import atom.service
import gdata.calendar
import atom
import getopt
import sys
import string
import time
from django.http import HttpResponse, HttpResponseRedirect
import datetime
import calendar
from django.conf import settings

calendar_service = ''


class GoogleCalendar(object):

    def auth(self):
        calendar_service = gdata.calendar.service.CalendarService()
        calendar_service.email = settings.CALENDAR_LOGIN_EMAIL
        calendar_service.password = settings.CALENDAR_LOGIN_PASSWORD
        calendar_service.source = 'Google-Calendar_Python_Sample-1.0'
        calendar_service.ProgrammaticLogin()
        return calendar_service

    def get_events_from_gcalendars(self, request, max_results,
                                   start_date=None, end_date=None):
        try:
            # start_date='2011-05-01',   end_date='2011-05-31'
            # max_results = 0, means all records
            calendar_service = self.auth()

            #feed = calendar_service.GetAllCalendarsFeed()
            s = ''
            query = gdata.calendar.service.CalendarEventQuery(
                'default',
                'private',
                'full'
            )

            if start_date:
                query.start_min = start_date

            if end_date:
                query.start_max = end_date

            query.orderby = 'starttime'
            query.sortorder = 'ascending'

            if max_results > 0:
                query.max_results = int(max_results)

            feed = calendar_service.CalendarQuery(query)
            events = []

            s += 'Events on Primary Calendar: %s <br>' % (feed.title.text,)
            for i, an_event in enumerate(feed.entry):
                title = an_event.title.text
                content = an_event.content.text

                for a_where in an_event.where:
                    where = a_where.value_string

                for a_when in an_event.when:
                    start_date = self.format_time(a_when.start_time, "date")
                    start_time = self.format_time(a_when.start_time, "time")
                    end_date = self.format_time(a_when.end_time, "date")
                    end_time = self.format_time(a_when.end_time, "time")

                event_dict = {
                    'title': title,
                    'content': content,
                    'where': where,
                    'start_date': start_date,
                    'start_time': start_time,
                    'end_date': end_date,
                    'end_time': end_time,
                }
                events.append(event_dict)

            return events
        except:
            return False

    def format_time(self, str_time, date_time="date"):
        """
        this is to convert the time format, to display nicely
        """
        if date_time == 'date':
            try:
                #2011-05-13T18:30:00.000+01:00
                f_time = time.strptime(str_time[:23], "%Y-%m-%dT%H:%M:%S.%f")
            except:
                f_time = time.strptime(str_time[:23], "%Y-%m-%d")

            ret_str = time.strftime("%a, %d %b", f_time)
            return ret_str

        elif date_time == 'time':
            try:
                #2011-05-13T18:30:00.000+01:00
                f_time = time.strptime(str_time[:23], "%Y-%m-%dT%H:%M:%S.%f")
            except:
                f_time = time.strptime(str_time[:23], "%Y-%m-%d")

            ret_str = time.strftime("%H:%M", f_time)
            return ret_str


#initiate the instance of class
events_instance = GoogleCalendar()

以上是关于python 使用Google日历事件在网页上显示,使用gdata库的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 android 使用日历 API 在谷歌日历上添加事件?

使用 Delphi 在 Google 日历中创建事件 - 错误 401

使用 Google 日历在 Fullcalendar 中选择事件

如何在网页上自动加载JQuery datepicker显示日历?需详细说明,将网页代码附上。

php 使用一个用于JavaScript地图显示的Google Maps API密钥(活动日历)和用于场地地理位置查找的不同API密钥(事件

如何在 Flutter 上使用我的 API 在表日历上显示事件