<commit_before><?php
/**
 * This file is part of the Passwords App
 * created by <NAME>
 * and licensed under the AGPL.
 */

namespace OCA\Passwords\Helper\Preview;

use OCA\Passwords\Exception\ApiException;
use OCA\Passwords\Services\HelperService;
use OCA\Passwords\Services\WebsitePreviewService;

/**
 * Class BrowshotPreviewHelper
 *
 * @package OCA\Passwords\Helper\Preview
 */
class BrowshotPreviewHelper extends AbstractPreviewHelper {

    const BWS_API_CONFIG_KEY = 'service/preview/bws/key';

    /**
     * @var string
     */
    protected $prefix = HelperService::PREVIEW_BROW_SHOT;

    /**
     * @param string $domain
     * @param string $view
     *
     * @return string
     * @throws ApiException
     */
    protected function getPreviewUrl(string $domain, string $view): string {
        $apiKey    = $this->config->getAppValue(self::BWS_API_CONFIG_KEY);
        $createUrl = $this->getCreateUrl($apiKey, $domain, $view);

        $request  = parent::getHttpRequest($createUrl);
        $response = $request->send();

        if($response === null) {
            $status = $request->getInfo('http_code');
            $this->logger->error("Browshot Request Failed, HTTP {$status}");
            throw new ApiException('API Request Failed', 502);
        }

        $data = json_decode($response);
        return $this->waitForResult($data, $apiKey);
    }

    /**
     * @param \stdClass $data
     * @param string    $apiKey
     *
     * @return string
     * @throws ApiException
     */
    protected function waitForResult(\stdClass $data, string $apiKey): string {
        $infoUrl = $this->getInfoUrl($data->id, $apiKey);

        while(in_array($data->status, ['in_queue', 'in_process', 'processing'])) {
            sleep(1);
            $request  = parent::getHttpRequest($infoUrl);
            $response = $request->send();

            if($response === null) {
                $status = $request->getInfo('http_code');
                $this->logger->error("Browshot Request Failed, HTTP {$status}");
                throw new ApiException('API Request Failed', 502);
            }

            $data = json_decode($response);
        }

        if($data->status === 'error') {
            $this->logger->error("Browshot Request Failed, Error {$data->error}");
            throw new ApiException('API Request Failed', 502);
        }

        return $data->screenshot_url;
    }

    /**
     * @param string $apiKey
     * @param string $domain
     * @param string $view
     *
     * @return string
     */
    protected function getCreateUrl(string $apiKey, string $domain, string $view): string {
        if($view === WebsitePreviewService::VIEWPORT_DESKTOP) {
            return "https://api.browshot.com/api/v1/screenshot/create?key={$apiKey}&url={$domain}&instance_id=27&size=page";
        }

        return "https://api.browshot.com/api/v1/screenshot/create?key={$apiKey}&url={$domain}&instance_id=67&size=page";
    }

    /**
     * @param string $id
     * @param string $apiKey
     *<commit_msg>Make instance configurable<commit_after><?php
/**
 * This file is part of the Passwords App
 * created by <NAME>
 * and licensed under the AGPL.
 */

namespace OCA\Passwords\Helper\Preview;

use OCA\Passwords\Exception\ApiException;
use OCA\Passwords\Services\HelperService;
use OCA\Passwords\Services\WebsitePreviewService;

/**
 * Class BrowshotPreviewHelper
 *
 * @package OCA\Passwords\Helper\Preview
 */
class BrowshotPreviewHelper extends AbstractPreviewHelper {

    const BWS_API_CONFIG_KEY = 'service/preview/bws/key';
    const BWS_MOBILE_INSTANCE = 'service/preview/bws/mobile';
    const BWS_DESKTOP_INSTANCE = 'service/preview/bws/desktop';

    /**
     * @var string
     */
    protected $prefix = HelperService::PREVIEW_BROW_SHOT;

    /**
     * @param string $domain
     * @param string $view
     *
     * @return string
     * @throws ApiException
     */
    protected function getPreviewUrl(string $domain, string $view): string {
        $apiKey    = $this->config->getAppValue(self::BWS_API_CONFIG_KEY);
        $createUrl = $this->getCreateUrl($apiKey, $domain, $view);

        $request  = parent::getHttpRequest($createUrl);
        $response = $request->send();

        if($response === null) {
            $status = $request->getInfo('http_code');
            $this->logger->error("Browshot Request Failed, HTTP {$status}");
            throw new ApiException('API Request Failed', 502);
        }

        $data = json_decode($response);
        return $this->waitForResult($data, $apiKey);
    }

    /**
     * @param \stdClass $data
     * @param string    $apiKey
     *
     * @return string
     * @throws ApiException
     */
    protected function waitForResult(\stdClass $data, string $apiKey): string {
        $infoUrl = $this->getInfoUrl($data->id, $apiKey);

        while(in_array($data->status, ['in_queue', 'in_process', 'processing'])) {
            sleep(1);
            $request  = parent::getHttpRequest($infoUrl);
            $response = $request->send();

            if($response === null) {
                $status = $request->getInfo('http_code');
                $this->logger->error("Browshot Request Failed, HTTP {$status}");
                throw new ApiException('API Request Failed', 502);
            }

            $data = json_decode($response);
        }

        if($data->status === 'error') {
            $this->logger->error("Browshot Request Failed, Error {$data->error}");
            throw new ApiException('API Request Failed', 502);
        }

        return $data->screenshot_url;
    }

    /**
     * @param string $apiKey
     * @param string $domain
     * @param string $view
     *
     * @return string
     */
    protected function getCreateUrl(string $apiKey, string $domain, string $view): string {

        $extra = '';
        $instance = $this->config->getAppValue(self::BWS_MOBILE_INSTANCE, '67');
        if($view === WebsitePreviewService::VIEWPORT_DESKTOP) {
            $instance = $this->config->getAppValue(self::BWS_DESKTOP_INSTANCE, '27');
            $extra = '&screen_width=1600&screen_height=1200';
        }

        return "https://api.browshot.com/api/v1/screenshot/create?key={$apiKey}&url={$domain}&instance_id={$instance}&size=page{$extra}";
    }

    /**
     * @param string $id
     * @param string $apiKey
     *
{'fraction_non_alphanumeric': 0.14438166980539863, 'fraction_numerical': 0.006120527306967985, 'mean_word_length': 2.61281179138322, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 49, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '128009', 'n_tokens_mistral': 1939, 'n_tokens_neox': 1871, 'n_words': 377}