Пример #1
0
def get_user_info_works(username):

    start = time.time()
    # if in cache already, return that
    if username in user_info_cache: return user_info_cache[username]

    try:
	data = json.load(urllib2.urlopen('http://tagnet.media.mit.edu/users?user_name=%s&capability=profile' % urllib.quote(username)))['profile']
	info = dict(
         	username=username,
                name=data['name'],
                picture_url=get_cached_img(150, data['picture_url']),
		url = data['url'],
                affiliation=', '.join(data['affiliation']),
		usertype=data.get('user_type'),
		last_loc=get_last_location(username),
		company=data.get('company_name'),
		company_url = data.get('company_url'),
		known=True)
    except Exception:
	info = {
                'username': username,
                'name': username,
                'picture_url': "http://pldb.media.mit.edu/face/nobody",
                'known': False}
     
    info['last_time'] = get_time_ago(info.get('last_loc').get('tstamp')) if 'last_loc' in info else None


    #get cached version of picture
    user_info_cache[username] = info

#    print "get_user_info took", time.time()-start
    return info
Пример #2
0
def get_user_info(username):
    start = time.time()
    if username in user_info_cache: return user_info_cache[username]
    data = None
    info = dict()
    try: 
        # if sponsor aka @ exist, query spm
	if '@' in username:
	    #print "sponsor!"
	    url = "http://data.media.mit.edu/spm/contacts/json?username=%s" % username
            data = json.loads(urllib2.urlopen(url).read().decode('latin1'))
#	    print "data", data
	    info = data['profile']
#	    print info
	    info[u'name'] = info['first_name']+ " " + info['last_name']
	    info[u'affiliation'] = info['mlid']
        # if no sponsor query people
	else:   	 
    	    url = "http://data.media.mit.edu/people/json/?filter=(cn=%s)" % urllib.quote(username)
	    data = json.loads(urllib2.urlopen(url).read().decode('latin1'))
#	    print "len of data", len(data)
#	    if len(data) == 0:
#		print data
	    info = data[0]

#	print info
#        print
#	print info['name']
	info['picture_url'] = get_cached_img(150, info['picture_url'])
#	print "after picture"
	info['last_loc'] = get_last_location(username)
#	print "after last loc", info['last_loc']
	if info['last_loc'] == "N/A":
	    info['last_time'] = "N/A"
	else:
	    info['last_time'] = get_time_ago(info.get('last_loc').get('tstamp')) if info['last_loc'] else None
	
#	print "after last time", info['last_time']
        #if isinstance(info['affiliation'], list):
        #    info['affiliation'] = ', '.join(info['affiliation'])


        info['known'] = True

	user_info_cache[username] = info	
	return info
  
    except Exception as e:
	print "user info error!", type(e), e, username
	info = {
	    'username':username,
	    'name':username,
	    'picture_url': "http://pldb.media.mit.edu/face/%s" % username, 
	    'affiliation': None, 
	    'known': False   }
    user_info_cache[username] = info
    return info