def extract_fb_data(data):
    """Extact and normalize facebook data as parsed from the graph JSON"""
    # Setup the normalized contact info
    nick = None

    # Setup the nick and preferred username to the last portion of the
    # FB link URL if its not their ID
    link = data.get('link')
    if link:
        last = link.split('/')[-1]
        if last != data['id']:
            nick = last

    profile = {
        'accounts': [{'domain': 'facebook.com', 'userid': data['id']}],
        'displayName': data['name'],
        'preferredUsername': nick or data['name'],
    }
    gender = data.get('gender')
    if gender:
        profile['gender'] = gender
    email = data.get('email')
    if email:
        profile['emails'] = [{'value': email, 'primary': True}]
        if data.get('verified') and email:
            profile['verifiedEmail'] = email

    tz = data.get('timezone')
    if tz:
        # -5.5 -> -05:30
        offset = float(tz)
        h = int(offset)
        m = int(abs(offset - h) * 60)
        profile['utcOffset'] = '{h:+03d}:{m:02d}'.format(h=h, m=m)
    bday = data.get('birthday')
    if bday:
        try:
            mth, day, yr = bday.split('/')
            date = datetime.date(int(yr), int(mth), int(day))
            profile['birthday'] = date.strftime('%Y-%m-%d')
        except ValueError:
            pass
    name = {}
    pcard_map = {'first_name': 'givenName', 'last_name': 'familyName'}
    for key, val in pcard_map.items():
        part = data.get(key)
        if part:
            name[val] = part
    name['formatted'] = data['name']

    profile['name'] = name

    # Now strip out empty values
    for k, v in profile.items():
        if not v or (isinstance(v, list) and not v[0]):
            del profile[k]

    return profile
dataset/ETHPy150Open bbangert/velruse/velruse/providers/facebook.py/extract_fb_data/original