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 replaceImage($data->media->large);
34 if (count($data->media->screenshots ?? [])) {
36 foreach ($data->media->screenshots as &$url) {
40 if (count($data->media->details ?? [])) {
42 foreach ($data->media->details as $detail) {
43 if ($detail->type == 'image') {
44 replaceImage($detail->url);
45 replaceimage($detail->thumb);
49 file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT) . "\n");
52 function replaceImage(&$url)
56 preg_match('#https://www.filepicker.io/api/file/([^/]+)/convert\?w=720#', $url, $matches);
57 if (isset($matches[1])) {
58 $url = 'https://d3e4aumcqn8cw3.cloudfront.net/api/file/' . $matches[1];
61 if (strlen($url) == 55) {
62 preg_match('#https://www.filepicker.io/api/file/([^/]+)$#', $url, $matches);
63 if (isset($matches[1])) {
64 $url = 'https://d3e4aumcqn8cw3.cloudfront.net/api/file/' . $matches[1];
68 if (isset($mapping[$url])) {
69 $url = $mapping[$url];