From 51ded037a6439c1c9da2b0b3b88c47ff119caa7d Mon Sep 17 00:00:00 2001 From: Christian Damken Date: Tue, 9 May 2017 00:10:41 +0200 Subject: [PATCH] 1.0.0 --- index.php | 151 +++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 121 insertions(+), 30 deletions(-) diff --git a/index.php b/index.php index 2dcc0b9..97d04c9 100755 --- a/index.php +++ b/index.php @@ -15,6 +15,15 @@ display: inline-block; } + table#result { + border-collapse: collapse; + } + + table#result td { + border: 1px solid gray; + padding: 0.5em; + } + div.table { display: table; max-width: 800px; @@ -35,6 +44,7 @@ display: table-cell; margin-top: 0.5em; margin-bottom: 0.5em; + } div.table>div.row>:first-child { vertical-align: top; @@ -62,14 +72,18 @@ border-color: hsl(224, 78%, 75%); background-color: hsl(224, 78%, 90%); } + + :required:invalid, :focus:invalid { + /* insert your own styles for invalid form input */ + background-color: #ffeeee; + } -

MtG Card Data Gatherer

+

MtG Card Data Collector

[ // 'user_agent' => '', @@ -77,47 +91,124 @@ $engine = new StringTemplate\Engine; //]]); -function get_card_data($set, $num) { - $fp = fopen('https://api.magicthegathering.io/v1/cards?set=' . $set . '&number=' . $num, 'r'); +function get_card_data($url) { + $fp = fopen($url, 'r'); $meta = stream_get_meta_data($fp); $content = stream_get_contents($fp); fclose($fp); - var_dump($meta); - var_dump(json_decode($content, true)['cards'][0]); -} - - -if (isset($_REQUEST['query'])) { - $q = $_REQUEST['query']; - $template = $_REQUEST['format']; - - $lines = explode("\n", $q); - - echo '
';
-    foreach ($lines as $line) {
-        $parts = explode(' ', $line);
-
-        echo $engine->render($template, $parts);
-        get_card_data($parts[0], $parts[1]);
-        echo "\n";
+    $headers = [];
+    foreach ($meta["wrapper_data"] as $line) {
+        $parts = explode(":", $line, 2);
+        if (isset($parts[1]))
+            $headers[trim($parts[0])] = trim($parts[1]);
     }
-    echo '

'; + + return ["meta" => $meta, "header" => $headers, "data" => json_decode($content, true)['cards'][0]]; } + +function x() { + $engine = new StringTemplate\Engine; + + if (isset($_REQUEST['query'])) { + $q = $_REQUEST['query']; + if (strlen($q) < 3){ + echo "

(屮゚Д゚)屮

"; + return; + } + + switch ($_REQUEST['format']) { + case "set:num": + $template = "{set}: {number}"; + break; + case "cardname": + default: + $template = "{name}"; + } + + $lines = explode("\n", $q); + if (count($lines) > 100) { + echo "

Please request no more than 100 cards at a time

"; + return; + } + + echo '
';
+        if (!empty($_REQUEST["prefix"]))
+            echo '';
+
+        $urls_only = false;
+        foreach ($lines as $i => $line) {
+            $parts = explode(' ', trim($line));
+            $url = "https://api.magicthegathering.io/v1/cards?set=$parts[0]&number=$parts[1]";
+
+            if (!$urls_only) {
+                $response = get_card_data($url);
+                if ($i > 1){
+                #if (isset($response["header"]["Ratelimit-Remaining"]) and $response["header"]["Ratelimit-Remaining"] === "0") {
+                    echo "
Rate limit exceeded. Please try again in a few hours or use the raw result URLs.

"; + $urls_only = true; + } + } + + if (!empty($_REQUEST["prefix"])) + echo "'; + else + echo "\n"; + } + if (!empty($_REQUEST["prefix"])) + echo '
$parts[0] $parts[1]"; + elseif ($urls_only) + echo "$parts[0] $parts[1]: "; + + if (empty($response["data"])) + echo "Card not found :("; + elseif (!$urls_only) + echo $engine->render($template, $response["data"]); + else + echo "$url"; + + if (!empty($_REQUEST["prefix"])) + echo '
'; + echo '

'; + } +} +x() ?> -
+
- > +
+ + +
- + +
+
+
+

+ Please keep in mind the data source for this service enforces a daily request limit, + so please don't waste it. If no cards are found and you are sure your set codes and + card numbers are valid, try again in a few hours or use the raw result URLs. +