Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 119 lines (100 sloc) 3.11 KB
#!/usr/bin/php
<?php
if(isset($_GET) && array_key_exists('source', $_GET)) {
$source = file_get_contents(__FILE__);
header('Content-type: text/plain');
echo str_replace("#!/usr/bin/php\n", '', $source);
die();
}
if(count($argv) == 1) {
echo "Usage: efinger firstname lastname\n";
die();
}
$search = implode(' ', array_slice($argv, 1));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://epdx.org/search/' . urlencode($search) . '.json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'eFinger 1.0 (https://github.com/aaronpk/ePDX-CLI)');
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($httpcode == 302) {
// Search returned just one result.
if(preg_match('/Location: (http:\/\/epdx.org\/([a-z]+)\/[0-9]+)/', $response, $match)) {
$url = trim($match[1]) . '.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'eFinger 1.0 (https://github.com/aaronpk/ePDX-CLI)');
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($httpcode != 200) {
echo 'Error fetching profile from ' . $url . "\n";
}
$data = json_decode($response);
switch($match[2]) {
case 'people':
echo getPersonOutput($data->person);
break;
}
}
else {
echo "Sorry, I didn't find anything matching your search.\n";
die();
}
} elseif($httpcode == 200) {
$parts = explode("\r\n\r\n", $response);
$results = json_decode(trim($parts[1]));
if(count($results) > 0) {
echo "\n";
foreach($results as $result) {
if(property_exists($result, 'person')) {
echo getPersonOutput($result->person);
echo "\n";
}
}
} else {
echo 'No results found.' . "\n";
}
} else {
echo 'Error Searching!' . "\n";
}
function getPersonOutput(&$person) {
$out = '';
$out .= getField($person, 'name', 'Name');
$out .= getField($person, 'twitter', 'Twitter');
$out .= getField($person, 'url', 'URL');
$out .= getField($person, 'location', 'Location');
$out .= getList($person, 'tags', 'Tags');
$out .= getList($person, 'groups', 'Groups');
$out .= getList($person, 'companies', 'Companies', 'Company');
$out .= getList($person, 'projects', 'Projects');
$out .= 'Bio: ';
$out .= trim($person->bio) . "\n";
$out .= getField($person, 'updated_at', 'Last Updated', TRUE);
return $out;
}
function getField(&$bio, $field, $title, $isDate=FALSE) {
if(property_exists($bio, $field) && $bio->{$field} != '') {
$data = $bio->{$field};
if($isDate) {
$data = date('n/j/Y g:ia', strtotime($data));
}
return $title . ': ' . $data . "\n";
}
return '';
}
function getList(&$bio, $field, $title, $titleSingular=FALSE) {
if(property_exists($bio, $field)) {
$items = array();
foreach($bio->{$field} as $n) {
$items[] = $n->name;
}
if(count($items) > 0) {
return ((count($items) == 1 && $titleSingular) ? $titleSingular : $title) . ': ' . implode(', ', $items) . "\n";
}
}
return '';
}