@param string $region
@param int    $gameId
@param int    $keyframeId

@return bool|string
549227
public function downloadKeyframe($region, $gameId, $keyframeId)
    {
        try {
            $keyframe = $this->buzz->get(sprintf(
                $this->getUrl($region) . self::URL_KEYFRAME_DOWNLOAD,
                $region,
                $gameId,
                $keyframeId
            ));
        } catch (TimeoutException $e) {
            return false;
        } catch (RequestException $e) {
            return false;
        }

        // Check if game exists
        $header = $keyframe->getHeaders()[0];
        if ('HTTP/1.1 500 Internal Server Error' == $header || 'HTTP/1.1 404 Not Found' == $header) {
            return false;
        }

        return $keyframe->getContent();
    }
845619
public String getNextPageUrl(String domain, String region) {
        if (nextPageUrl != null) {
            return nextPageUrl;
        }

        return urlFromUri(domain, region, nextPageUri);
    }
920902
public function getEndOfGameStatsPath($region, $gameId)
    {
        $endStatsPath = null;

        try {
            $endStatsPath = $this->observerClient->getEndStats($region, $gameId);
        } catch (ReplayEndStatsNotFoundException $e) {
            // Log
            $this->log($gameId, sprintf('GET /endOfGameStats/%s/%s/null | ERROR: the endstats is not found', $region, $gameId));

            throw $e;
        }

        // Log
        $this->log($gameId, sprintf('GET /endOfGameStats/%s/%s/null', $region, $gameId));

        return $endStatsPath;
    }
430221
public function downloadEndStats($region, $gameId)
    {
        try {
            $endStats = $this->buzz->get(sprintf($this->getUrl($region) . self::URL_ENDSTATS, $region, $gameId));
        } catch (TimeoutException $e) {
            return false;
        } catch (RequestException $e) {
            return false;
        }

        // base64_decode to decode endStats file

        return $endStats->getContent();
    }
842134
public function getShardByRegion($region)
	{
		foreach($this->info as $shard) {
			if($shard->slug == $region)
				return $shard;
		}
		return null;
	}
115147
public RefreshResponse refresh(String region) throws ApiException {
        ApiResponse<RefreshResponse> resp = refreshWithHttpInfo(region);
        return resp.getData();
    }
882051
private function buildQuery(string $url, string $locale = null, string $region = null): string
    {
        if (null !== $locale) {
            $url = sprintf('%s&language=%s', $url, $locale);
        }

        if (null !== $region) {
            $url = sprintf('%s&region=%s', $url, $region);
        }

        if (null !== $this->apiKey) {
            $url = sprintf('%s&key=%s', $url, $this->apiKey);
        }

        if (null !== $this->clientId) {
            $url = sprintf('%s&client=%s', $url, $this->clientId);

            if (null !== $this->channel) {
                $url = sprintf('%s&channel=%s', $url, $this->channel);
            }

            if (null !== $this->privateKey) {
                $url = $this->signQuery($url);
            }
        }

        return $url;
    }
872529
public function fetch_achievements($gamertag, $gameid, $region)
    {
        $gamertag = trim($gamertag);
        $url      = 'https://live.xbox.com/' . $region . '/Activity/Details?titleId=' . urlencode($gameid) . '&compareTo=' . urlencode($gamertag);
        $key      = $this->version . ':achievements.' . $gamertag . '.' . $gameid;

        $data = $this->__cache->fetch($key);
        if (!$data) {
            $data      = $this->fetch_url($url);
            $freshness = 'new';
            $this->__cache->store($key, $data, 3600);
        } else {
            $freshness = 'from cache';
        }

        $json = $this->find($data, 'broker.publish(routes.activity.details.load, ', ');');
        $json = json_decode($json, true);

        if (!empty($json)) {
            $achievements                           = array();
            $achievements['gamertag']               = $g = $json['Players'][0]['Gamertag'];
            $achievements['game']                   = $this->clean($json['Game']['Name']);
            $achievements['id']                     = $json['Game']['Id'];
            $achievements['hid']                    = dechex($json['Game']['Id']);
            $achievements['gamerscore']['current']  = $json['Game']['Progress'][$g]['Score'];
            $achievements['gamerscore']['total']    = $json['Game']['PossibleScore'];
            $achievements['achievement']['current'] = $json['Game']['Progress'][$g]['Achievements'];
            $achievements['achievement']['total']   = $json['Game']['PossibleAchievements'];
            $achievements['progress']               = $json['Players'][0]['PercentComplete'];
            $achievements['lastplayed']             = (int)substr($json['Game']['Progress'][$g]['LastPlayed'], 6, 10);

            $i = 0;
            foreach ($json['Achievements'] as $achievement) {
                $achievements['achievements'][$i]['id']                  = $achievement['Id'];
                $achievements['achievements'][$i]['title']               = '';
                $achievements['achievements'][$i]['artwork']['locked']   = 'https://live.xbox.com/Content/Images/HiddenAchievement.png';
                $achievements['achievements'][$i]['artwork']['unlocked'] = 'https://live.xbox.com/Content/Images/HiddenAchievement.png';
                $achievements['achievements'][$i]['title']               = 'Secret Achievement';
                $achievements['achievements'][$i]['description']         = 'This is a secret achievement. Unlock it to find out more.';
                $achievements['achievements'][$i]['gamerscore']          = 0;
                $achievements['achievements'][$i]['secret']              = (bool)$achievement['IsHidden'];
                $achievements['achievements'][$i]['unlocked']            = false;
                $achievements['achievements'][$i]['unlockdate']          = null;
                $achievements['achievements'][$i]['unlockedoffline']     = null;

                if (!$achievement['IsHidden']) {
                    // find colored achievement tile
                    // get image name. removes url and extension
                    $ach_info  = pathinfo($achievement['TileUrl']);
                    // decode image name
                    $ach_str1  = base64_decode($ach_info['filename']);
                    // remove everything before /ach
                    $ach_str2  = strstr($ach_str1, '/ach');
                    // subtract last 12 spaces and/or whitespace from string
                    $ach_str3  = substr($ach_str2, 0, -11);
                    // create color tile source
                    $ach_color = 'https://image-ssl.xboxlive.com/global/t.' . dechex($json['Game']['Id']) . $ach_str3;

                    $achievements['achievements'][$i]['artwork']['locked']   = $achievement['TileUrl'];
                    $achievements['achievements'][$i]['artwork']['unlocked'] = $ach_color;
                }

                if (!empty($achievement['Name'])) {
                    $achievements['achievements'][$i]['title'] = $this->clean($achievement['Name']);
                }

                if (!empty($achievement['Description'])) {
                    $achievements['achievements'][$i]['description'] = $this->clean($achievement['Description']);
                }

                if (!empty($achievement['Score'])) {
                    $achievements['achievements'][$i]['gamerscore'] = $achievement['Score'];
                }

                if (!empty($achievement['EarnDates'][$g]['EarnedOn'])) {
                    $achievements['achievements'][$i]['unlocked']        = true;
                    $achievements['achievements'][$i]['unlockdate']      = (int)substr($achievement['EarnDates'][$g]['EarnedOn'], 6, 10);
                    $achievements['achievements'][$i]['unlockedoffline'] = (bool)$achievement['EarnDates'][$g]['IsOffline'];
                }

                $i++;
            }

            $achievements['freshness'] = $freshness;
            return $achievements;
        } elseif (stripos($data, 'errorCode: \'404\'')) {
            $this->error = 502;
            return false;
        } else {
            $this->error = 500;
            $this->__cache->remove($key);
            $this->force_new_login();
            return false;
        }
    }
676117
public function setRegion($region)
	{
		if ( ! $region instanceof Region)
		{
			$region = new Region($region);
		}
		$this->region = $region;

		return $this;
	}
1142679
public function countInRegion($region)
    {
        $query = $this->_em->createQuery('
            SELECT COUNT(n.id)
            FROM CMSBundle:Node AS n
            JOIN CMSBundle:Region AS r
            WHERE r.id = :region
            AND n.region = r
        ')->setParameter('region', $region);

        return $query->getSingleScalarResult();
    }
222325
public function getKeyframePath($region, $gameId, $keyframeId)
    {
        $keyframePath = null;

        try {
            $keyframePath = $this->observerClient->getKeyframePath($region, $gameId, $keyframeId);
        } catch (ReplayKeyframeNotFoundException $e) {
            // Log
            $this->log($gameId, sprintf('GET /getKeyFrame/%s/%s/%d/token | ERROR: the keyframe is not found', $region, $gameId, $keyframeId));

            throw $e;
        }

        // Log
        $this->log($gameId, sprintf('GET /getKeyFrame/%s/%s/%d/token', $region, $gameId, $keyframeId));

        return $keyframePath;
    }
453378
public function createDirs($region, $gameId)
    {
        // Check if exists
        $replayDirPath = $this->getReplayDirPath($region, $gameId);
        if (is_dir($replayDirPath)) {
            throw new ReplayFolderAlreadyExistsException('The replay #' . $gameId . ' already exists');
        }

        // Create dirs for replay
        $permissions = 0755;
        mkdir($replayDirPath . '/chunks', $permissions, true);
        mkdir($replayDirPath . '/keyframes', $permissions, true);

        // Save decrypted file only in dev
        if ($this->options['replay.decoder.enable'] && $this->options['replay.decoder.save_files']) {
            mkdir($replayDirPath . '/keyframes.decoded', $permissions, true);
            mkdir($replayDirPath . '/chunks.decoded', $permissions, true);
        }
    }
1171090
public function getMetas($region, $gameId)
    {
        // Check if exists, if no disable it
        if (!is_dir($this->getReplayDirPath($region, $gameId))) {
            throw new ReplayFolderNotFoundException(
                'The replay folder #' . $gameId . ' is not found. The replay will be disabled'
            );
        }

        return json_decode(file_get_contents($this->getReplayDirPath($region, $gameId) . '/metas.json'), true);
    }
1099128
public function getEndStats($region, $gameId)
    {
        $filePath = $this->getReplayDirPath($region, $gameId) . '/endstats';

        if (!is_file($filePath)) {
            throw new ReplayEndStatsNotFoundException('The endstats for game #' . $gameId . ' is not found');
        }

        return $filePath;
    }
79636
protected function doDownload($region, $gameId, $encryptionKey, OutputInterface $output = null)
    {
        $hasOutput = null != $output;
        $replay = $this->createReplay($region, $gameId, $encryptionKey);

        // Download metas
        $this->downloadMetas($replay, $output);

        // Validate game criterias based on metas
        $this->isValid($replay, $output);

        // Retrieve last infos to download previous files
        if ($hasOutput) {
            $output->write("Retrieve last infos...\t\t\t");
        }

        $lastChunkInfo = $this->getLastChunkInfos($replay);
        $replay->setLastChunkId($lastChunkInfo['chunkId']);
        $replay->setLastKeyframeId($lastChunkInfo['keyFrameId']);

        if ($hasOutput) {
            $output->writeln('<info>OK</info>');
        }

        // Download previous chunks
        if ($hasOutput) {
            $output->write("Download all previous chunks (" . $replay->getLastChunkId() . ")...\t");
        }

        $this->downloadChunks($replay);

        if ($hasOutput) {
            $output->writeln('<info>OK</info>');
        }

        // Download previous keyframes
        if ($hasOutput) {
            $output->write("Download all previous keyframes (" . $replay->getLastKeyframeId() . ")...\t");
        }

        $this->downloadKeyframes($replay);

        if ($hasOutput) {
            $output->writeln(array('<info>OK</info>', ''));
        }

        // Download current chunks & keyframes
        if ($hasOutput) {
            $output->writeln("Download current game data :");
        }

        $this->downloadCurrentData($replay, $output);

        if ($hasOutput) {
            $output->writeln('');
        }

        // Update metas
        if ($hasOutput) {
            $output->write("Update metas...\t\t\t\t");
        }

        $this->updateMetas($replay);

        if ($hasOutput) {
            $output->writeln('<info>OK</info>');
        }

        return $replay;
    }
760813
public function isGameExists($region, $gameId)
    {
        $metas = $this->buzz->get(sprintf($this->getUrl($region) . self::URL_META, $region, $gameId));

        // Check if game exists
        if ('HTTP/1.1 500 Internal Server Error' == $metas->getHeaders()[0]) {
            return false;
        }

        return true;
    }
137517
public function isRegionInEditMode($region)
    {
        if ($site = $this->siteManager->getContext()) {
            return
                ($this->getPageContext()->isTemporary() && $this->isPageContextRegion($region, $site->theme)) ||
                ($this->getSiteContext()->isTemporary() && $this->isTransversalContextRegion($region, $site->theme))
            ;
        }
        return false;
    }