Psst.. new poll here.
[email protected] webmail now available. Want one? Go here.
Cannot use outlook/hotmail/live here to register as they blocking our mail servers. #microsoftdeez
Obey the Epel!
Paste
Pasted as text by Eugene ( 15 years ago )
package org.lfhs.klein.www;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.apache.commons.httpclient.NameValuePair;
/**
* Provides a simple java interface to Facebook.com. Handles connecting,
* loading and parsing pages, and cleanly terminating that connection.
*
* @author Peter Klein
*/
public class FaceBookInteractor extends WebInteractor{
public static final String LOGIN_URL = "https://login.facebook.com/login.php";
public static final String SEARCH_URL = "http://www.facebook.com/s.php";
public static final int SCHOOL_ID = 33569307;
static boolean connected = false;
static String error;
private static void prep(){
//This is why god invented constructors
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
System.setProperty ("org.apache.commons.logging.simplelog.showdatetime","true");
System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient","error");
}
/**
* Connects to and logs on to Facebook.<!-- -->com. This method will return true if
* the login succeeded, or false if it failed. If an error occures, false
* will be returned, and a more detailed error report will be avalable by
* calling {@link #getError()}
*
* @param email the email you wish to use to login
* @param pass the password you wish to use to login
* @return true if the login succeeded, or false if it failed
*/
public static boolean connect(String email, String pass){
prep();
//This lets myspace know we support cookies
setCookie("test_cookie","1");
NameValuePair[] postData = {
new NameValuePair("md5pass",""),
new NameValuePair("next",""),
new NameValuePair("email",email),
new NameValuePair("pass",pass)
};
try {
String results = postURL(LOGIN_URL,postData);
} catch (IOException e) {
error = "Problably a Socket/Connection error, make sure facebook is up";
}
/* The cookie 'xs' contains some tracking number,
* and will only show up if login succeeded */
if(!cookies.containsKey("xs")){
error = "Make sure your login information is correct";
} else {
connected = true;
}
return cookies.containsKey("xs");
}
/**
* Given the name of a user on Facebook, will search for their ID.
* This method uses Facebook's search functionality. This will return
* the Facebook ID of the user with the name specified. If a connection
* error occurs, or you arent connected to begin with, -2 will be returned.
* If there is no user found, or more than one user found, -1 will be returned.
*
* @param name a string representing the name to search for
* @return ID number. <code>-1</code>: no or too many results. <code>-2</code>: connection error or not connected.
*/
public static int getId(String name){
String DIVIDER_NUM_S = "10008">";
String DIVIDER_ID_S = "friends.php?id=";
String DIVIDER_ID_E = "">View Friend";
String result = "";
if(!connected){
error = "You must be connected to search";
return -2;
}
try {
result = getURL(SEARCH_URL + "?q=" + URLEncoder.encode(name,"UTF-8") +
"&n=" + SCHOOL_ID);
} catch (IOException e) {
error = "Connection error";
return -2;
}
//Check number of results
if (result.indexOf("Found no results") >= 0){ //No results
error = "Nobody found";
return -1;
}
int numResults = Integer.parseInt(result.substring(
result.indexOf(DIVIDER_NUM_S) + DIVIDER_NUM_S.length(),
result.indexOf(DIVIDER_NUM_S) + DIVIDER_NUM_S.length()+2).trim());
if (numResults > 1){ //Several results
error = "Search found in more than one result";
return -1;
}
int id = Integer.parseInt(result.substring(
result.indexOf(DIVIDER_ID_S) + DIVIDER_ID_S.length(),
result.indexOf(DIVIDER_ID_E)));
return id;
}
//disconnect
/**
* Returns the most recent failure causing error that occured.
*
* @return a string containing details on the most recent error
*/
static public String getError(){
return (error == null)? "no error (or I suck at coding)" : error;
}
}
Revise this Paste