崩溃、Python和Google日历API

崩溃、Python和Google日历API,python,calendar,raspberry-pi2,Python,Calendar,Raspberry Pi2,啊 我按照googlecalendarapi的文档()和本教程()创建了一个闹钟。原理很简单。我把一个事件放在我的日历上,如果它在“树莓”日历上,并且当时间=指定的时间时描述为“唤醒”,它会在Pi上播放我的音乐 除了当我运行我的脚本时,它会在一段可变的时间内工作,然后会因随机错误而崩溃 Traceback (most recent call last): File "alarmclock.py", line 65, in <module> calendar_eve

我按照googlecalendarapi的文档()和本教程()创建了一个闹钟。原理很简单。我把一个事件放在我的日历上,如果它在“树莓”日历上,并且当时间=指定的时间时描述为“唤醒”,它会在Pi上播放我的音乐

除了当我运行我的脚本时,它会在一段可变的时间内工作,然后会因随机错误而崩溃

    Traceback (most recent call last):
  File "alarmclock.py", line 65, in <module>
    calendar_event_query()
  File "alarmclock.py", line 37, in calendar_event_query
    events      = service.events().list(singleEvents=True, calendarId=CALENDAR_ID).execute()
  File "/usr/local/lib/python2.7/dist-packages/oauth2client/util.py", line 140, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/googleapiclient/http.py", line 722, in execute
    body=self.body, headers=self.headers)
  File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 596, in new_request
    redirections, connection_type)
  File "/usr/local/lib/python2.7/dist-packages/httplib2/__init__.py", line 1609, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/usr/local/lib/python2.7/dist-packages/httplib2/__init__.py", line 1351, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/usr/local/lib/python2.7/dist-packages/httplib2/__init__.py", line 1272, in _conn_request
    conn.connect()
  File "/usr/local/lib/python2.7/dist-packages/httplib2/__init__.py", line 1036, in connect
    self.disable_ssl_certificate_validation, self.ca_certs)
  File "/usr/local/lib/python2.7/dist-packages/httplib2/__init__.py", line 80, in _ssl_wrap_socket
    cert_reqs=cert_reqs, ca_certs=ca_certs)
  File "/usr/lib/python2.7/ssl.py", line 891, in wrap_socket
    ciphers=ciphers)
  File "/usr/lib/python2.7/ssl.py", line 566, in __init__
    self.do_handshake()
  File "/usr/lib/python2.7/ssl.py", line 788, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:581)

很难具体诊断,但如果我冒昧猜测的话——这看起来像是网络连接不良,而不是脚本的任何问题。我真的不认为这是一个网络问题,因为我使用了其他一些连接到互联网的脚本,它们不会像这样“崩溃”……或者可能是谷歌服务器的连接问题?我在3个小时前重新启动了我的脚本,现在仍然没有任何问题难以明确诊断,但如果我冒险猜测的话——看起来网络连接不好,而脚本有任何问题。我真的不认为这是网络问题,因为我使用了其他一些连接到internet的脚本,它们不会“崩溃”就像这个……或者是谷歌服务器的连接问题?我在3小时前重新启动了脚本,但仍然没有任何问题(目前)
    Traceback (most recent call last):
  File "alarmclock.py", line 80, in <module>
    calendar_event_query()
  File "alarmclock.py", line 52, in calendar_event_query
    events = service.events().list(singleEvents=True, calendarId=CALENDAR_ID).execute()
  File "/usr/local/lib/python2.7/dist-packages/oauth2client/util.py", line 140, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/googleapiclient/http.py", line 722, in execute
    body=self.body, headers=self.headers)
  File "/usr/local/lib/python2.7/dist-packages/oauth2client/client.py", line 596, in new_request
    redirections, connection_type)
  File "/usr/local/lib/python2.7/dist-packages/httplib2/__init__.py", line 1609, in request
    (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
  File "/usr/local/lib/python2.7/dist-packages/httplib2/__init__.py", line 1351, in _request
    (response, content) = self._conn_request(conn, request_uri, method, body, headers)
  File "/usr/local/lib/python2.7/dist-packages/httplib2/__init__.py", line 1278, in _conn_request
    raise ServerNotFoundError("Unable to find the server at %s" % conn.host)
httplib2.ServerNotFoundError: Unable to find the server at www.googleapis.com
    #!/usr/bin/env python
# -*- coding: UTF-8 -*-

from __future__ import print_function
import httplib2

from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools

from datetime import datetime
import logging
import time
import random
import os
import argparse

try:
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

CALENDAR_ID = 'MY_CALENDAR_ID@group.calendar.google.com'
CLIENT_SECRET_FILE = 'raspi-wake.json'
SCOPE = 'https://www.googleapis.com/auth/calendar.readonly'
MP3_FOLDER = 'mp3'

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


def get_credentials():
    home_dir = os.path.expanduser('~')
    credential_dir = os.path.join(home_dir, '.credentials')
    if not os.path.exists(credential_dir):
        os.makedirs(credential_dir)
    credential_path = os.path.join(credential_dir, 'raspberry-wake.json')

    store = oauth2client.file.Storage(credential_path)
    credentials = store.get()
    if not credentials or credentials.invalid:
        flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, scope=SCOPE)
        flow.user_agent = 'Raspberry Wake'
        if flags:
            credentials = tools.run_flow(flow, store, flags)
        else:
            credentials = tools.run(flow, store)
        print('Storing credentials to ' + credential_path)

    return credentials


def calendar_event_query():
    credentials = get_credentials()
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('calendar', 'v3', http=http)

    today = datetime.today()
    events = service.events().list(singleEvents=True, calendarId=CALENDAR_ID).execute()

    for i, event in enumerate(events['items']):
        try:
            start = event['start']['dateTime'][:-9]
        except KeyError:
            start = ''

        description = event.get('description', '')
        now = today.strftime('%Y-%m-%dT%H:%M')

        if start >= now and description.lower() == 'wake':
            if start == now:
                mp3_files = random.choice(os.listdir(MP3_FOLDER))
                command = 'mpg123 \'{}/{}\''.format(MP3_FOLDER, mp3_files)
                os.system(command)
                time.sleep(60)

while True:
    calendar_event_query()
    time.sleep(5)