-
-
Save shekkbuilder/0d5386fca93c9ae60175 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
/* | |
* A program to grab a quick summary of some topic from Wikipedia. | |
* Usage: wiki <subject> | |
* | |
* subject can contain spaces if, for example, it is more than one word. | |
* Examples | |
* `wiki cherry bomb` | |
* `wiki Hercules` | |
* | |
* Douglas Lenz 2015 | |
* | |
*/ | |
// Verify the correct number of command line args have been given | |
$num_args = count($argv) - 1; | |
if($num_args == 0) { | |
echo "Usage: wiki <subject>\n"; | |
exit(0); | |
} | |
// Get the command line args that constitute the search term | |
$args = []; | |
for($i=0; $i < $num_args; $i++) { | |
$args[] = $argv[$i+1]; | |
} | |
// Where there are spaces, instead use _ | |
$search_term = join("_", $args); | |
// Set up curl options | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_URL, "http://en.wikipedia.org/w/api.php?continue=&action=query&titles=$search_term&prop=extracts&exintro=&explaintext=&format=json&redirects"); | |
curl_setopt($curl, CURLOPT_HEADER, 0); | |
curl_setopt($curl, CURLOPT_USERAGENT, "WikiTerm/1.0"); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
// Run and close curl | |
$result = curl_exec($curl); | |
curl_close($curl); | |
// Parse the JSON response and locate the data we want | |
$data = json_decode($result, true); | |
$subdata = $data['query']['pages']; | |
$subsubdata = $subdata[array_keys($subdata)[0]]; | |
// Display to screen | |
if(array_key_exists('missing', $subsubdata)) { | |
echo "No articles found."; | |
} else { | |
echo "\n" . $subsubdata['title'] . "\n\n"; | |
exec("fold -s -w 80 <<EOF\n" . $subsubdata['extract'] . "\nEOF", $lines, $_); | |
foreach($lines as $line) { | |
echo $line . "\n"; | |
} | |
//echo $subsubdata['extract']; | |
} | |
echo "\n"; | |
exit(0); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment