예제 #1
0
        def gather_info(self, user):
                if not '@' in user:
			logging.info('not email address: skipping ...')
                        user_detail = UserDetails()
			return user_detail
                user_detail = UserDetails.gql('where instapaper_account = :1' , user).get()
		if user_detail is None:
			user_detail = UserDetails()
		if user_detail.mail is None:
			logging.info('updating user details setting user mail %s' % user)
			user_detail.mail = user
                if user_detail.instaright_account is None:
                        logging.info('update instaright account for user %s' % user)
                        user_detail.instaright_account = user

                #PREPARE for request
                #time_milis = time.time() * 1000
                logging.info("user encoded %s" % user)
                RAPPORTIVE_LINK = "https://rapportive.com/contacts/email/%s" % user
		logging.info('fetchin url:%s' %RAPPORTIVE_LINK)
		#TILL here
                try:
                        response = urlfetch.fetch(RAPPORTIVE_LINK)
                except:
                        logging.info('error downloading rapportive info. reTRYing')
			try:
                        	response = urlfetch.fetch(RAPPORTIVE_LINK)
			except:
                        	e, e1 = sys.exc_info()[0], sys.exc_info()[1]
                        	return user_detail
                if response.status_code == 200:

                        json_string_response = simplejson.loads(simplejson.dumps(response.content))
                        json = simplejson.loads(json_string_response)
			name = None
			try:
                        	name = json["contact"]["name"]
			except:
				logging.info("unable to gather name from rapportive")

                        if name is None or len(name) < 1 or unicode(name).startswith('Thanks') or unicode(name).startswith('Sorry'):
				logging.info('did not find name for account %s' % user)
                                return user_detail
                        
                        logging.info('name: %s' % unicode(name))
                        user_detail.name = unicode(name)
                        memberships = json["contact"]["memberships"] 
                        memships = {}
                        for m in memberships:
                                service_name = m['site_name'].lower().replace(' ','_').replace('.','_')
                                memships[service_name]=m['profile_url']
				try:
                                	setattr(user_detail, service_name, m['profile_url'])
					logging.info('service %s profile %s' % (service_name, m['profile_url']))
                                        if service_name == 'twitter' and not user_detail.twitter_request_sent:
                                                # send twitter request and need to put since we need key for follow
						user_detail.twitter_request_sent=True
						user_detail.put()
                                                taskqueue.add(url='/util/twitter/follow/'+str(user_detail.key()), queue_name='twit-queue')
				except:
					logging.error('memberships error %s ::: more details %s ' % (sys.exc_info()[0], sys.exc_info()[1]))

                        user_detail.social_data = simplejson.dumps(memships)
                        occupations = json["contact"]["occupations"]
                        occups = []
                        for o in occupations:
                                tmp_o = o['job_title'] + " ^_^ " + o['company']
                                occups.append(tmp_o)
                        user_detail.occupations = '; '.join(occups)
			logging.info('occupations %s' %user_detail.occupations)
			avatar = None
			try:
                        	avatar = json["contact"]["images"][0]["url"]
                        	logging.info('avatar %s' % avatar)
			except:
				logging.info('no image info cause of %s' % sys.exc_info()[0])
                        if avatar is not None and len(avatar) > 0:
                                user_detail.avatar = avatar
			location = None
			try:
				location = json["contact"]["location"]
			except:
				logging.info('no location info cause of %' %sys.exc_info()[0])
			if location is not None and len(location):
				location_lines = location.splitlines()
				user_detail.location = ' '.join(location_lines)
                return user_detail