[Q] Extracting FB contacts from Android's contacts database.

shadowofdarkness

Senior Member
Jun 11, 2010
554
139
0
I found this script to extract Facebooks contact info from a Android phone but even though it mostly works and I get all emails and phone numbers the names get left out and I was wondering if someone knew how to fix this script.

I assume it is just a database change since the script was made.

This is all to make it easier to find friends on Google+ since Facebook tries to stop anyone from getting this info.

Code:
#! /bin/sh

###########################################
#
# Android DB contacts exporter (proof-of-concept)
# 2011-01-06
#
# Extracts "certain contacts" from the Android contacts database to a CSV file
# for import into other contact list tools.
#
#
# == Usage ==
#
# 1) Copy your Android contacts database to your computer
#     adb pull /data/data/com.android.providers.contacts/databases/contacts2.db ./
# 2) Run this file from the same directory as contacts2.db
#     ./fb-extract.sh
# 3) Import csv to an address book of your choice
#
# == Notice ==
#
# This is a proof of concept script. Use at your own risk and check
# with laws and terms of service regarding data usage.
#
###########################################

# In and out files
CONTACTSDB="./contacts2.db"
OUTFILE="./contacts_fb.csv"

# Write table header
echo "First Name,Last Name,E-mail Address,Mobile Phone,Home Phone" >> $OUTFILE

# Get the raw IDs of all facebook contacts
RAWID_LIST=`sqlite3 $CONTACTSDB "SELECT _id from raw_contacts WHERE account_type='com.facebook.auth.login'"`

# Loop through the IDs and extract necessary data
for RAWID in $RAWID_LIST; do
    # Get the first and last names
    NAME=`sqlite3 $CONTACTSDB "SELECT data2, data3 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=7"`
    FNAME=`echo $NAME | awk '{split($0,a,"|"); print a[1]}'`
    LNAME=`echo $NAME | awk '{split($0,a,"|"); print a[2]}'`

    # Get the email address
    EMAIL=`sqlite3 $CONTACTSDB "SELECT data1 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=1"`

    # Get the phone numbers (mobile & other). Convert international Japanese # to local using `sed`
    MPHONE=`sqlite3 $CONTACTSDB "SELECT data1 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=5 AND data2=2" | sed 's/^81\([0-9]\{7,\}\)/0\1/'`
    OPHONE=`sqlite3 $CONTACTSDB "SELECT data1 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=5 AND data2=7" | sed 's/^81\([0-9]\{7,\}\)/0\1/'`

    # If at least one info field was set then write the csv line
    if [ -n "$EMAIL" ] || [ -n "$MPHONE" ] || [ -n "$OPHONE" ]; then
      echo "$FNAME,$LNAME,$EMAIL,$MPHONE,$OPHONE" >> $OUTFILE
    fi
done

Original source of script
http://webcache.googleusercontent.c...c+contacts+sqlite&hl=en&client=ubuntu&strip=1