I had a couple of hypotheses, one was that the more friends you had on Facebook, the higher your photo count was likely to be. I wanted to verify this with my friends/my friends of friends on Facebook programatically using AppleScript. I also tried to see if a correlation existed with regard to gender.
read more
on friendExtractionGivenan(id, type)
if type is equal to "indirect" then
tell application "Safari"
set doc to front document
set done_loading to false
do JavaScript "
window.location.assign('https://www.facebook.com/" & id & "?sk=friends&v=friends');
" in doc
delay 1
end tell
end if
if type is equal to "direct" then
tell application "Safari"
set doc to front document
set done_loading to false
do JavaScript "
window.location.assign('https://www.facebook.com/profile.php?id=" & id & "&sk=friends&v=friends');
" in doc
delay 1
end tell
end if
tell application "Safari"
repeat while done_loading is not equal to true
set doc to front document
set done_loading to do JavaScript "
var done = false;
if (document.readyState=='complete'){
done=true;
}
done;
" in doc
delay 1
end repeat
end tell
tell application "Safari"
set doc to front document
set this_url to URL of doc
set done_scrolling to false
set filtered_URLs to {}
set completed_once to false
set erred_once to false
repeat while done_scrolling is not equal to true
try
set done_scrolling to do JavaScript "
var completed_once = " & completed_once & ";
if(completed_once!=true){
var done;
var number_of_trys =0;
}
function scrollToBottom(){
bottom = document.body.scrollHeight;
current = window.innerHeight+ document.body.scrollTop;
done = false;
if((bottom-current) >0){
window.scrollTo(0, bottom);
setTimeout (scrollToBottom, 3500 ); //If the loading exceeds this timeout, a try will be used.
}
else {
done = true;
}
};
if(completed_once!=true){
scrollToBottom();
}
if(done==true&&number_of_trys<10){//This number changes the number of allowed timeouts
done=false;
number_of_trys++;
setTimeout(scrollToBottom, 2000);
//This number sets the allotted timeout length.
//If the loading exceeds both the timeout above and this timeout, another try will be used immediately.
}
done;
" in doc
set completed_once to true
delay 1.0
on error
end try
end repeat
set URLs to false
repeat while URLs is equal to false
set URLs to do JavaScript "
URLArray = [];
allLinks = document.links;
for (i = 0; i < allLinks.length; i++) {
URLArray.push(allLinks[i].href);
}
URLArray;
" in doc
end repeat
return URLs
end tell
end friendExtractionGivenan
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
on extractionOfIDsFrom(unfiltered_links)
set unfiltered to unfiltered_links
set filtered to {}
set direct_IDs to {}
set indirect_IDs to {}
set refined_IDs to {}
set returned_IDs to {}
repeat with x from 1 to count of items of unfiltered
set n to item x of unfiltered
if "=pb" is in n then
if n is not in filtered then set end of filtered to n
end if
end repeat
repeat with y from 1 to count of items of filtered
set profile to item y of filtered
if "profile.php?" is in profile then
set end of direct_IDs to text ((offset of "=" in profile) + 1) thru ((offset of "&" in profile) - 1) of profile
else
set end of indirect_IDs to text 25 thru ((offset of "?" in profile) - 1) of profile
end if
end repeat
return direct_IDs & indirect_IDs
end extractionOfIDsFrom
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
on dataExtraction(id, type)
if type is equal to "indirect" then
tell application "Safari"
set doc to front document
set done_loading to false
do JavaScript "
window.location.assign('https://www.facebook.com/" & id & "');
" in doc
delay 1
end tell
end if
if type is equal to "direct" then
tell application "Safari"
set doc to front document
set done_loading to false
do JavaScript "
window.location.assign('https://www.facebook.com/profile.php?id=" & id & "');
" in doc
delay 1
end tell
end if
tell application "Safari"
repeat while done_loading is not equal to true
set doc to front document
set done_loading to do JavaScript "
var done = false;
if (document.readyState=='complete'){
done=true;
}
done;
" in doc
delay 1
end repeat
end tell
tell application "Safari"
set doc to front document
set sourceCode to do JavaScript "
document.documentElement.outerHTML;
" in doc
end tell
set AppleScript's text item delimiters to "(Friends (" in sourceCode
set friendCount to text (theOffset + 25) thru (theOffset + 28) of sourceCode
--return photoCount
try
set friendCount to friendCount as number
on error
set friendCount to text 1 thru 3 of friendCount
end try
set counts to {friendCount, photoCount}
return counts
end dataExtraction
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
on storeIDs(user_id, friendPhotoCount, database1)
tell application "Database Events"
tell database1
set node to make new record with properties {name:user_id}
tell node
make new field with properties {name:"Friend Count", value:(item 1 of friendPhotoCount)}
make new field with properties {name:"Photo Count", value:(item 2 of friendPhotoCount)}
end tell
end tell
end tell
save database1
end storeIDs
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
on existsInDatabaseChecker(user_id, database1)
tell application "Database Events"
if record user_id of database1 exists then
return true
else
return false
end if
end tell
end existsInDatabaseChecker
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
on exportToPlist(database1)
tell application "System Events"
set parent_dictionary to make new property list item with properties {kind:record}
set the plistfile_path to ("Macintosh HD:Users:Puccio:Documents:My Documents:Scripts:Database:" & "FriendPhoto.plist")
set plist to make new property list file with properties {contents:parent_dictionary, name:plistfile_path}
end tell
tell application "Database Events"
repeat with y from 4 to count of records in database1
set thefields to {}
set currentRecordName to name of record y of database1
repeat with x from 2 to count of fields of record currentRecordName in database1
set end of thefields to value of field x of record currentRecordName of database1
end repeat
tell application "System Events"
tell plist
set node to make new property list item at end with properties {kind:record, name:currentRecordName}
tell node
set fieldID to item 1 of thefields
make new property list item at end with properties {name:"Number of Friends", value:fieldID}
set fieldID to item 2 of thefields
make new property list item at end with properties {name:"Number of Photos", value:fieldID}
end tell
end tell
end tell
end repeat
end tell
end exportToPlist
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
set theOutputFolder to POSIX path of "Macintosh HD:Users:Puccio:Documents:My Documents:Scripts:Database:"
set databaselocation to POSIX path of "Macintosh HD:Users:Puccio:Documents:My Documents:Scripts:Database:PhotoFriend.dbev"
tell application "Database Events"
set database1 to database databaselocation
end tell
--exportToPlist(database1)
set processCancelled to false
set requestingStartingID to display dialog "Enter the subject whose friends you want to analyze" default answer "Monstermac77" with title "Analyze" buttons {"Indirect", "Direct", "Cancel"} default button "Indirect"
set button_pressed to the button returned of the result
if the button_pressed is "Direct" then
set startingIDType to "Direct"
else if the button_pressed is "Indirect" then
set startingIDType to "Indirect"
else
set processCancelled to true
end if
display dialog "Would you like to use what's already in the database?" buttons {"Yes", "No"} default button "Yes"
if button returned of the result is "No" then
set startingID to text returned of requestingStartingID
set unfiltered_links to friendExtractionGivenan(startingID, startingIDType)
set returnedIDs to extractionOfIDsFrom(unfiltered_links)
set refinedIDs to {}
repeat with b from 1 to count of returnedIDs
set IDinterest to item b of returnedIDs
set done to existsInDatabaseChecker(IDinterest, database1)
if done is false then
set end of refinedIDs to IDinterest
end if
end repeat
repeat with a from 1 to count of refinedIDs
set IDofInterest to item a of refinedIDs
set friendPhotoCount to dataExtraction(IDofInterest, "Indirect")
storeIDs(IDofInterest, friendPhotoCount, database1)
end repeat
else
end if
display dialog "What would you like to do?" buttons {"Export file for Grapher", "No"} default button "Export file for Grapher"
if button returned of the result is "Export file for Grapher" then
set outputText to ""
tell application "Database Events"
repeat with y from 4 to count of records in database1
set thefields to {}
set currentRecordName to name of record y of database1
repeat with x from 2 to count of fields of record currentRecordName in database1
set end of thefields to value of field x of record currentRecordName of database1
end repeat
set gotAnError to false
try
set item 1 of thefields to item 1 of thefields as number
on error
set gotAnError to true
end try
try
set item 2 of thefields to item 2 of thefields as number
on error
set gotAnError to true
end try
if gotAnError is false then
set outputText to outputText & return & (item 1 of thefields) & "," & (item 2 of thefields)
end if
end repeat
end tell
tell application "TextEdit"
activate
make new document
set text of document 1 to outputText as text
--save document 1 in "/Users/Puccio/Desktop/hey.txt"
end tell
else
end if