4 * Replace all game image URLs listed in the given CSV file with
5 * the ones in the second CSV column
7 if (!isset($argv[1])) {
8 fwrite(STDERR, "CSV file missing\n");
11 $imageUrlMapFile = $argv[1];
13 $hdl = fopen($imageUrlMapFile, 'r');
15 fwrite(STDERR, "Cannot load image url map file\n");
18 while ($data = fgetcsv($hdl, 4096, ',')) {
19 if (count($data) == 2) {
20 $mapping[$data[0]] = $data[1];
23 if (count($mapping) == 0) {
24 fwrite(STDERR, "Image url map file is empty\n");
28 $files = glob(__DIR__ . '/../games/*.json');
29 foreach ($files as $file) {
30 $data = json_decode(file_get_contents($file));
31 $package = $data->packageName;
32 replaceImage($data->media->discover);
33 if (count($data->media->screenshots ?? [])) {
35 foreach ($data->media->screenshots as &$url) {
39 if (count($data->media->details ?? [])) {
41 foreach ($data->media->details as $detail) {
42 if ($detail->type == 'image') {
43 replaceImage($detail->url);
44 replaceimage($detail->thumb);
48 file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT) . "\n");
51 function replaceImage(&$url)
55 preg_match('#https://www.filepicker.io/api/file/([^/]+)/convert\?w=720#', $url, $matches);
56 if (isset($matches[1])) {
57 $url = 'https://d3e4aumcqn8cw3.cloudfront.net/api/file/' . $matches[1];
60 if (strlen($url) == 55) {
61 preg_match('#https://www.filepicker.io/api/file/([^/]+)$#', $url, $matches);
62 if (isset($matches[1])) {
63 $url = 'https://d3e4aumcqn8cw3.cloudfront.net/api/file/' . $matches[1];
67 if (isset($mapping[$url])) {
68 $url = $mapping[$url];