getMessage().' - '.$mdb2->getUserinfo());
}
if ($DEBUG) echo "1\n";
while (true)
{
$mdb2->query("UPDATE friendship_summary SET 2nd_degree = 0 WHERE (complete = false OR complete IS NULL)");
$sql = "SELECT * FROM friendship_summary WHERE 1st_degree <= $max_size";
if ($argv[1] != "")
{
if ($DEBUG) echo("argv[1] : " . $argv[1] . "
\n");
$sql = $sql . " and id = \"" . $argv[1] . "\"";
}
else
{
$sql = $sql . " and (complete = false OR complete IS NULL)";
}
$result = $mdb2->query($sql);
if(PEAR::isError($result)) {
if ($DEBUG) echo("Failed to issue query, error message : " . $result->getMessage() . "
\n");
if ($DEBUG) echo("SQL : " . $sql . "
\n");
}
if ($DEBUG) echo "2\n";
while($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))
{
if ($DEBUG) echo $row . "\n";
$degree_2_sql = "SELECT * FROM friendships WHERE id = '" . $row[id] . "'";
$degree_2_result = $mdb2->query($degree_2_sql);
if(PEAR::isError($degree_2_result)) {
if ($DEBUG) echo("Failed to issue query, error message : " . $degree_2_result->getMessage() . "\n");
}
if ($DEBUG) echo "Loading " . $row[id] . "'s Vancouver friends.\n";
while($degree_2_row = $degree_2_result->fetchRow(MDB2_FETCHMODE_ASSOC))
{
$id = $degree_2_row[friend_id];
mkdir("$id-2");
$page = 1;
$next_relative_url = "friends/?id=$id&nk=$network_id";
if ($DEBUG) echo "Getting friend's total friendships.\n";
// Sleep for 2 seconds.
sleep(5);
`wget --no-verbose --output-document=$id-2/friends_$page.html --load-cookies=cookies.txt --save-cookies=cookies.txt --keep-session-cookies --no-check-certificate --user-agent=Mozilla/5.0 "http://www.facebook.com/$next_relative_url";`;
// Open downloaded page
if (filesize("$id-2/friends_$page.html") < 20000)
{
if ($DEBUG) echo "$id-2/friends_$page.html" . " is too small. Deleting.\n";
unlink("$id-2/friends_$page.html");
}
else
{
if ($DEBUG) echo "Opening friend's page.\n";
$fh2 = fopen("$id-2/friends_$page.html", "r") or die("Could not open file!");
$this_friend_page = fread($fh2, filesize("$id-2/friends_$page.html")) or die("Could not read file!");
fclose($fh2);
// Scrape Total Friendships
if ($page == 1) scrape_total_degree_2_friendships($row[id], $this_friend_page, $mdb2);
}
if ($DEBUG) echo "Deleting $id-2.\n";
deltree("$id-2");
}
complete_summary($row[id], $mdb2);
}
if ($argv[0] != "")
{
break;
}
else
{
sleep(5);
}
}
function deltree($path) {
if (is_dir($path)) {
if (version_compare(PHP_VERSION, '5.0.0') < 0) {
$entries = array();
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) $entries[] = $file;
closedir($handle);
}
} else {
$entries = scandir($path);
if ($entries === false) $entries = array(); // just in case scandir fail...
}
foreach ($entries as $entry) {
if ($entry != '.' && $entry != '..') {
deltree($path.'/'.$entry);
}
}
return rmdir($path);
} else {
return unlink($path);
}
}
function scrape_total_degree_2_friendships($id, $this_friend_page, $mdb2)
{
$DEBUG = true;
if ($DEBUG) echo "Attempting to scrape 2nd degree friend's total friendships.\n";
// Scrape out total friendships
$total_friends_pattern = "/has (\d+) friends/";
preg_match($total_friends_pattern, $this_friend_page, &$total_friend_matches);
// Insert total into database
if ($total_friend_matches[1])
{
if ($DEBUG) echo $id . " has " . $total_friend_matches[1] . " friends.\n";
$sql = "UPDATE friendship_summary SET 2nd_degree = (2nd_degree + " . $total_friend_matches[1] . ") WHERE id = $id";
$result = $mdb2->query($sql);
if(PEAR::isError($result)) {
if ($DEBUG) echo("Failed to issue query, error message : " . $result->getMessage() . "\n");
}
}
else
{
if ($DEBUG) echo $id . " has no friends in Vancouver.\n";
}
}
function complete_summary($id, $mdb2)
{
$DEBUG = true;
$sql = "UPDATE friendship_summary SET complete = true WHERE id = $id";
$result = $mdb2->query($sql);
if(PEAR::isError($result))
{
if ($DEBUG) echo("Failed to issue query, error message : " . $result->getMessage() . "\n");
}
}
function summary_is_completed($id, $mdb2)
{
$DEBUG = true;
$sql = "SELECT * FROM friendship_summary WHERE id = $id";
$result = $mdb2->query($sql);
if(PEAR::isError($result))
{
if ($DEBUG) echo("Failed to issue query, error message : " . $result->getMessage() . "\n");
}
$row = $result->fetchRow(MDB2_FETCHMODE_ASSOC);
return $row[complete];
}
/*
foreach row in FRIENDSHIP_SUMMARY where 1st_degree < 25
for each linked friend in FRIENDSHIPS,
go to first page of linked friend's New York friends
http://www.facebook.com/friends.php?id=PERSONS_ID&nk=67108866
get total friends
update FRIENDSHIP_SUMMARY set 2nd_degree = 2nd_degree + number of friends
update FRIENDSHIP_SUMMARY set timestamp = now
update FRIENDSHIP_SUMMARY set complete = true;
$d = dir(".");
while (false !== ($entry = $d->read())) {
if (preg_match("/^newyork-\d+\.txt/",$entry))
{
$fh = fopen($entry, "r") or die("Could not open file!");
$data = fread($fh, filesize($entry)) or die("Could not read file!");
fclose($fh);
// Get all the ids on that page
$pattern = "/