Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

PHP command line script for searching epdx.org. Includes a super trus…

…tworthy shell script to install it
  • Loading branch information...
commit 5bf6fe0de0ad7e23f3b2616a2141c826fe71d547 0 parents
Aaron Parecki authored

Showing 3 changed files with 139 additions and 0 deletions. Show diff stats Hide diff stats

  1. +16 0 README.md
  2. +118 0 efinger.php
  3. +5 0 install
16 README.md
Source Rendered
... ... @@ -0,0 +1,16 @@
  1 +ePDX Command Line Client
  2 +========================
  3 +
  4 +Yes, it's written in PHP. Deal with it.
  5 +
  6 +Usage:
  7 +$ efinger your name
  8 +
  9 +Installation:
  10 +
  11 +First, look at the shell script:
  12 +$ curl http://aaron.pk/efinger/install
  13 +
  14 +Then, run it!
  15 +$ curl http://aaron.pk/efinger/install | bash
  16 +
118 efinger.php
... ... @@ -0,0 +1,118 @@
  1 +#!/usr/bin/php
  2 +<?php
  3 +
  4 +if(isset($_GET) && array_key_exists('source', $_GET)) {
  5 + $source = file_get_contents(__FILE__);
  6 + header('Content-type: text/plain');
  7 + echo str_replace("#!/usr/bin/php\n", '', $source);
  8 + die();
  9 +}
  10 +
  11 +if(count($argv) == 1) {
  12 + echo "Usage: efinger firstname lastname\n";
  13 + die();
  14 +}
  15 +
  16 +$search = implode(' ', array_slice($argv, 1));
  17 +
  18 +$ch = curl_init();
  19 +curl_setopt($ch, CURLOPT_URL, 'http://epdx.org/search/' . urlencode($search) . '.json');
  20 +curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  21 +curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
  22 +curl_setopt($ch, CURLOPT_HEADER, TRUE);
  23 +curl_setopt($ch, CURLOPT_USERAGENT, 'eFinger 1.0 (https://github.com/aaronpk/ePDX-CLI)');
  24 +$response = curl_exec($ch);
  25 +
  26 +$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  27 +
  28 +if($httpcode == 302) {
  29 + // Search returned just one result.
  30 + if(preg_match('/Location: (http:\/\/epdx.org\/([a-z]+)\/[0-9]+)/', $response, $match)) {
  31 + $url = trim($match[1]) . '.json';
  32 +
  33 + $ch = curl_init();
  34 + curl_setopt($ch, CURLOPT_URL, $url);
  35 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  36 + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
  37 + curl_setopt($ch, CURLOPT_USERAGENT, 'eFinger 1.0 (https://github.com/aaronpk/ePDX-CLI)');
  38 + $response = curl_exec($ch);
  39 +
  40 + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  41 + if($httpcode != 200) {
  42 + echo 'Error fetching profile from ' . $url . "\n";
  43 + }
  44 +
  45 + $data = json_decode($response);
  46 +
  47 + switch($match[2]) {
  48 + case 'people':
  49 + echo getPersonOutput($data->person);
  50 + break;
  51 + }
  52 +
  53 + }
  54 + else {
  55 + echo "Sorry, I didn't find anything matching your search.\n";
  56 + die();
  57 + }
  58 +} elseif($httpcode == 200) {
  59 + $parts = explode("\r\n\r\n", $response);
  60 + $results = json_decode(trim($parts[1]));
  61 +
  62 + if(count($results) > 0) {
  63 + echo "\n";
  64 + foreach($results as $result) {
  65 + if(property_exists($result, 'person')) {
  66 + echo getPersonOutput($result->person);
  67 + echo "\n";
  68 + }
  69 + }
  70 + } else {
  71 + echo 'No results found.' . "\n";
  72 + }
  73 +} else {
  74 + echo 'Error Searching!' . "\n";
  75 +}
  76 +
  77 +
  78 +function getPersonOutput(&$person) {
  79 + $out = '';
  80 + $out .= getField($person, 'name', 'Name');
  81 + $out .= getField($person, 'twitter', 'Twitter');
  82 + $out .= getField($person, 'url', 'URL');
  83 + $out .= getField($person, 'location', 'Location');
  84 + $out .= getList($person, 'tags', 'Tags');
  85 + $out .= getList($person, 'groups', 'Groups');
  86 + $out .= getList($person, 'companies', 'Companies', 'Company');
  87 + $out .= getList($person, 'projects', 'Projects');
  88 + $out .= 'Bio: ';
  89 + $out .= trim($person->bio) . "\n";
  90 + $out .= getField($person, 'updated_at', 'Last Updated', TRUE);
  91 + return $out;
  92 +}
  93 +
  94 +
  95 +
  96 +function getField(&$bio, $field, $title, $isDate=FALSE) {
  97 + if(property_exists($bio, $field) && $bio->{$field} != '') {
  98 + $data = $bio->{$field};
  99 + if($isDate) {
  100 + $data = date('n/j/Y g:ia', strtotime($data));
  101 + }
  102 + return $title . ': ' . $data . "\n";
  103 + }
  104 + return '';
  105 +}
  106 +
  107 +function getList(&$bio, $field, $title, $titleSingular=FALSE) {
  108 + if(property_exists($bio, $field)) {
  109 + $items = array();
  110 + foreach($bio->{$field} as $n) {
  111 + $items[] = $n->name;
  112 + }
  113 + if(count($items) > 0) {
  114 + return ((count($items) == 1 && $titleSingular) ? $titleSingular : $title) . ': ' . implode(', ', $items) . "\n";
  115 + }
  116 + }
  117 + return '';
  118 +}
5 install
... ... @@ -0,0 +1,5 @@
  1 +
  2 +echo "Installing efinger at /usr/bin/efinger"
  3 +sudo curl -o /usr/bin/efinger "http://aaron.pk/efinger/efinger.php?source"
  4 +sudo chmod 755 /usr/bin/efinger
  5 +echo 'Done! try `efinger your name`'

0 comments on commit 5bf6fe0

Please sign in to comment.
Something went wrong with that request. Please try again.