Script to convert broken .apk download URLs to Internet Archive URLs
authorChristian Weiske <cweiske@cweiske.de>
Wed, 13 Nov 2019 06:41:48 +0000 (07:41 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Wed, 13 Nov 2019 06:41:48 +0000 (07:41 +0100)
bin/convert-internetarchive-apk-urls.php [new file with mode: 0755]

diff --git a/bin/convert-internetarchive-apk-urls.php b/bin/convert-internetarchive-apk-urls.php
new file mode 100755 (executable)
index 0000000..d4dd307
--- /dev/null
@@ -0,0 +1,50 @@
+#!/usr/bin/env php
+<?php
+/**
+ * Take OUYA game data files and replace the broken download links
+ * with archive.org links
+ */
+foreach (glob(__DIR__ . '/../games/*.json') as $gameFile) {
+    $gameData = json_decode(file_get_contents($gameFile));
+    echo $gameFile . "\n";
+    $changed = false;
+    foreach ($gameData->releases as $release) {
+        if (parse_url($release->url, PHP_URL_HOST) != 'devs-ouya-tv-prod.s3.amazonaws.com') {
+            echo " release url ok\n";
+            continue;
+        }
+
+        $releaseNameFile = str_replace(
+            [' ', '(', ')', '!'],
+            ['_', '', '', ''],
+            $release->name
+        );
+        $iaJsonFile = __DIR__ . '/../old-data/ia-data/'
+            . 'ouya_' . $gameData->packageName . '_' . $releaseNameFile . '.json';
+        if (!file_exists($iaJsonFile)) {
+            echo " IA file not found: $iaJsonFile\n";
+            continue;
+        }
+        $iaData = json_decode(file_get_contents($iaJsonFile));
+
+        $url = null;
+        foreach ($iaData->files as $iaFile) {
+            if ($iaFile->format == 'Android Package Archive') {
+                $iaSlug = basename($iaJsonFile, '.json');
+                $url = 'https://archive.org/download/' . $iaSlug . '/' . rawurlencode($iaFile->name);
+            }
+        }
+        if ($url === null) {
+            echo " No apk found!\n";
+            continue;
+        }
+
+        $release->url = $url;
+        $changed = true;
+    }
+
+    if ($changed) {
+        echo " Saving game data\n";
+        file_put_contents($gameFile, json_encode($gameData, JSON_PRETTY_PRINT) . "\n");
+    }
+}