百度推送站点信息以及日志记录

56 篇文章 0 订阅
12 篇文章 0 订阅
<?php
/**
 *
 * User: guo 
 * Date: 2019-08-20
 */


namespace console\controllers;


use backend\models\BaiduPushList;
use backend\models\BaiduPushLog;
use yii\console\Controller;

class BaiduPushController extends Controller
{
    //PC端地址
    public $pc_host;
    //M端地址
    public $m_host;
    //查询时间范围
    public $time;
    //M端推送地址
    public $push_mobile_url;
    //PC端推送地址
    public $push_pc_url;

    //数据类型
    const TYPE_ARTICLE = 1;//文章类型
    const TYPE_QUESTION = 2;//问答类型
    //推送类型
    const PUSH_TYPE_URL = 1;//URL推送类型
    //平台
    const PLATFORM_PC = 1;//pc平台
    const PLATFORM_MOBILE = 2;//Mobile平台
    //分页条数

    /**
     * 初始化
     * Author: guo 
     * Date: 2019-08-20
     */
    public function init()
    {
        parent::init();
        //配置完成后删除注释
        $this->pc_host = rtrim(\Yii::$app->params['domain']['pc'],'/');
        $this->m_host = rtrim(\Yii::$app->params['domain']['mobile'],'/');

        $this->time = strtotime('-7 days');

        $this->push_mobile_url = rtrim(\Yii::$app->params['baidu_cooperation']['push_mobile_url'],'/');
        $this->push_pc_url = rtrim(\Yii::$app->params['baidu_cooperation']['push_pc_url'],'/');

    }

    /**
     * 获取推送数据
     * Author: guo 
     * Date: 2019-08-20
     */
    public function actionGetData()
    {
        $page = 1;
        $pageSize = 1000;
        $time = $this->time;

        $this->getArticleData('article','文章推送数据', $time, $page, $pageSize);

        $this->getQuestionData('question','问答推送数据', $time, $page, $pageSize);
    }

    /**
     * 问答推送数据
     * Author: guo 
     * Date: 2019-08-20
     * @param $dataType
     * @param $dataName
     * @param $time
     * @param $page
     * @param $pageSize
     */
    public function getQuestionData($dataType, $dataName, $time, $page, $pageSize)
    {
        $baiduPushListModel = new BaiduPushList();
        $questionModel = new Question();
        do {
            $offset = ($page - 1) * $pageSize;
            $list = $questionModel::find()
                ->joinWith('reply')
                ->andWhere(['reply.status' => 2])
                ->andWhere(['>=', 'reply.checked_at', $time])
                ->orderBy('reply.checked_at DESC')
                ->offset($offset)
                ->limit($pageSize)
                ->asArray()
                ->all();

            if (empty($list)) {
                break;
            }

            //组合数据
            $data = [];
            foreach ($list as $item) {
                $tmp = [];
                $tmp['type'] = self::TYPE_QUESTION;
                $tmp['platform'] = self::PLATFORM_MOBILE;
                $tmp['url'] = sprintf($this->m_host . '/'.$dataType.'/%s.html', $item['hash_id']);
                $tmp['origin_id'] = $item['question_id'];
                $tmp['origin_hash_id'] = $item['hash_id'];
                $tmp['status'] = 0;
                $tmp['push_type'] = self::PUSH_TYPE_URL;
                $tmp['create_time'] = time();
                $data[] = $tmp;
                $tmp['url'] = sprintf($this->pc_host . '/'.$dataType.'/%s.html', $item['hash_id']);
                $tmp['platform'] = self::PLATFORM_PC;
                $data[] = $tmp;
                unset($tmp);
            }

            echo "[" . date('Y-m-d H:i:s') . "] 处理" . $dataName . "第{$page}页数据\n";

            $result = $baiduPushListModel->insertUniqueData($data);

            echo "插入 总数:{$result['totalCount']}, 成功:{$result['success']},失败:{$result['errors']}\n";

            $page++;
        } while (count($list));

        echo "[" . date('Y-m-d H:i:s') . "] 拉取" . $dataName . "完成!\n";
    }

    /**
     * 获取文章数据
     * Author: guo 
     * Date: 2019-08-20
     * @param $model
     * @param $dataName
     * @param $time
     * @param $page
     * @param $pageSize
     */
    public function getArticleData($dataType, $dataName, $time, $page, $pageSize)
    {
        $baiduPushListModel = new BaiduPushList();
        $articleModel = new Article();
        do {
            $offset = ($page - 1) * $pageSize;
            $list = $articleModel::find()->andWhere(['status' => 2])->andWhere(['>=', 'checked_at', $time])->orderBy('checked_at DESC')->offset($offset)->limit($pageSize)->asArray()->all();
            if (empty($list)) {
                break;
            }

            //组合数据
            $data = [];
            foreach ($list as $item) {
                $tmp = [];
                $tmp['type'] = self::TYPE_ARTICLE;
                $tmp['platform'] = self::PLATFORM_MOBILE;
                $tmp['url'] = sprintf($this->m_host . '/'.$dataType.'/%s.html', $item['hash_id']);
                $tmp['origin_id'] = $item['article_id'];
                $tmp['origin_hash_id'] = $item['hash_id'];
                $tmp['status'] = 0;
                $tmp['push_type'] = self::PUSH_TYPE_URL;
                $tmp['create_time'] = time();
                $data[] = $tmp;
                $tmp['url'] = sprintf($this->pc_host . '/'.$dataType.'/%s.html', $item['hash_id']);
                $tmp['platform'] = self::PLATFORM_PC;
                $data[] = $tmp;
                unset($tmp);
            }

            echo "[" . date('Y-m-d H:i:s') . "] 处理" . $dataName . "第{$page}页数据\n";

            $result = $baiduPushListModel->insertUniqueData($data);

            echo "插入 总数:{$result['totalCount']}, 成功:{$result['success']},失败:{$result['errors']}\n";

            $page++;
        } while (count($list));

        echo "[" . date('Y-m-d H:i:s') . "] 拉取" . $dataName . "完成!\n";
    }

    /**
     * 推送URL到百度
     * Author: guo 
     * Date: 2019-08-20
     */
    public function actionPushUrl()
    {
        //将以前推送失败的数据,改为未操作
        BaiduPushList::updateAll(['status' => 0], ['status' => -1]);

        $page = 1;
        $pageSize = 500;
        do {
            $offset = ($page - 1) * $pageSize;
            $list = BaiduPushList::find()->select(['id', 'origin_hash_id', 'platform', 'url'])->where(['status' => 0])->orderBy(['create_time' => SORT_DESC])->offset($offset)->limit($pageSize)->asArray()->all();
            if (empty($list)) {
                echo "[" . date('Y-m-d H:i:s') . "] 没有数据了\n";
                break;
            }


            //组合数据
            $pc_urls = [];
            $mobile_urls = [];
            $pc_ids = [];
            $mobile_ids = [];
            foreach ($list as $item) {
                if ($item['platform'] == 'pc') {
                    $pc_urls[] = $item['url'];
                    $pc_ids[] = $item['id'];
                } else {
                    $mobile_urls[] = $item['url'];
                    $mobile_ids[] = $item['id'];
                }
            }

            //推送pc url
            if (!empty($pc_urls)) {
                $pc_result = $this->pushBaiduUrl($pc_urls, 'pc');
                $this->pushDone(count($pc_ids), $pc_ids, $pc_result);
                echo "推送PC URL 百度返回结果:\n";
                print_r($pc_result);
            }

            //推送m url
            if (!empty($mobile_urls)) {
                $mobile_result = $this->pushBaiduUrl($mobile_urls, 'mobile');
                $this->pushDone(count($mobile_ids), $mobile_ids, $mobile_result);
                echo "推送Mobile URL 百度返回结果:\n";
                print_r($mobile_result);
            }


            echo "[" . date('Y-m-d H:i:s') . "] 处理第{$page}页\n";
            $page++;
        } while (count($list));

        echo "[" . date('Y-m-d H:i:s') . "] 处理完成!\n";
    }

    /**
     * 推送回调
     * Author: guo 
     * Date: 2019-08-20
     * @param $push_count
     * @param $ids
     * @param $result
     */
    private function pushDone($push_count, $ids, $result)
    {
        //保存推送日志信息
        $model = new BaiduPushLog();
        $model->setAttribute('push_type', self::PUSH_TYPE_URL);
        $model->setAttribute('push_time', time());
        $model->setAttribute('push_count', $push_count);

        if (isset($result['success'])) {
            $push_status = 1;
            $model->setAttribute('success', $result['success']);
            $model->setAttribute('remain', $result['remain']);
            if (isset($result['not_same_site']) && is_array($result['not_same_site'])) {
                $model->setAttribute('not_same_site', implode(',', $result['not_same_site']));
            }
            if (isset($result['not_valid']) && is_array($result['not_valid'])) {
                $model->setAttribute('not_valid', implode(',', $result['not_valid']));
            }
            $model->setAttribute('status', 1);
        } else {
            $push_status = -1;
            $model->setAttribute('status', 0);
            $model->setAttribute('message', $result['error'] . ' ' . $result['message']);
        }
        if ($model->save()) {
            $log_id = $model->log_id;

            //更新列表推送状态,推送时间
            BaiduPushList::updateAll(['push_time' => time(), 'status' => $push_status, 'log_id' => $log_id], ['id' => $ids]);
        } else {
            echo "[" . date('Y-m-d H:i:s') . "] 保存推送日志失败:\n";
            print_r($model->getFirstErrors());
        }

    }

    /**
     * 推送百度原创
     * Author: guo 
     * Date: 2019-08-20
     * @param $urls
     * @param string $platform
     * @return mixed
     */
    private function pushBaiduUrl($urls, $platform = 'mobile')
    {
        if ($platform == 'mobile') {
            $api = $this->push_mobile_url;
        } else {
            $api = $this->push_pc_url;
        }

        $ch = curl_init();
        $options = array(
            CURLOPT_URL => $api,
            CURLOPT_POST => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POSTFIELDS => implode("\n", $urls),
            CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
        );
        curl_setopt_array($ch, $options);
        $result = curl_exec($ch);

        return json_decode($result, true);

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值