feat: Add image download logic

This commit is contained in:
overflowerror 2024-07-26 23:07:11 +02:00
parent 904469636b
commit e0d6605a44
6 changed files with 142 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
.idea

39
bin/cron.php Normal file
View file

@ -0,0 +1,39 @@
<?php
require_once __DIR__ . "/../core.php";
require_once __DIR__ . "/../lib/updateData.php";
echo "Loading mob list...\n";
$mobs = getMobs();
echo "Filtering invalid entries...\n";
$mobs = array_filter($mobs, fn ($mob) =>
!str_starts_with($mob, "id=") &&
!str_contains($mob, "Old ") &&
$mob != "NPC" && $mob != "Agent" &&
!str_ends_with($mob, "Ghost") &&
$mob != "Giant" &&
$mob != "Killer Bunny"
);
echo "Fetching image URLs...\n";
$mobs = array_map(fn ($mob) => [
"name" => $mob,
"image" => getImage($mob)
], $mobs);
echo "Removing duplicates...\n";
$mobs = array_reduce($mobs, function ($mobs, $mob) {
$urls = array_map(fn ($mob) => $mob["image"], $mobs);
if (!in_array($mob["image"], $urls)) {
$mobs[] = $mob;
}
return $mobs;
}, []);
echo "Downloading images...\n";
foreach ($mobs as $mob) {
$filename = downloadImage($mob["image"], $mob["name"]);
$mob["filename"] = $filename;
var_dump($mob);
}

1
core.php Normal file
View file

@ -0,0 +1 @@
<?php

1
html/images/mobs/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*

10
lib/request.php Normal file
View file

@ -0,0 +1,10 @@
<?php
function get(string $url): string {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36');
$response = curl_exec($curl);
curl_close($curl);
return $response;
}

90
lib/updateData.php Normal file
View file

@ -0,0 +1,90 @@
<?php
require_once __DIR__ . "/request.php";
function handleWikiRequest(string $args, bool $rvslots = true): array {
$url = "https://minecraft.wiki/api.php?action=query&format=json&" . $args;
if ($rvslots) {
$url .= "&rvslots=main";
}
$response = get($url);
$response = json_decode($response, true);
if ($rvslots) {
$content = getWikiTextFromResponse($response);
if (str_contains($content, "#REDIRECT")) {
$matches = [];
preg_match("/\[\[([^#\]]+)[^]]*]]/", $content, $matches);
$response = get(preg_replace("/titles=[^&]*/", "titles=" . $matches[1], $url));
$response = json_decode($response, true);
}
}
return $response;
}
function getWikiTextFromResponse(array $response): string {
return array_values($response["query"]["pages"])[0]["revisions"][0]["slots"]["main"]["*"];
}
function getImageUrlFromResponse(array $response): string {
return array_values($response["query"]["pages"])[0]["imageinfo"][0]["url"];
}
function getMobs(): array {
$wikiPage = handleWikiRequest("titles=Mob&prop=revisions&rvprop=content");
$wikiText = getWikiTextFromResponse($wikiPage);
$matches = [];
preg_match_all('/\{\{EntityLink\|([^}]+)}}/', $wikiText, $matches);
return array_unique($matches[1]);
}
function getImageName(string $name): string {
$nameWithUnderscores = str_replace(" ", "_", $name);
$wikiPage = handleWikiRequest("titles=" . $nameWithUnderscores . "&prop=revisions&rvprop=content");
$wikiText = getWikiTextFromResponse($wikiPage);
$matches = [];
preg_match("/(image[0-9]*|1-1)\s*=\s*([^\n]+)\n/", $wikiText, $matches);
$imageName = $matches[2] ?? "";
if (str_contains($imageName, ".")) {
return $imageName;
}
preg_match("/" . preg_quote($name) . "[^.]*\.(gif|png|jpg|jpeg)/", $wikiText, $matches);
return $matches[0] ?? "";
}
function getImageUrlFromName(string $filename): string {
$filename = str_replace(" ", "_", $filename);
$response = handleWikiRequest("titles=File:" . $filename . "&prop=imageinfo&iiprop=url", false);
return getImageUrlFromResponse($response);
}
function getImage(string $name): string {
return getImageUrlFromName(getImageName($name));
}
function downloadImage(string $url, string $mobname): string {
$name = preg_replace(
"/[^.]*(\.[a-zA-Z0-9]+).*/",
strtolower(str_replace(" ", "_", $mobname)) . "$1",
basename($url)
);
$file = fopen(__DIR__ . "/../html/images/mobs/" . $name, "w");
$response = get($url);
fwrite($file, $response);
fclose($file);
return $name;
}