<?php

namespace App\Console\Commands\Mining;

use App\Console\Commands\Core\BaseCommand;
use App\Repository\Core\ScriptStatusRepo;
use App\Repository\Mining\MoversTopKRepo;
use App\Repository\Mining\MoversTopKMinersRepo; use Illuminate\Support\Facades\DB;
use App\Core\Common\Helpers\MiningHelper; /**
* Listing分析脚本
* @auth ives
* @since 20170606 0.0.1
*/
class GetTopKAsins extends BaseCommand
{
/**
* The name and signature of the console command.
* DATA_CENTER_MANAGE_SYSTEM
* @var string
*/
protected $signature = 'movers {country}'; /**
* The console command description.
*
* @var string
*/
protected $description = '获取爬虫采集的飙升榜 参数:国家码'; /**
* @var
*/
public $country; /**
* @var
*/
public $createDatas; /**
* @var ScriptStatusRepo
*/
public $statHandler; /**
* @var 暂时略过的 listid>1000 的数据,后续分析
*/
public $listidThan1000Arr = []; /**
* Create a new command instance.
*
* @return void
*/ //當前爬蟲飆升榜最大主鍵
public $moversTopKRepoMaxId; public function __construct(
MoversTopKRepo $moversTopKRepo
, MoversTopKMinersRepo $moversTopKMinersRepo
)
{
parent::__construct();
$this->moversTopKRepo = $moversTopKRepo;
$this->moversTopKMinersRepo = $moversTopKMinersRepo;
} public function handle()
{
$this->info('start 获取爬虫采集的飙升榜'); $country = $this->argument('country');
$countrys = MiningHelper::getCountrys();
if (!in_array($country, $countrys)) {
$this->error($country . ' is not country code');
exit();
}
$this->country = $country;
//任务名称
//支持多国家同时运行
$this->statHandler = new ScriptStatusRepo('GetTopKAsins_' . $country . ''); //开启任务
$endPoint = $this->start(); //TODO 需结合脚本自身和爬虫脚本执行频率修改
//从panew.amazon_keyword_* 表中取 1000 条记录 分块处理
//一次選3000
$step = 3000;
$id_range = $this->moversTopKRepoMaxId - $step;
$db_getMoversTopK = $this->moversTopKRepo->getMoversTopK($id_range);
foreach ($db_getMoversTopK as $k => $asinArr) {
$this->mainFun($asinArr);
} //结束任务
$this->finish();
} public function start()
{
//任务已存在则不执行
if (!$this->statHandler->scriptExist()) {
$this->info('任务已存在则不执行script exist..');
exit();
}
$statHandler = $this->statHandler;
$endPoint = $statHandler->getEndPoint(); $this->info('上次執行后,分析至amazon_movers_' . $this->country . '的主鍵=' . $endPoint);
$maxId = $this->moversTopKRepo->getMoversTopKMaxId();
$maxId = $maxId[0]['maxId'];
$this->moversTopKRepoMaxId = $maxId;
if ($maxId <= $endPoint) {
$this->info('當前amazon_movers_' . $this->country . '最大主鍵--maxId=' . $maxId . '無新數據待分析');
exit();
} else {
$this->info('已經校驗當前amazon_movers_' . $this->country . '最大主鍵--maxId=' . $maxId . '---開始分析---'); } //开启
$statHandler->scriptStart($endPoint);
return $endPoint; } public function finish()
{
$statHandler = $this->statHandler;
$statHandler->freshEndPoint($this->moversTopKRepoMaxId);
$statHandler->scriptEnd(2);
$this->error('更新脚本狀態表endPoint=' . $this->moversTopKRepoMaxId . ':本次完成分許,正常結束--script finished!');
} public function mainFun($asinArr)
{ foreach ($asinArr as $k => $v) {
$$k = $v;
} //sales_rank_now > 1000 pass
if ($asinArr['sales_rank_now'] > 1000) {
$this->info('sales_rank_now=' . $asinArr['sales_rank_now'] . '---排名大于1000');
return;
} $if_existed = $this->moversTopKMinersRepo->chkAsinExisted($asinArr['asin']);
if ($if_existed) {
$this->info('miner_movers_shakers_' . $this->country . '中存在asin=' . $asinArr['asin'] . ':跳过');
return;
} else {
$this->info('miner_movers_shakers_' . $this->country . '中不存在asin=' . $asinArr['asin'] . ':继续'); } $data['asin'] = $asin;
$data['img'] = $pic_url;
$data['first_inrank'] = $add_time;
$data['first_rapidly'] = $sales_rank_ago;
$data['created_at'] = date('Y-m-d H:i:s');
$data['updated_at'] = date('Y-m-d');
$data['price'] = $price;
$data['bsr1'] = $sales_rank_now;
$data['bsr1path'] = $cate_id;
$data['reviews'] = $review_counts;
// 首次
$data['status'] = 1;
$data['is_top1000'] = 1;
$data['day_span'] = 14;
$data['top_count'] = 1;
$data['is_steady'] = 0; $data['end_date'] = date("Y-m-d", strtotime("+14 Days"));
$data_add_quote = array();
foreach ($data as $k => $v) {
$data_add_quote[$k] = '`' . $v . '`';
}
// $data = $data_add_quote;
$table = 'miner_movers_shakers_' . $this->country;
try { DB::connection('mining_db')
->table($table)
->insert($data);
$this->info('对' . $table . '新增asin=' . $asin);
} catch (Exception $exception) {
$this->info('db异常停止:' . $exception);
$this->statHandler->scriptEnd(3);
} //对listing采集表collect_listing_asins以asin+country为条件添加或者更新
$table_collect_listing_asins = 'collect_listing_asins';
try {
$data = array();
$data['asin'] = $asin;
$data['country'] = $this->country; $res = DB::connection('mining_db')
->table($table_collect_listing_asins)
->select('id', 'pass_at')
->where('country', '=', $this->country)
->where('asin', '=', $asin)
->where('status', '=', 1)
->orderBy('id', 'desc')
->limit(1)
->first();
$str_info = $table_collect_listing_asins . 'asin=' . $asin . '+country=' . $this->country . '+sataus=1--';
if (empty($res)) {
$this->info('查询' . $str_info . '无数据' . ':新增');
$data['is_fresh'] = 1;
$data['status'] = 1;
$data['updated_at'] = date('Y-m-d H:i:s');
$data['pass_at'] = date("Y-m-d", strtotime("+14 Days"));
//TODO
DB::connection('mining_db')
->table($table_collect_listing_asins)
->insert($data);
} else { $id = $res->id;
$pass_at = $res->pass_at; $this->info('查询' . $str_info . '有数据' . ':检查是否更新');
$diff = strtotime($pass_at) - strtotime("+14 Days");
$data_update = array();
$data_update['pass_at'] = date("Y-m-d", strtotime("+14 Days")); if ($diff < 0) {
$this->info('----需更新');
DB::connection('mining_db')
->table($table_collect_listing_asins)
->where('id', '=', $id)
->update($data_update);
} else {
$this->info('----不需更新');
}
} } catch (Exception $exception) {
$this->info('db异常停止:' . $exception);
$this->statHandler->scriptEnd(3);
} } }
                $diff = strtotime($pass_at) - strtotime("+14 Days");
$data_update = array();
$data_update['pass_at'] = date("Y-m-d", strtotime("+14 Days"));

14days laravel的更多相关文章

  1. TODO:Laravel增加验证码

    TODO:Laravel增加验证码1. 先聊聊验证码是什么,有什么作用?验证码(CAPTCHA)是"Completely Automated Public Turing test to te ...

  2. TODO:Laravel 内置简单登录

    TODO:Laravel 内置简单登录 1. 激活Laravel的Auth系统Laravel 利用 PHP 的新特性 trait 内置了非常完善好用的简单用户登录注册功能,适合一些不需要复杂用户权限管 ...

  3. TODO:Laravel 使用blade标签布局页面

    TODO:Laravel 使用blade标签布局页面 本文主要介绍Laravel的标签使用,统一布局页面.主要用到到标签有@yield,@ stack,@extends,@section,@stop, ...

  4. TODO:搭建Laravel VueJS SemanticUI

    TODO:搭建Laravel VueJS SemanticUI Laravel是一套简洁.优雅的PHP开发框架(PHP Web Framework).可以让你从面条一样杂乱的代码中解脱出来:它可以帮你 ...

  5. Bringing Whoops Back to Laravel 5

    You might be missing the "prettier" Whoops error handler from Laravel 4. If so, here's how ...

  6. 在 Laravel 中使用图片处理库 Integration/Image

    系统需求 PHP >= 5.3 Fileinfo Extension GD Library (>=2.0) … or … Imagick PHP extension (>=6.5.7 ...

  7. Laravel Composer and ServiceProvider

    Composer and: 创建自定义类库时,按命名空间把文件夹结构组织好 composer.json>autoload>classmap>psr-4 composer dump-a ...

  8. Laravel 5.x 请求的生命周期(附源码)

    Laravel最早接触是刚开始实习的时候,那时通过网上的学习资料很快便上手,开发模块接口.后来没有什么深入和总结,但是当我刚开始学Laravel的时候,我对Laravel最大的认识就是,框架除了路由. ...

  9. Laravel 5.3 登录注册底层实现详解

          每个控制器都使用 trait 来引入它们需要的方法 */ 用于处理用户登录认证 用于处理新用户注册 包含重置密码逻辑 用于处理重置密码邮件链接   认证需要的视图 包含了应用的基础布局文件 ...

随机推荐

  1. 1.关于狗书《Flask Web开发 基于Python的web开发应用实战》身份验证的改进

    在我学习用户身份验证的时候,我发现这里有个小弊端,在用户注册完成后想要验证邮箱的时候,点击邮箱中的网址进行验证,此时还要登陆,这及其不符合我们的习惯.一般情况下我们只需要点击网址就可以验证成功并且进入 ...

  2. Nginx负载均衡的max_fails和fail_timeout的默认配置问题

    今天发现一个奇怪的现象,前端请求后端服务多次后会超时一次,经过多次验证确定是大概10s左右就会超时一次,检查后端服务,发现其中一个节点已经夯死. 但是我们的nginx负载均衡策略是轮询机制,按照配置来 ...

  3. 知乎使用selenium反爬虫的解决方案

    from selenium.webdriver import Chrome from selenium.webdriver import ChromeOptions option = ChromeOp ...

  4. CSS-子盒子撑开父盒子,让父盒子的高随内容自适应

    方法一: height:auto!important; height:200px; min-height:200px; ie6并不支持min-height.ie7,opera,火狐没有问题. 方法二: ...

  5. ASP.NET MVC @html帮助类

    原文:https://www.cnblogs.com/caofangsheng/p/10462494.html HTML Helpers是用来创建HTML标签进而创建HTML控件的.HTML Help ...

  6. 灵活的理解JavaScript中的this指向(一)

    this是JavaScript中的关键字之一,在编写程序的时候经常会用到,正确的理解和使用关键字this尤为重要.首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确 ...

  7. 命令行运行python -m http.server报错

    最近在学习网站搭建,借助python搭建服务器时,在网站目录启动python服务时报错,如下: UnicodeDecodeError: 'utf-8' codec can't decode byte ...

  8. a标签实现下载canvas图片

    令 a 的 href = canvas.toDataURL("image/png");

  9. TensorFlow——实现线性回归算法

    import tensorflow as tf import numpy as np import matplotlib.pyplot as plt #使用numpy生成200个随机点 x_data= ...

  10. python基础--3 列表

    #list类#li是list类的一个对象li=[11,22,33,22,44] #参数#在原来值最后进行整个作为元素追加 # li.append((11,22,33))#对列表本身进行操作,appen ...