* @version 1.0
*/
class DiggSigAPIScraper {
/**
* The user's Digg username.
* @protected string
*/
protected $user_name;
/**
* Array of data pulled from grabbed digg.com pages using regexp.
* @protected array
*/
protected $user_profile_data;
/**
* HTML from the user's profile and dugg page.
* @protected string
*/
protected $user_profile_dugg_html;
/**
* HTML from the user's submitted page.
* @protected string
*/
protected $user_submitted_html;
/**
* HTML from the user's commented on page.
* @protected string
*/
protected $user_commented_html;
/**
* HTML from the user's homepage page.
* @protected string
*/
protected $user_homepage_html;
/**
* HTML from the user's friends page.
* @protected string
*/
protected $user_friends_html;
/**
* Class constructor
*
* @param string $user_name The user's Digg username
* @return void
*/
public function __construct($user_name = null) {
if ($user_name) $this->SetUserName($user_name);
}
/**
* Sets the user's Digg username
*
* Sets the user's Digg username. Always returns TRUE.
*
* @param string $user_name The user's Digg username
* @return bool
*/
public function SetUserName($user_name) {
$this->user_name = str_replace(' ', '_', $user_name);
return true;
}
/**
* Grabs the HTML from digg.com, and runs regexp on it
*
* Grabs all the required pages from digg.com, and runs regexp on the
* contents to get all the user's Digg data. Returns an array with
* all the data, or FALSE if there was a failure.
*
* @return mixed
*/
public function Parse($query = null) {
$query_protecteds = array (
'profile' => '',
'submitted' => '',
'commented' => '',
'homepage' => '',
'friends' => ''
);
if ($query) {
parse_str($query, $query_protecteds);
}
if (isset($query_protecteds['profile'])) {
if (!$this->GetUserProfileDugg()) {
return false;
}
if (!$this->ParseUserProfileDuggHTML()) {
return false;
}
}
if (isset($query_protecteds['submitted'])) {
if (!$this->GetUserSubmitted()) {
return false;
}
if (!$this->ParseUserSubmittedHTML()) {
return false;
}
}
if (isset($query_protecteds['commented'])) {
if (!$this->GetUserCommentedOn()) {
return false;
}
if (!$this->ParseUserCommentedHTML()) {
return false;
}
}
if (isset($query_protecteds['homepage'])) {
if (!$this->GetUserHomepage()) {
return false;
}
if (!$this->ParseUserHomepageHTML()) {
return false;
}
}
if (isset($query_protecteds['friends'])) {
if (!$this->GetUserFriends()) {
return false;
}
if (!$this->ParseUserFriendsHTML()) {
return false;
}
}
return $this->user_profile_data;
}
/**
* Gets the user's profile information from digg.com
*
* Grabs the Digg profile page for the user, and the dugg page for the
* user. Stores the HTML in the class property $user_profile_dugg_html.
* Returns TRUE if the HTML was grabbed successfully, and FALSE if it
* wasn't.
*
* @return bool
*/
private function GetUserProfileDugg() {
if (!$this->user_name) return false;
$profile_html = $this->GetUserPage('http://digg.com/users/' . $this->user_name . '/profile');
if (!$profile_html) return false;
$dugg_html = $this->GetUserPage('http://digg.com/users/' . $this->user_name . '/dugg');
if (!$dugg_html) return false;
$this->user_profile_dugg_html = $profile_html . $dugg_html;
return true;
}
/**
* Gets the user's submitted information from digg.com
*
* Grabs the Digg submitted page for the user. Stores the HTML
* in the class property $user_submitted_html.
* Returns TRUE if the HTML was grabbed successfully, and FALSE if it
* wasn't.
*
* @return bool
*/
private function GetUserSubmitted() {
if (!$this->user_name) return false;
$this->user_submitted_html = $this->GetUserPage('http://digg.com/users/' . $this->user_name . '/submitted');
return $this->user_submitted_html;
}
/**
* Gets the user's commented on information from digg.com
*
* Grabs the Digg commented on page for the user. Stores the HTML
* in the class property $user_commented_html.
* Returns TRUE if the HTML was grabbed successfully, and FALSE if it
* wasn't.
*
* @return bool
*/
private function GetUserCommentedOn() {
if (!$this->user_name) return false;
$this->user_commented_html = $this->GetUserPage('http://digg.com/users/' . $this->user_name . '/commented');
return $this->user_commented_html;
}
/**
* Gets the user's homepage information from digg.com
*
* Grabs the Digg homepage page for the user. Stores the HTML
* in the class property $user_homepage_html.
* Returns TRUE if the HTML was grabbed successfully, and FALSE if it
* wasn't.
*
* @return bool
*/
private function GetUserHomepage() {
if (!$this->user_name) return false;
$this->user_homepage_html = $this->GetUserPage('http://digg.com/users/' . $this->user_name . '/homepage');
return $this->user_homepage_html;
}
/**
* Gets the user's friends information from digg.com
*
* Grabs the Digg friends page for the user. Stores the HTML
* in the class property $user_homepage_html.
* Returns TRUE if the HTML was grabbed successfully, and FALSE if it
* wasn't.
*
* @return bool
*/
private function GetUserFriends() {
if (!$this->user_name) return false;
$this->user_friends_html = $this->GetUserPage('http://digg.com/users/' . $this->user_name . '/friends/list');
return $this->user_friends_html;
}
/**
* Grabs a page from digg.com
*
* Grabs a page from digg.com, and if the $property parameter is set,
* stores the HTML in the variable. Returns the HTML on success and
* FALSE on failure.
*
* @param string $url The URL of the page to grab
* @param string $property The member property to store the HTML in
* @return mixed
*/
private function GetUserPage($url) {
$html = Headzoo::get_http_document($url);
if (!$html) return false;
return $html;
}
/**
* Parse the $user_profile_dugg_html content
*
* Runs regular expressions on the $user_profile_dugg_html property, and
* adds matched data to the $user_profile_data property. Returns TRUE
* if all the regular expressions matched, returns FALSE otherwise.
*
* @return bool
*/
private function ParseUserProfileDuggHTML() {
$regexps = array (
'GetRealName' => "~
Real Name:.(.*)<\/dd>~isU",
'GetMemberName' => "~Member Name:.*(.*)~isU",
'GetAvatarURL' => "~
~iU",
'GetLocation' => "~Location:.*(.*)~isU",
'GetRanking' => "~Overall Ranking:.*user_name}\">(.*)~isU",
'GetNumberOfStoriesDugg' => "~Stories Dugg:.*(.*)\s{4}\(~isU",
'GetNumberOfStoriesSubmitted' => "~Stories Submitted:.*(.*)\s{4}\(~isU",
'GetNumberOfPromotedStories' => "~Promoted Stories:.*(.*)\s{4}\(~isU",
'GetNumberOfProfileViews' => "~Profile Views:.*(.*)~isU"
);
foreach ($regexps as $method_name => $regexp) {
if (!preg_match($regexp, $this->user_profile_dugg_html, $matches)) {
return false;
}
$this->user_profile_data[$method_name] = $matches[1];
}
if (!preg_match_all($this->_GetStoriesPageRegExp(), $this->user_profile_dugg_html, $matches)) {
return false;
}
for($i = 0; $i < count($matches[1]); $i++) {
$this->user_profile_data['GetLastDuggStories'][] = array('title' => $matches[2][$i], 'url' => $matches[1][$i], 'diggs' => $matches[3][$i]);
}
return true;
}
/**
* Parse the $user_submitted_html content
*
* Runs regular expressions on the $user_submitted_html property, and
* adds matched data to the $user_profile_data property. Returns TRUE
* if all the regular expressions matched, returns FALSE otherwise.
*
* @return bool
*/
private function ParseUserSubmittedHTML() {
if (!preg_match_all($this->_GetStoriesPageRegExp(), $this->user_submitted_html, $matches)) {
return false;
}
for($i = 0; $i < count($matches[1]); $i++) {
$this->user_profile_data['GetSubmittedStories'][] = array('title' => $matches[2][$i], 'url' => $matches[1][$i], 'diggs' => $matches[3][$i]);
}
return true;
}
/**
* Parse the $user_commented_html content
*
* Runs regular expressions on the $user_commented_html property, and
* adds matched data to the $user_profile_data property. Returns TRUE
* if all the regular expressions matched, returns FALSE otherwise.
*
* @return bool
*/
private function ParseUserCommentedHTML() {
if (!preg_match_all($this->_GetStoriesPageRegExp(), $this->user_commented_html, $matches)) {
return false;
}
for($i = 0; $i < count($matches[1]); $i++) {
$this->user_profile_data['GetCommentedStories'][] = array('title' => $matches[2][$i], 'url' => $matches[1][$i], 'diggs' => $matches[3][$i]);
}
return true;
}
/**
* Parse the $user_homepage_html content
*
* Runs regular expressions on the $user_homepage_html property, and
* adds matched data to the $user_profile_data property. Returns TRUE
* if all the regular expressions matched, returns FALSE otherwise.
*
* @return bool
*/
private function ParseUserHomepageHTML() {
if (!preg_match_all($this->_GetStoriesPageRegExp(), $this->user_homepage_html, $matches)) {
return false;
}
for($i = 0; $i < count($matches[1]); $i++) {
$this->user_profile_data['GetPromotedStories'][] = array('title' => $matches[2][$i], 'url' => $matches[1][$i], 'diggs' => $matches[3][$i]);
}
return true;
}
/**
* Parse the $user_friends_html content
*
* Runs regular expressions on the $user_friends_html property, and
* adds matched data to the $user_profile_data property. Returns TRUE
* if all the regular expressions matched, returns FALSE otherwise.
*
* @return bool
*/
private function ParseUserFriendsHTML() {
if (!preg_match_all('~~isU', $this->user_friends_html, $matches)) {
return false;
}
for($i = 0; $i < count($matches[1]); $i++) {
$this->user_profile_data['GetFriendsList'][] = array('name' => $matches[2][$i], 'url' => $matches[1][$i]);
}
return true;
}
/**
* Returns the regexp needed for many of the methods
*
* Many of the methods run the same exact regular expression. Because of
* that, the regexp is stored in this function, and retrieved by those
* methods. That way if the regexp needs to be changed, it can be changed
* in this one spot.
*
* @return string
*/
private function _GetStoriesPageRegExp() {
return '~.*(.*)~isU';
}
}
/**
* DiggSig API Get Methods Class
*
* The class is used to get values created by the DiggSigAPIScraper class
* via method calls. This class extends the DiggSigAPIScraper class, and
* this is the class that should be initiated to get user Digg information.
*
* @author Sean Hickey
* @version 1.0
*/
class DiggSigAPI extends DiggSigAPIScraper {
/**
* PHP's magic __call() method to call members methods
*
* PHP's magic __call() method to call member methods on this class
* that doen't exists.
* Faux methods available through this method:
*
* string|null GetRealName()
* string|null GetMemberName()
* string|null GetAvatarURL()
* string|null GetLocation()
* string|null GetRanking()
* string|null GetNumberOfStoriesDugg()
* string|null GetNumberOfStoriesSubmitted()
* string|null GetNumberOfPromotedStories()
* string|null GetNumberOfProfileViews()
* array|null GetLastDuggStories($number_to_return = null)
* array|null GetSubmittedStories($number_to_return = null)
* array|null GetCommentedStories($number_to_return = null)
* array|null GetPromotedStories($number_to_return = null)
* array|null GetFriendsList($number_to_return = null)
*
* @param string $method The name of the method being called
* @param array $args Arguments passed to the method
* @return mixed
*/
public function __call($method, $args) {
if (isset($args[0])) {
$arg = $args[0];
} else {
$arg = null;
}
return $this->GetUserData($method, $arg);
}
/**
* Returns a value from the $user_profile_data array
*
* Given the index key for the $user_profile_data, returns the value
* at that index, or NULL if the index key doesn't exists.
*
* @param string $key The index key
* @return mixed
*/
public function GetUserData($key) {
return (isset($this->user_profile_data[$key])) ? $this->user_profile_data[$key] : null;
}
/**
* Returns an array of stories, or friends for the user
*
* Returns an array of stories, or friends for the user. Can return a
* list of dugg stories, promoted stories, commented on stories,
* submitted stories, or the list of friends for the user.
* The number of array indixes to be returned can be limited with the
* second parameter. Returns NULL if the index doesn't exists.
*
* @param string $key The key for the $user_profile_data array
* @param int $number_to_return The number of array indixes to return
* @return null
*/
public function GetStoryData($key, $number_to_return = null) {
if (!isset($this->user_profile_data[$key]) || empty($this->user_profile_data[$key])) {
return null;
}
$story_list = $this->user_profile_data[$key];
if ($number_to_return) {
if ($number_to_return > count($story_list)) $number_to_return = count($story_list);
return array_slice($story_list, 0, $number_to_return);
} else {
return $story_list;
}
}
}
class DiggSigAPIXMLRPCServer extends IXR_IntrospectionServer {
private $DiggSigAPI;
private $parse_pages = null;
public function SetPages($parse_pages) {
$this->parse_pages = $parse_pages;
return true;
}
public function SetUserName($user_name) {
$this->DiggSigAPI->SetUserName($user_name);
return $this->DiggSigAPI->Parse($this->parse_pages);
}
public function GetRealName($user_name = null) {
if ($user_name) $this->SetUserName($user_name);
return $this->DiggSigAPI->GetRealName();
}
public function GetMemberName($user_name = null) {
if ($user_name) $this->SetUserName($user_name);
return $this->DiggSigAPI->GetMemberName();
}
public function GetAvatarURL($user_name = null) {
if ($user_name) $this->SetUserName($user_name);
return $this->DiggSigAPI->GetAvatarURL();
}
public function GetLocation($user_name = null) {
if ($user_name) $this->SetUserName($user_name);
return $this->DiggSigAPI->GetLocation();
}
public function GetRanking($user_name = null) {
if ($user_name) $this->SetUserName($user_name);
return $this->DiggSigAPI->GetRanking();
}
public function GetNumberOfStoriesDugg($user_name = null) {
if ($user_name) $this->SetUserName($user_name);
return $this->DiggSigAPI->GetNumberOfStoriesDugg();
}
public function GetNumberOfStoriesSubmitted($user_name = null) {
if ($user_name) $this->SetUserName($user_name);
return $this->DiggSigAPI->GetNumberOfStoriesSubmitted();
}
public function GetNumberOfPromotedStories($user_name = null) {
if ($user_name) $this->SetUserName($user_name);
return $this->DiggSigAPI->GetNumberOfPromotedStories();
}
public function GetNumberOfProfileViews($user_name = null) {
if ($user_name) $this->SetUserName($user_name);
return $this->DiggSigAPI->GetNumberOfProfileViews();
}
public function GetLastDuggStories($args = null) {
if ($args[0]) $this->SetUserName($args[0]);
return $this->DiggSigAPI->GetLastDuggStories($args[1]);
}
public function GetSubmittedStories($args = null) {
if ($args[0]) $this->SetUserName($args[0]);
return $this->DiggSigAPI->GetSubmittedStories($args[1]);
}
public function GetCommentedStories($args = null) {
if ($args[0]) $this->SetUserName($args[0]);
return $this->DiggSigAPI->GetCommentedStories($args[1]);
}
public function GetPromotedStories($args = null) {
if ($args[0]) $this->SetUserName($args[0]);
return $this->DiggSigAPI->GetPromotedStories($args[1]);
}
public function GetFriendsList($args = null) {
if ($args[0]) $this->SetUserName($args[0]);
return $this->DiggSigAPI->GetFriendsList($args[1]);
}
public function __construct(DiggSigAPI $DiggSigAPI) {
$this->DiggSigAPI = $DiggSigAPI;
$this->addCallback (
'digg.SetPages',
'this:SetPages',
array('bool', 'string'),
"Sets the pages to download from Digg"
);
$this->addCallback (
'digg.SetUserName',
'this:SetUserName',
array('bool', 'string'),
"Sets the user's Digg username"
);
$this->addCallback (
'digg.GetRealName',
'this:GetRealName',
array('string', 'string'),
"Returns the user's real name"
);
$this->addCallback (
'digg.GetMemberName',
'this:GetMemberName',
array('string', 'string'),
"Returns the user's member name"
);
$this->addCallback (
'digg.GetAvatarURL',
'this:GetAvatarURL',
array('string', 'string'),
"Returns the user's avatar URL"
);
$this->addCallback (
'digg.GetLocation',
'this:GetLocation',
array('string', 'string'),
"Returns the user's location"
);
$this->addCallback (
'digg.GetRanking',
'this:GetRanking',
array('string', 'string'),
"Returns the user's Digg ranking"
);
$this->addCallback (
'digg.GetNumberOfStoriesDugg',
'this:GetNumberOfStoriesDugg',
array('string', 'string'),
"Returns the number of stories dugg by the user"
);
$this->addCallback (
'digg.GetNumberOfStoriesSubmitted',
'this:GetNumberOfStoriesSubmitted',
array('string', 'string'),
"Returns the number of stories submitted by the user"
);
$this->addCallback (
'digg.GetNumberOfPromotedStories',
'this:GetNumberOfPromotedStories',
array('string', 'string'),
"Returns the number of stories submitted by the user that were promoted"
);
$this->addCallback (
'digg.GetNumberOfProfileViews',
'this:GetNumberOfProfileViews',
array('string', 'string'),
"Returns the number of times the user's profile was viewed"
);
$this->addCallback (
'digg.GetLastDuggStories',
'this:GetLastDuggStories',
array('struct', 'string', 'int'),
"Returns an array of the last 20 stories dugg by the user"
);
$this->addCallback (
'digg.GetSubmittedStories',
'this:GetSubmittedStories',
array('struct', 'string', 'int'),
"Returns an array of the last 20 stories submitted by the user"
);
$this->addCallback (
'digg.GetCommentedStories',
'this:GetCommentedStories',
array('struct', 'string', 'int'),
"Returns an array of the last 20 stories the user commented on"
);
$this->addCallback (
'digg.GetPromotedStories',
'this:GetPromotedStories',
array('struct', 'string', 'int'),
"Returns an array of the last 20 stories the user submitted that were promoted"
);
$this->addCallback (
'digg.GetFriendsList',
'this:GetFriendsList',
array('struct', 'string', 'int'),
"Returns an array of the last 20 friends added to the user's friends list"
);
$this->serve();
}
}
$DiggSigAPIXMLRPCServer = new DiggSigAPIXMLRPCServer(new DiggSigAPI);
?>