]> BookStack Code Mirror - devops/blobdiff - meta-scripts/bookstack-update-translators
24.04 script, switched to php-fpm, force-started services
[devops] / meta-scripts / bookstack-update-translators
index 717b0cf4b95cba697d558f6291724546cc189330..435137e2431fb078fd4738c89883e61f48744eae 100755 (executable)
@@ -3,9 +3,9 @@
 
 // Script to fetch translators from crowdin via the API
 // and format into a BookStack attribution file.
-$key = getenv('CROWDIN_PROJECT_KEY');
+$key = getenv('CROWDIN_ACCESS_TOKEN');
 if (!$key) {
-    echo "Crowdin project key needs to be set on [CROWDIN_PROJECT_KEY] environment variable to run this script";
+    echo "A Crowdin access token with relevant report permissions needs to be set on [CROWDIN_ACCESS_TOKEN] environment variable to run this script";
     exit(0);
 }
 
@@ -19,11 +19,15 @@ if (!file_exists($reportLocation)) {
 
 $reportDelimiter = ' :: ';
 
+echo "Loading existing data...\n";
 $reportMap = loadExistingReportIntoMap($reportDelimiter, $reportLocation);
+echo "Exporting members from Crowdin...\n";
 $csvReport = exportTopMembersReport($key);
 $csvData = csv_to_array($csvReport);
+echo "Merging, formatting and writing report...\n";
 mergeCsvDataIntoReportMap($reportMap, $csvData, $reportDelimiter);
 formatAndWriteOutput($reportLocation, $reportMap, $reportDelimiter);
+echo "Done!\n";
 
 function formatAndWriteOutput(string $reportLocation, array $reportMap, string $reportDelimiter) {
     $output = "Name :: Languages\n";
@@ -38,6 +42,9 @@ function formatAndWriteOutput(string $reportLocation, array $reportMap, string $
 
 function mergeCsvDataIntoReportMap(array &$reportMap, array $csvData, string $reportDelimiter) {
     foreach ($csvData as $csvLine) {
+        if (intval($csvLine['Target Words']) == 0) {
+            continue;
+        }
         $name = $csvLine['Name'];
         $name = str_replace($reportDelimiter, '', $name);
         $languages = explode('; ', $csvLine['Languages']);
@@ -68,20 +75,24 @@ function loadExistingReportIntoMap($reportDelimiter, $reportLocation) {
 function exportTopMembersReport($key) {
     $result = makeMemberExportReport($key);
 
-    $exportHash = $result->hash;
+    $exportHash = $result->data->identifier;
+    echo "Waiting for Crowdin report to be generated...\n";
+    sleep(5);
+    echo "Downloading Crowdin report...\n";
     $csv = downloadMemberReport($exportHash, $key);
 
     return $csv;
 }
 
 function makeMemberExportReport(string $key) {
-    $url = 'https://api.crowdin.com/api/project/bookstack/reports/top-members/export';
+    $url = 'https://api.crowdin.com/api/v2/projects/377219/reports';
     $postData = [
-        'date_from' => '2019-10-01',
-        'date_to' => date('Y-m-d'),
-        'format' => 'csv',
-        'json' => true,
-        'key' => $key,
+        'name' => 'top-members',
+        'schema' => [
+            'dateFrom' => '2019-10-01T00:00:00Z',
+            'dateTo' => date('Y-m-d') . 'T23:59:59Z',
+            'format' => 'csv',
+        ],
     ];
 
     $ch = curl_init();
@@ -89,12 +100,23 @@ function makeMemberExportReport(string $key) {
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_TIMEOUT, 15);
     curl_setopt($ch, CURLOPT_POST, true);
-    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
+    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
+    curl_setopt($ch, CURLOPT_HTTPHEADER, [
+        'Content-Type: application/json',
+        'Authorization: Bearer ' . $key,
+    ]);
 
     $result = curl_exec($ch);
+    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+    $error = curl_error($ch);
+    if ($error) {
+        throw new Exception($error);
+    }
+
     curl_close($ch);
 
     $data = json_decode($result);
+
     return $data;
 }
 
@@ -103,17 +125,26 @@ function downloadMemberReport(string $exportHash, string $key) {
         'hash' => $exportHash,
         'key' => $key
     ];
-    $url = 'https://api.crowdin.com/api/project/bookstack/reports/top-members/download';
-    $url .= '?' . http_build_query($params);
+    $url = "https://api.crowdin.com/api/v2/projects/377219/reports/{$exportHash}/download";
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_TIMEOUT, 15);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+    curl_setopt($ch, CURLOPT_HTTPHEADER, [
+        'Content-Type: application/json',
+        'Authorization: Bearer ' . $key,
+    ]);
 
     $result = curl_exec($ch);
     curl_close($ch);
+    $data = json_decode($result);
+
+    $downloadUrl = $data->data->url ?? null;
+    if (!$downloadUrl) {
+        throw new Exception("Could not get report download URL. Download response data:\n" . $result);
+    }
 
-    return $result;
+    return file_get_contents($downloadUrl);
 }
 
 /**