def __init__(self, uri, domain):
        self.uri = uri
        self.domain = domain
        headers = {'X-Redirect-Calendar-Shard': 'true', 'GData-Version': '2'}
        self.client = calendar_service.CalendarService(
            additional_headers=headers)
        gdata.alt.appengine.run_on_appengine(self.client)
        self.valid = True
        try:
            self.client.AuthSubTokenInfo()
        except:
            self.valid = False

        if not self.valid:
            session_token = None
            # Find the AuthSub token and upgrade it to a session token.
            try:
                auth_token = gdata.auth.extract_auth_sub_token_from_url(uri)
                if auth_token:
                    # Upgrade the single-use AuthSub token to a multi-use session token.
                    session_token = self.client.upgrade_to_session_token(
                        auth_token)
                if session_token and users.get_current_user():
                    # If there is a current user, store the token in the datastore and
                    # associate it with the current user. Since we told the client to
                    # run_on_appengine, the add_token call will automatically store the
                    # session token if there is a current_user.
                    self.client.token_store.add_token(session_token)
                    self.valid = True
                else:
                    self.valid = False
            except:
                self.valid = False
Example #2
0
def StoreCalendarSessionToken(relative_url):
  """Extract calendar auth tokens from url and store them in the datastore.

  Args:
    relative_url: The url that was called by the calendar token system with
        token information in it.
  """
  # Get auth token from url.
  auth_token = auth.extract_auth_sub_token_from_url(relative_url)
  assert auth_token

  # Upgrade to session token.
  calendar_client = calendar_service.CalendarService()
  gdata_appengine.run_on_appengine(calendar_client, store_tokens=False,
                                   single_user_mode=True, deadline=10)
  session_token = calendar_client.upgrade_to_session_token(auth_token)
  assert session_token

  # Write session token to datastore.
  session_token_str = session_token.get_token_string()
  config = models.Configuration(key_name=_CALENDAR_TOKEN_KEY_NAME,
                                config_value=session_token_str,
                                config_key=_CALENDAR_TOKEN_KEY_NAME)
  config.put()

  # Refresh memcache session token and make sure a usable calendar client can be
  # created.
  calendar_client = _GetCalendarService(refresh_memcache=True)
  assert calendar_client is not None
Example #3
0
def _GetCalendarService(refresh_memcache=False):
  """Create gdata calendar service with a valid session id.

  Creates a gdata calendar service. Uses session key from memcache when
  available. On a memcache miss, loads the session key from datastore. Tries to
  return a usable calendar service and None if it fails to create one.

  Args:
    refresh_memcache: Force a memcache session key refresh from datastore.

  Returns:
    A gdata.calendar.service.CalendarService
  """
  # Initialize calendar service for AppEngine.
  headers = {'X-Redirect-Calendar-Shard': 'true'}
  calendar_client = calendar_service.CalendarService(additional_headers=headers)
  gdata_appengine.run_on_appengine(calendar_client, store_tokens=False,
                                   single_user_mode=True, deadline=10)

  session_token = None
  memcache_key = 'memcache_'+ _CALENDAR_TOKEN_KEY_NAME
  if not refresh_memcache:
    session_token = memcache.get(memcache_key)

  if session_token is None:  # Memcache miss, load from datastore.
    config_entity = models.Configuration.get_by_key_name(
        _CALENDAR_TOKEN_KEY_NAME
    )
    if not config_entity or not config_entity.config_value: return None
    session_token = config_entity.config_value
    memcache.set(memcache_key, session_token)  # Place in memcache.

  token = auth.AuthSubToken()
  token.set_token_string(session_token)
  calendar_client.current_token = token

  return calendar_client
Example #4
0
#************************************************************************************#
#****           Global variables that can be changed in wakeup.cfg file          ****#
#************************************************************************************#
email = parser.get('credentials', 'email')
password = parser.get('credentials', 'password')
q = parser.get('alarm', 'query')
mp3_path = parser.get('alarm', 'mp3_path')

date = (datetime.now() + timedelta(days=-1)).strftime("%Y-%m-%dT%H:%M:%S.000Z")
endDate = (datetime.now() +
           timedelta(days=14)).strftime("%Y-%m-%dT%H:%M:%S.000Z")

#************************************************************************************#
#****           Login credentials for your Google Account                        ****#
#************************************************************************************#
calendar_service = GServ.CalendarService()
calendar_service.email = email
calendar_service.password = password
calendar_service.source = 'SimpleGoogleAlarmClock'
calendar_service.ProgrammaticLogin()


#************************************************************************************#
#****           Main query                                                       ****#
#************************************************************************************#
def FullTextQuery(calendar_service):
    print 'Full text query for events on Primary Calendar: \'%s\'' % (q)
    query = GServ.CalendarEventQuery('default', 'private', 'full', q)
    query.start_min = date  #  calling date to set the beginning of query range for the present day
    query.start_max = endDate  #  calling endDate to limit the query range to the next 14 days. change tmedelta(days) to set the range
    query.singleevents = 'true'  #  enables creation of repeating events