TixController.php 2.22 KB
<?php

namespace app\commands;

use PHPHtmlParser\Dom;
use yii\db\Query;
use yii;
use yii\console\Controller;
use yii\db\Expression;
use yii\helpers\Console;
use app\components\Collection;
use app\models\Host;
use app\models\Filter;

/**
 * Парсинг запрещенных ресурсов в РФ.
 */
class TixController extends Controller
{
    public $count = 10;

    public function beforeAction($action)
    {
        if (!parent::beforeAction($action)) {
            return false;
        }
        return true;
    }

    public function options($actionID)
    {
        return ['count'];
    }

    public function optionAliases()
    {
        return ['c' => 'count'];
    }

    /**
     * Парсинг запрещенных ресурсов в РФ.
     */
    public function actionIndex()
    {
        $hosts = (new Query())
            ->select('id, domain')
            ->from('{{%host}}')
            ->where(
                [
                    'wis_status'=>Host::STATUS_WIS_FREE,
                    'tix_status'=>Host::STATUS_TIX_UNCHECK
                ]
            )
            ->orderBy(['status'=>SORT_DESC, 'rand()'=>SORT_ASC])
            ->limit($this->count)
            ->all();

        foreach ($hosts as $h)
            {
                $host = $h['domain'];
                $result = Collection::getTixRawData(Yii::$app->params['tix_url'].$host);
                if ($result['errorno'])
                    echo $result['errorno'] . ' ' . $result['errormessage'];
                else if ($result['data']['status']==='OK')
                    $this->upTix($result['data']['domain'], $result['data']['govalue']);
                else
                    echo '! '.$host.' - '.$result['data']['message'] .' ('.$result['data']['status'].')';
                unset($host);
                usleep (1000);
            }
        unset($h);
        unset($hosts);
    }

    private function upTix($domain, $value = 0)
    {
        echo $domain . ' => ' . $value . "\n";

        $update = ['tix'=>(int)$value, 'tix_status' => Host::STATUS_TIX_CHECK, 'tix_date' => new Expression('NOW()')];
        Yii::$app->db->createCommand()
            ->update('{{%host}}', $update, ['domain' => $domain])
            ->execute();
    }

}