#!/bin/bash
#
# BASH login&example by http://360percents.com
# v1.0
#
email="[email protected]"
pass="derp"
host_url="http://m.facebook.com"
album_ind=1
album_inc=1
album_page=1
photo_ind=0
photo_inc=12
ref_id=""
output_path="pages"
photo_path="photos"
cookie_path="/tmp/fb_cookie"
touch $cookie_path
isLoggedIn=false
host_html=$(curl -s -c ${cookie_path} -A "Mozilla/5.0" "${host_url}")
form_action=$(echo "${host_html}" | tr '"' "\n" | grep "https://www.facebook.com/login.php")
form_fields=$(echo "${host_html}" | sed -e 's/.*<form//' | sed -e 's/form>.*//' | tr '/>' "\n" | grep 'input ' | grep -v "email\|pass")
#FUNCTION PARSES FORM DATA LIKE HIDDEN INPUTS
function parse_form() {
form_fields="${1}"
params=""
for (( i=1; i<=`echo "${form_fields}" | wc -l`; i++ )); do
name=$(echo "${form_fields}" | sed -n "${i}"p | tr ' ' "\n" | grep 'name' | cut -d '"' -f 2)
value=$(echo "${form_fields}" | sed -n "${i}"p | tr ' ' "\n" | grep 'value' | cut -d '"' -f 2)
params="${params}${name}=${value}&"
done
}
function parse_profile() {
printf "Parsing Profile...\n"
profile_html=$(curl -s -b ${cookie_path} -c ${cookie_path} -A 'Mozilla/5.0' --location "http://m.facebook.com/profile.php?id=${1}")
echo -n "${profile_html}" > "${output_path}/m.profile_[${fb_id}].htm"
}
function parse_info() {
printf "Parsing Info [%s]... " $1
info_html=$(curl -s -b ${cookie_path} -c ${cookie_path} -A 'Mozilla/5.0' --location "http://m.facebook.com/profile.php?id=${1}&v=info")
echo -n "${info_html}" > "${output_path}/m.info_[${fb_id}].htm"
ref_id=$(echo "${info_html}" | tr '"' "\n" | grep "refid=" | sed 's/.*=\(.*\).*$/\1/' | head -n 1)
handle_name=$(echo "${info_html}" | tr '"' "\n" | grep ">facebook\.com" | sed 's/.*com\/\(.*\)\<\/a.*/\1/')
printf "(%s)... " "${handle_name}"
friends_tot=$(echo "${info_html}" | tr '"' "\n" | grep "Total Friends" | sed 's/.* (\(.*\)).*$/\1/' | sed 's/,//')
album_tot=0
sex_usr=$(echo "${info_html}" | tr '"' "\n" | grep -A 4 "Sex" | tail -n 1 | sed 's/.\(.*\)\<\/div.*$/\1/')
college_info=$(echo "${info_html}" | tr '"' "\n" | grep -A 4 "College" | tail -n 1 | sed 's/\>\<div\>\(.*\)\<br \/\>.*$/\1/')
hs_info=$(echo "${info_html}" | tr '"' "\n" | grep -A 4 "High School" | tail -n 1 | sed 's/\>\<div\>\(.*\)\<\/div\>\<\/div\>.*$/\1/')
friends_url=$(echo "${info_html}" | tr '"' "\n" | grep "friends.php?id" | sed 's/\&\;/\&/g' | head -n 1 | sed 's/.\(.*\)$/\1/')
if [ "${friends_url}" ]; then printf "Friends (%d)... " "${friends_tot}"; fi
wall_url=$(echo "${info_html}" | tr '"' "\n" | grep "feed" | sed 's/\&\;/\&/g' | sed 's/.\(.*\)$/\1/')
if [ "${wall_url}" ]; then printf "Wall... "; fi
photos_url=$(echo "${info_html}" | tr '"' "\n" | grep "v\=photos" | head -n 1 | sed 's/\&\;/\&/g' | sed 's/.\(.*\)$/\1/')
if [ "${photos_url}" ]; then printf "Photos... "; fi
echo
}
function parse_friends() {
printf "Parsing Friends [%s]...\n" "${fb_id}"
friends_html=$(curl -s -b ${cookie_path} -c ${cookie_path} -A 'Mozilla/5.0' --location "http://m.facebook.com/${friends_url}")
echo -n "${friends_html}" > "${output_path}/m.friends_[${fb_id}].htm"
}
function parse_photos() {
printf "Parsing Photos [%s]...\n" "${fb_id}"
local cnt="10"
photos_html=$(curl -s -b ${cookie_path} -c ${cookie_path} -A 'Mozilla/5.0' --location "http://m.facebook.com/${photos_url}")
echo -n "${photos_html}" > "${output_path}/m.photos-1_[${fb_id}].htm"
mkdir -p "${photo_path}/${fb_id}"
parse_albums "${photos_html}"
#allPhotos_url=$(echo "${photos_html}" | tr '"' "\n" | grep "set\=t" | sed 's/\&\;/\&/g' | sed 's/.\(.*\)$/\1/')
#if [ "${allPhotos_url}" ]; then printf "All Photos... ";fi
next_page=$(echo "${photos_html}" | tr '"' "\n" | grep "p\=${album_inc}" | sed 's/\&\;/\&/g' | sed 's/.\(.*\)$/\1/')
if [ "${next_page}" ]; then
album_inc=10
next_albums
fi
echo
}
function next_albums() {
photos_html=$(curl -s -b ${cookie_path} -c ${cookie_path} -A 'Mozilla/5.0' --location "http://m.facebook.com/photos.php?id=${fb_id}&p=${album_inc}&refid=${ref_id}")
echo -n "${photos_html}" > "${output_path}/m.photos-$(( ($album_inc / 10) + 1 ))_[${fb_id}].htm"
parse_albums "${photos_html}"
album_inc=$(( $album_inc + 10 ))
next_page=$(echo "${photos_html}" | tr '"' "\n" | grep "p\=${album_inc}" | sed 's/\&\;/\&/g' | sed 's/.\(.*\)$/\1/')
if [ "${next_page}" ]; then
next_albums; fi
}
function parse_albums() {
printf "Albums Page: %d [%s]\n" $(( ($album_inc / 10) + 1 )) "http://m.facebook.com/photos.php?id=${fb_id}&p=${album_inc}&refid=${ref_id}"
album_urls=$(echo "${1}" | tr '"' "\n" | grep "set\=a" | sed 's/\&\;/\&/g')
(IFS=$'\n'
for (( i=1; i<=`echo "${album_urls}" | wc -l`; i++ )); do
album_url=$(echo "${album_urls}" | sed -n "${i}"p | sed 's/\&\;/\&/g' | sed 's/.\(.*\)$/\1/')
album_id=$(echo "${album_url}" | sed 's/.*a\.\(.*\)\&.*$/\1/' | sed 's/\&op\='$album_inc'\&v//')
album_name=$(echo "${1}" | tr '"' "\n" | grep -A 1 "${album_id}" | tail -n 1 | sed 's/^\>\<span\>\(.*\)\<\/span\>.*$/\1/' | sed 's/\&\;/\&/g' | sed 's/^\>\<strong\>.*$/Profile Pictures/' | sed 's/\&\#039\;/‘/g' | sed 's/\"\;/‘‘/g' | sed 's/\&hearts\;/♥/g')
photo_tot=$(echo "${1}" | tr '"' "\n" | grep -A 3 "${album_id}" | tail -n 1 | sed 's/.\(.*\) photos.*$/\1/' | sed 's/.*Profile Pictures.*/0/')
album_tot=$(( ($album_inc + $i) - 1 ))
album_page=0
photo_ind=0
printf "\t[%d] %s (%d) [%s]\n" $album_tot "${album_name}" "${photo_tot}" "${album_id}"
parse_album
done
)
}
function parse_album() {
printf "Parsing Album %s [%s]...\n" "${album_name}" "${album_id}"
short_id=$(echo "${album_id}" | awk -F\. '{print $2}')
photos_html=$(curl -s -b ${cookie_path} -c ${cookie_path} -A 'Mozilla/5.0' --location "http://m.facebook.com/${album_url}")
echo -n "${photos_html}" > "${output_path}/m.album-${short_id}-${album_page}_[${fb_id}].htm"
parse_photo "${photos_html}"
next_page=$(echo "${photos_html}" | tr '"' "\n" | grep "s\=12" | sed 's/\&\;/\&/g' | sed 's/.\(.*\)$/\1/')
if [ "${next_page}" ]; then
photo_inc=12
next_photos; fi
}
function parse_photo() {
photo_urls=$(echo "${1}" | tr '"' "\n" | grep "\_t\.jpg" | sed 's/_t/_n/')
if [ ! "${photo_urls}" ]; then
photo_urls=$(echo "${1}" | tr '"' "\n" | grep "\/t.*\.jpg" | sed 's/\/t\(.*\)\.jpg/\/n\1\.jpg/'); fi
(IFS=$'\n'
for (( i=1; i<=`echo "${photo_urls}" | wc -l`; i++ )); do
photo_url=$(echo "${photo_urls}" | sed -n "${i}"p)
photo_id=$(echo "${photo_url}" | awk -F\_ '{print $2}')
photo_page=$(printf "photo.php?fbid=%s&id=%s&set=%s&refid=%s" "${photo_id}" "${fb_id}" "${album_id}" "${ref_id}")
photo_name=$(echo "${photo_url}" | awk -F\/ '{print $NF}')
photo_ind=$(( $photo_ind + 1 ))
printf "\t[%d] %s\n" $photo_ind "${photo_url}"
echo "${photo_url}" >> "${photo_path}/${fb_id}/${short_id}_${album_name}.txt"
done
)
}
function next_photos() {
short_id=$(echo "${album_id}" | awk -F\. '{print $2}')
photos_html=$(curl -s -b ${cookie_path} -c ${cookie_path} -A 'Mozilla/5.0' --location "http://m.facebook.com/${album_url}&s=${photo_inc}")
echo -n "${photos_html}" > "${output_path}/m.album-${short_id}-${album_page}_[${fb_id}].htm"
parse_photo "${photos_html}"
photo_inc=$(( $photo_inc + 12 ))
next_page=$(echo "${photos_html}" | tr '"' "\n" | grep "s\=${photo_inc}" | sed 's/\&\;/\&/g' | sed 's/.\(.*\)$/\1/')
if [ "${next_page}" ]; then
next_photos; fi
}
function download_photos() {
(IFS=$'\n'
printf "Parsing Album %s [%s]...\n" "${album_name}" "${album_id}"
for f in `find "${photo_path}/${fb_id}" -type f -name "*.txt"`; do
echo "${f}"
album_id=$(echo `basename ${f}` | awk -F\_ '{print $1}')
album_name=$(echo `basename ${f}` | awk -F\_ '{print $2}' | sed 's/\.txt//')
album_path="${photo_path}/${fb_id}/albums/${album_id}_${album_name}"
mkdir -p "./${album_path}"
while read l; do
img_name=$(echo "${l}" | awk -F\/ '{print $NF}')
echo "${img_name}"
curl --silent --location "${l}" --output "./${album_path}/${img_name}"
done < $f
done
)
}
function parse_wall() {
wall_html=$(curl -s -b ${cookie_path} -c ${cookie_path} -A 'Mozilla/5.0' --location "http://m.facebook.com/${wall_url}")
echo -n "${wall_html}" > "${output_path}/m.wall_[${fb_id}].htm"
}
#LOGIN
function login_user() {
printf "Logging In...\n"
params="email=${email}&pass=${pass}&"`parse_form "${form_fields}"`
login_html=$(curl -s -b ${cookie_path} -c ${cookie_path} -A "Mozilla/5.0" -d "${params}" --location "${form_action}")
echo -n "${login_html}" > "${output_path}/m.login.htm"
home_html=$(curl -s -b ${cookie_path} -c ${cookie_path} -A "Mozilla/5.0" --location "${host_url}/profile.php")
echo -n "${home_html}" > "${output_path}/m.home.htm"
isDiff=$(echo "${login_html}" | grep "Log In")
if [ -z "$isDiff" ]; then
isLoggedIn=true
else
isLoggedIn=false;
echo "Can't login!"; fi
}
#LOGOUT
function logout_user() {
printf "Logging Out...\n"
logout_url=$(echo "${home_html}" | tr '"' "\n" | grep "/logout.php?")
logout_html=$(curl -s -b ${cookie_path} -c ${cookie_path} -A "Mozilla/5.0" --location "${host_url}${logout_url}")
echo -n "${logout_html}" > "${output_path}/m.logout.htm"
isLoggedIn=false
rm $cookie_path
}
login_user
if $isLoggedIn; then
parse_info "${fb_id}"
parse_wall
parse_friends
parse_photos
#download_photos
logout_user
fi
exit 1;
Comments