This commit is contained in:
fanir 2017-05-09 00:10:41 +02:00
parent 935c65d38d
commit 51ded037a6

129
index.php
View file

@ -15,6 +15,15 @@
display: inline-block; display: inline-block;
} }
table#result {
border-collapse: collapse;
}
table#result td {
border: 1px solid gray;
padding: 0.5em;
}
div.table { div.table {
display: table; display: table;
max-width: 800px; max-width: 800px;
@ -35,6 +44,7 @@
display: table-cell; display: table-cell;
margin-top: 0.5em; margin-top: 0.5em;
margin-bottom: 0.5em; margin-bottom: 0.5em;
} }
div.table>div.row>:first-child { div.table>div.row>:first-child {
vertical-align: top; vertical-align: top;
@ -62,14 +72,18 @@
border-color: hsl(224, 78%, 75%); border-color: hsl(224, 78%, 75%);
background-color: hsl(224, 78%, 90%); background-color: hsl(224, 78%, 90%);
} }
:required:invalid, :focus:invalid {
/* insert your own styles for invalid form input */
background-color: #ffeeee;
}
</style> </style>
</head> </head>
<body> <body>
<h1>MtG Card Data Gatherer</h1> <h1>MtG Card Data Collector</h1>
<?php <?php
require 'vendor/autoload.php'; require 'vendor/autoload.php';
$engine = new StringTemplate\Engine;
//$context = stream_context_create(['wrapper' => [ //$context = stream_context_create(['wrapper' => [
// 'user_agent' => '', // 'user_agent' => '',
@ -77,47 +91,124 @@ $engine = new StringTemplate\Engine;
//]]); //]]);
function get_card_data($set, $num) { function get_card_data($url) {
$fp = fopen('https://api.magicthegathering.io/v1/cards?set=' . $set . '&number=' . $num, 'r'); $fp = fopen($url, 'r');
$meta = stream_get_meta_data($fp); $meta = stream_get_meta_data($fp);
$content = stream_get_contents($fp); $content = stream_get_contents($fp);
fclose($fp); fclose($fp);
var_dump($meta); $headers = [];
var_dump(json_decode($content, true)['cards'][0]); foreach ($meta["wrapper_data"] as $line) {
$parts = explode(":", $line, 2);
if (isset($parts[1]))
$headers[trim($parts[0])] = trim($parts[1]);
}
return ["meta" => $meta, "header" => $headers, "data" => json_decode($content, true)['cards'][0]];
} }
function x() {
$engine = new StringTemplate\Engine;
if (isset($_REQUEST['query'])) { if (isset($_REQUEST['query'])) {
$q = $_REQUEST['query']; $q = $_REQUEST['query'];
$template = $_REQUEST['format']; if (strlen($q) < 3){
echo "<p style='font-size: 1.5em; margin: 2em 1em;'>(屮゚Д゚)屮</p>";
return;
}
switch ($_REQUEST['format']) {
case "set:num":
$template = "{set}: {number}";
break;
case "cardname":
default:
$template = "{name}";
}
$lines = explode("\n", $q); $lines = explode("\n", $q);
if (count($lines) > 100) {
echo "<p>Please request no more than 100 cards at a time</p>";
return;
}
echo '<pre style="font-family: monospace; background-color: lightgray; border-radius: 5px; padding: 3px 6px;">'; echo '<pre style="font-family: monospace; background-color: lightgray; border-radius: 5px; padding: 3px 6px;">';
foreach ($lines as $line) { if (!empty($_REQUEST["prefix"]))
$parts = explode(' ', $line); echo '<table id=result><tbody>';
echo $engine->render($template, $parts); $urls_only = false;
get_card_data($parts[0], $parts[1]); 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 "<br>Rate limit exceeded. Please try again in a few hours or use the raw result URLs.<br><br>";
$urls_only = true;
}
}
if (!empty($_REQUEST["prefix"]))
echo "<tr><td><a href=\"$url\">$parts[0] $parts[1]</a></td><td>";
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 "<a href=\"$url\">$url</a>";
if (!empty($_REQUEST["prefix"]))
echo '</td></tr>';
else
echo "\n"; echo "\n";
} }
if (!empty($_REQUEST["prefix"]))
echo '</tbody></table>';
echo '</pre><hr>'; echo '</pre><hr>';
}
} }
x()
?> ?>
<form method=post> <form id=form method=post>
<div class="table"> <div class="table">
<div class="row"> <div class="row">
<label for="format">Output Format:</label> <label for="format">Output Format:</label>
<input type="text" id="format" name="format" <?= $_REQUEST['format'] ? "value=\"$_REQUEST[format]\"" : '' ?>> <div>
<select type="text" id="format" name="format" required>
<?php
foreach ([
"Card name" => "cardname",
"Set: Number" => "set:num",
] as $label => $value) {
$selected = $_REQUEST["format"] === $value ? " selected" : "";
echo "<option value=\"$value\" $selected>$label</option>";
}
?>
</select>
<label style="display: block; margin: 0.25em 0;">
<input name=prefix value=1 type=checkbox <?= !empty($_REQUEST["prefix"]) ? "checked" : "" ?>>
Prefix with input and raw result URL
</label>
</div>
</div> </div>
<div class="row"> <div class="row">
<label for="query">Cards:</label> <label for="query">Cards:</label>
<textarea id="query" name="query" rows="20" placeholder="M12 120 <textarea id="query" name="query" rows="20" <?= !isset($_REQUEST['query']) ? "autofocus" : "" ?> required placeholder="M12 7
ORI 53 ORI 25
AER 178">M12 120 AER 178"><?= !empty($_REQUEST['query']) ? $_REQUEST['query'] : "M12 7\nORI 25\nAER 178" ?></textarea>
ORI 53 </div>
AER 178</textarea> <div class="row">
<div></div>
<p style="padding: 1em 0;">
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.
</p>
</div> </div>
<div class="row"> <div class="row">
<span></span> <span></span>