summaryrefslogtreecommitdiff
path: root/search.php
diff options
context:
space:
mode:
authorPhil Burton <philbeansburton@gmail.com>2015-12-07 14:30:28 +0000
committerPhil Burton <philbeansburton@gmail.com>2015-12-07 14:30:28 +0000
commita1a294d5ac75f74e615134faa76841e660cf823f (patch)
tree4fabc6d604d16d315dcd396475ddcded5273f6b1 /search.php
parent33431d562ef7cc43834c65a66ba9365370c9a968 (diff)
parent75872be417067e4ef1f8d128b96fc338540cda50 (diff)
Merge branch 'help-version-args' into 'master'
Add options to display the version and help message It's nice to have a version output from -v and --version so we can check that we're running the latest code in IRC. It's also nice to have a help option so people know how to use it, particularly since the #2 syntax for page numbers is new and undocumented. I also added the command name (eg blaup) to the config so that it shows the correct thing in the help output. I had to ommit the ! from the start as if I used that, it just printed 1 for $config["command"] (I guess this is php interpreting !blaup as a boolean?) Also added a new line to the end of the output for tidiness in a terminal, and cleaned up some unnecessary whitespace See merge request !2
Diffstat (limited to 'search.php')
-rw-r--r--search.php141
1 files changed, 79 insertions, 62 deletions
diff --git a/search.php b/search.php
index c2845f7..c3e2d6d 100644
--- a/search.php
+++ b/search.php
@@ -1,5 +1,7 @@
<?php
+ $version = 1.2;
+
$config = false;
try {
if (!is_readable("config.ini")) {
@@ -8,7 +10,12 @@ try {
if (!$config = parse_ini_file("config.ini")) {
throw new Exception('Could not parse ini file');
}
-
+ if (!isset($config["url"])) {
+ throw new Exception('`url` not found in ini file');
+ }
+ if (!isset($config["command"])) {
+ throw new Exception('`command` not found in ini file');
+ }
// hash the password
if (isset($config['password'])) {
$hash = hash("sha256", $config['password']);
@@ -20,7 +27,7 @@ try {
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$output = json_decode(curl_exec($curl));
curl_close($curl);
-
+
$search = read_stdin();
if (empty($search)) {
@@ -29,86 +36,96 @@ try {
$args = explode(" ", $search);
$page = 1;
+ $out = [];
+ if ($args[0] == "-v" || $args[0] == "--version") {
+ $out[] = "blaupload-search v" . $version;
+ } elseif ($args[0] == "-h" || $args[0] == "--help") {
+ $help = "Usage: !" . $config["command"] . " [query] #[page] | ";
+ $help .= "e.g: !" . $config["command"] . " intense | ";
+ $help .= "!" . $config["command"] . " intense marcus | ";
+ $help .= "!" . $config["command"] . " intense marcus #2 (returns the second page of results)";
+ $out[] = $help;
+ } else {
- // Check to see if user specified pagination
- $pages = array_filter($args, function($elem) {
- if (strpos($elem, "#") === 0) {
- return true;
+ // Check to see if user specified pagination
+ $pages = array_filter($args, function($elem) {
+ if (strpos($elem, "#") === 0) {
+ return true;
+ }
+ return false;
+ });
+ // reset the indexes after the array filter
+ $pages = array_merge($pages);
+
+ if (count($pages) > 1) {
+ throw new Exception("Too many page choices. Enter only one `#<page>`");
}
- return false;
- });
- // reset the indexes after the array filter
- $pages = array_merge($pages);
- if (count($pages) > 1) {
- throw new Exception("Too many page choices. Enter only one `#<page>`");
- }
+ if (!empty($pages)) {
+ // Set the page
+ $page = substr($pages[0], 1);
+ }
+ if (!is_int((int)$page) || $page <= 0) {
+ throw new Exception("Page `" . $page . "` is not valid");
+ }
- if (!empty($pages)) {
- // Set the page
- $page = substr($pages[0], 1);
- }
- if (!is_int((int)$page) || $page <= 0) {
- throw new Exception("Page `" . $page . "` is not valid");
- }
-
- // remove the pagination from the search args
- $pos = array_search("#" . $page, $args);
- if ($pos) {
- $args[$pos] = null;
- }
-
- // Really slow search
- $results = [];
- foreach ($output as $upload) {
- $match = false;
- foreach ($args as $arg) {
- if (!is_null($arg) && stripos($upload->filename, $arg) === false) {
- $match = false;
- break;
- }
- $match = true;
+ // remove the pagination from the search args
+ $pos = array_search("#" . $page, $args);
+ if ($pos) {
+ $args[$pos] = null;
}
- if ($match) {
- $results[] = $config["url"] . "/" . rawurlencode($upload->filename);
+
+ // Really slow search
+ $results = [];
+ foreach ($output as $upload) {
+ $match = false;
+ foreach ($args as $arg) {
+ if (!is_null($arg) && stripos($upload->filename, $arg) === false) {
+ $match = false;
+ break;
+ }
+ $match = true;
+ }
+ if ($match) {
+ $results[] = $config["url"] . "/" . rawurlencode($upload->filename);
+ }
}
- }
- // Return results
- $out = [];
- if (empty($results)) {
- echo "No results found\n";
- } else {
- $count = count($results);
- $i = 0;
+ // Return results
+ if (empty($results)) {
+ echo "No results found\n";
+ } else {
+ $count = count($results);
+ $i = 0;
- $start = ($page-1)*3;
- $end = $start+2;
- if ($start > ($count-1)) {
- throw new Exception("No results for this page");
- }
- foreach($results as $result) {
- if ($i < $start || $i > $end) {
+ $start = ($page-1)*3;
+ $end = $start+2;
+ if ($start > ($count-1)) {
+ throw new Exception("No results for this page");
+ }
+ foreach($results as $result) {
+ if ($i < $start || $i > $end) {
+ $i++;
+ continue;
+ }
+ $out[] = $i+1 . ": " . $result;
$i++;
- continue;
}
- $out[] = $i+1 . ": " . $result;
- $i++;
- }
- if ($count > 3) {
- $out[] = "results (" . $count . ")";
+ if ($count > 3) {
+ $out[] = "results (" . $count . ")";
+ }
}
}
- echo implode(" - ", $out);
+ echo implode(" - ", $out) . "\n";
} catch (Exception $e) {
echo $e->getMessage() . "\n";die();
}
function read_stdin()
-{
+{
$fr=fopen("php://stdin","r"); // open our file pointer to read from stdin
$input = fgets($fr,128); // read a maximum of 128 characters
$input = rtrim($input); // trim any trailing spaces.