<commit_before><?php
/*
 * FIXME
 * XXX
 * TODO
 *
 * Need to use the standard Services_Providers_Http class
 *
 */
/**
 *
 * This class is used to find alternate download urls for nzb's.
 *
 */
class Services_Providers_HttpNzb {
    protected $spot = null;
    protected $alternateDownloadUrl = null;
    protected $nzb = null;
    protected $_cacheDao = null;

    public function __construct(array $spot, Dao_Cache $cacheDao) {
        $this->spot = $spot;
        $this->_cacheDao = $cacheDao;
    }

    public function hasNzb() {
        // Check if we can get an nzb.
        if ($this->getNzb()) {
            return true;
        }

        return false;
    }

    /**
     *
     * Returns nzb file in xml format.
     */
    public function getNzb() {
        if ($this->nzb) {
            // \O/ We already found an nzb before. Return the xml!
            return $this->nzb;
        } else if ($this->nzb === false) {
            // We already did a http request and this results in a badly formed xml.
            return null;
        }

        // Get the alternate url.
        $url = $this->getUrlForSpot();

        // If there is no alternate url return;
        if (!$url) {
            SpotDebug::msg(SpotDebug::DEBUG, __CLASS__ . '->getnzb(), failed to find url for spot');

            $this->nzb = false;
            return null;
        }

        SpotDebug::msg(SpotDebug::DEBUG, __CLASS__ . '->getnzb(), url: ' . $url);

        // Initialize http class.
        $svcHttp = new Services_Providers_Http($this->_cacheDao);
        $result = $svcHttp->perform($url);

        // Check if any error occured
        if ($result['successful']) {
            // Load the body into simplexml.
            // If the xml is well formed this will result in true thus returning the xml.
            // Suppress errors if the string is not well formed, where testing here.
            $body = $result['data'];
            if (@simplexml_load_string($body)) {
                SpotDebug::msg(SpotDebug::DEBUG, __CLASS__ . '->getnzb(), found nzb');

                $this->nzb = $body;
                return $this->nzb;
            } else if ($body) {
                SpotDebug::msg(SpotDebug::DEBUG, __CLASS__ . '->getnzb(), found body but not a valid XML');

                // we did not get a direct link to an nzb file.
                // more parsing is needed t(*_*t)
                $this->nzb = $this->downloadNzbFrom($url, $body);
                return $this->nzb;
            } else {
                $this->nzb = false;
            }
        } else {
            trigger_error($result['errorstr']);
        }

        // Return nothing we have a http error.
        return null;
    }

    /**
     *
     * Check if we have an url and return it if there is one.
     * @return String returns the url or null
     */
    function getUrlForSpot() {
        if (!$this->hasUrlForSpot()) {
            $this->nzb = false;
            return null;
        }

        return $this->alternateDownloadUrl;
    }

    /**
     *
     * Check for specific string to check if we have an alternate download url.
     */
    public function hasUrlForSpot() {
        if ($this->alternateDownloadUrl) {
            return true;
        }

        // Array containing url matches. Must contain the first part of the url.
        $matches = array(
            'http://base64.derefer.me',
            'http://derefer.me',
            'http://alturl.com',
            'http://tiny.cc',
            'http://bit.ly',
            'http://goo.gl',
            'http://hideref.org',
            'http://tiny.cc',
            'http://www.dereferer.org',
            'http://www.nzsearch.net'
        );

        // Search in the website url<commit_msg>Remove nzbsearch from the redir list as it is not a redirection provider<commit_after><?php
/*
 * FIXME
 * XXX
 * TODO
 *
 * Need to use the standard Services_Providers_Http class
 *
 */
/**
 *
 * This class is used to find alternate download urls for nzb's.
 *
 */
class Services_Providers_HttpNzb {
    protected $spot = null;
    protected $alternateDownloadUrl = null;
    protected $nzb = null;
    protected $_cacheDao = null;

    public function __construct(array $spot, Dao_Cache $cacheDao) {
        $this->spot = $spot;
        $this->_cacheDao = $cacheDao;
    }

    public function hasNzb() {
        // Check if we can get an nzb.
        if ($this->getNzb()) {
            return true;
        }

        return false;
    }

    /**
     *
     * Returns nzb file in xml format.
     */
    public function getNzb() {
        if ($this->nzb) {
            // \O/ We already found an nzb before. Return the xml!
            return $this->nzb;
        } else if ($this->nzb === false) {
            // We already did a http request and this results in a badly formed xml.
            return null;
        }

        // Get the alternate url.
        $url = $this->getUrlForSpot();

        // If there is no alternate url return;
        if (!$url) {
            SpotDebug::msg(SpotDebug::DEBUG, __CLASS__ . '->getnzb(), failed to find url for spot');

            $this->nzb = false;
            return null;
        }

        SpotDebug::msg(SpotDebug::DEBUG, __CLASS__ . '->getnzb(), url: ' . $url);

        // Initialize http class.
        $svcHttp = new Services_Providers_Http($this->_cacheDao);
        $result = $svcHttp->perform($url);

        // Check if any error occured
        if ($result['successful']) {
            // Load the body into simplexml.
            // If the xml is well formed this will result in true thus returning the xml.
            // Suppress errors if the string is not well formed, where testing here.
            $body = $result['data'];
            if (@simplexml_load_string($body)) {
                SpotDebug::msg(SpotDebug::DEBUG, __CLASS__ . '->getnzb(), found nzb');

                $this->nzb = $body;
                return $this->nzb;
            } else if ($body) {
                SpotDebug::msg(SpotDebug::DEBUG, __CLASS__ . '->getnzb(), found body but not a valid XML');

                // we did not get a direct link to an nzb file.
                // more parsing is needed t(*_*t)
                $this->nzb = $this->downloadNzbFrom($url, $body);
                return $this->nzb;
            } else {
                $this->nzb = false;
            }
        } else {
            trigger_error($result['errorstr']);
        }

        // Return nothing we have a http error.
        return null;
    }

    /**
     *
     * Check if we have an url and return it if there is one.
     * @return String returns the url or null
     */
    function getUrlForSpot() {
        if (!$this->hasUrlForSpot()) {
            $this->nzb = false;
            return null;
        }

        return $this->alternateDownloadUrl;
    }

    /**
     *
     * Check for specific string to check if we have an alternate download url.
     */
    public function hasUrlForSpot() {
        if ($this->alternateDownloadUrl) {
            return true;
        }

        // Array containing url matches. Must contain the first part of the url.
        $matches = array(
            'http://base64.derefer.me',
            'http://derefer.me',
            'http://alturl.com',
            'http://tiny.cc',
            'http://bit.ly',
            'http://goo.gl',
            'http://hideref.org',
            'http://tiny.cc',
            'http://www.dereferer.org',
        );

        // Search in the website url
{'fraction_non_alphanumeric': 0.12285828131225926, 'fraction_numerical': 0.0005312790543232833, 'mean_word_length': 1.596551724137931, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 3, 'xml': 14}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5896000', 'n_tokens_mistral': 2174, 'n_tokens_neox': 2112, 'n_words': 660}