def get_service_profile(author_service_context, request):
  author = author_service_context.author

  service = data_access.service.name_to_service.get(author_service_context.service_name)
  if service is None:
    # TODO: better error
    request.response.status_int = 404
    return {'error': 'unknown service %s' % author_service_context.service_name}

  fetcher = profile_fetcher.from_service_name(
      service.service_name,
      miapi.oauth_config[service.service_name])
  if not fetcher:
    request.response.status_int = 404
    return {'error': 'profile information is not available for service %s' % service.service_name}

  try:
    mapping = tim_commons.db.Session().query(AuthorServiceMap). \
              filter_by(service_id=service.id, author_id=author.id).one()
  except:
    request.response.status_int = 404
    return {'error': 'unknown service for author'}

  profile_json = fetcher.get_author_profile(mapping.service_author_id, mapping)

  return profile_json
def get_profile(author_context, request):
  author = author_context.author

  # service profile precedence is: linkedin, facebook, googleplus, twitter, instagram, foursquare
  # get all service mappings for author
  mappings = {}
  for asm in tim_commons.db.Session().query(AuthorServiceMap).filter_by(author_id=author.id).all():
    mappings[asm.service_id] = asm

  profile_json = None

  for service_name in ['linkedin', 'facebook', 'googleplus', 'twitter', 'instagram', 'foursquare']:
    asm = mappings.get(service.name_to_service[service_name].id)
    if asm:
      fetcher = profile_fetcher.from_service_name(service_name, miapi.oauth_config[service_name])
      profile_json = fetcher.get_author_profile(asm.service_author_id, asm)
      break

  return profile_json
  def app_main(self, config, options, args):

    logging.info("Beginning...")

    # read the db url from the config
    db_url = db.create_url_from_config(config['db'])

    # initialize the db engine & session
    db.configure_session(db_url)

    # if services option is empty default to all available configured services
    if not options.services:
      service_names = []
      # generate the list of service name's from queue list
      for key in config['queues'].iterkeys():
        service_names.append(key)
    else:
      service_names = options.services

    # for each specified service
    for service_name in service_names:

      fetcher = profile_fetcher.from_service_name(service_name, config['oauth'][service_name])

      # get each author subscribed to this service
      for asm in db.Session().query(AuthorServiceMap). \
                              join(Service, AuthorServiceMap.service_id == Service.id). \
                              filter(Service.service_name == service_name).all():

        profile_json = fetcher.get_author_profile(asm.service_author_id, asm)

        if 'picture_url' in profile_json:
          asm.profile_image_url = profile_json['picture_url']
          db.Session().flush()

    logging.info("Finished...")