#!/usr/bin/env php $languages) { if (count($languages) === 0 || (count($languages) === 1 && empty($languages[0]))) continue; if ($name === 'Dan Brown (ssddanbrown)' || $name === 'Name') continue; $output .= $name . $reportDelimiter . implode('; ', $languages) . "\n"; } file_put_contents($reportLocation, $output); } 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']); if (isset($reportMap[$name])) { $languages = array_unique(array_merge($languages, $reportMap[$name])); } $reportMap[$name] = $languages; } } function loadExistingReportIntoMap($reportDelimiter, $reportLocation) { try { $reportData = file_get_contents($reportLocation); } catch (Exception $exception) { $reportData = ''; } $reportLines = explode("\n", $reportData); $reportMap = []; foreach ($reportLines as $reportLine) { if (empty($reportLine)) continue; [$name, $langs] = explode($reportDelimiter, $reportLine); $splitLangs = explode('; ', $langs); $reportMap[$name] = $splitLangs; } return $reportMap; } function exportTopMembersReport($key) { $result = makeMemberExportReport($key); $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/v2/projects/377219/reports'; $postData = [ 'name' => 'top-members', 'schema' => [ 'dateFrom' => '2019-10-01T00:00:00Z', 'dateTo' => date('Y-m-d') . 'T23:59:59Z', 'format' => 'csv', ], ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 15); curl_setopt($ch, CURLOPT_POST, true); 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; } function downloadMemberReport(string $exportHash, string $key) { $params = [ 'hash' => $exportHash, 'key' => $key ]; $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 file_get_contents($downloadUrl); } /** * Convert a comma separated string into an associated array. * @link http://gist.github.com/385876 (Modified) * @author Jay Williams (Modified) * @copyright Copyright (c) 2010, Jay Williams (Modified) * @license http://www.opensource.org/licenses/mit-license.php MIT License */ function csv_to_array(string $csvString): array { $header = null; $data = []; $lines = explode("\n", trim($csvString)); foreach ($lines as $line) { $csvLine = str_getcsv($line); if (!$header) { $header = $csvLine; } else { $data[] = array_combine($header, $csvLine); } } return $data; }