<?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. Jboss JNDI ENC 数据源

    结合最近两天在网上看的各种帖子,昨天自己实际试了下,下面以问题的方式,对自己之前的困惑做个回答. 首先说一下,要用到的几个文件. 1)Jboss数据源配置文件,这儿是oracle数据源,数据源后缀名必 ...

  2. 常用js方法封装使用

    // 冒泡排序 export function bubbleSort(arr) { let i = arr.length - 1; while (i > 0) { let maxIndex = ...

  3. hive DDL操作

    1.创建数据库 CREATE DATABASE [IF NOT EXISTS] database_name [COMMENT database_comment] [LOCATION hdfs_path ...

  4. [常用类]Math、Random、System、BigInteger、BigDecimal

    Math类中的成员全是静态成员,构造方法是 私有的,以避免被创建对象 常用方法: int abs() double ceil() //向上取整 double floor() //向下取整 int ma ...

  5. jQuery 的attr()与css()的区别

    attr是attribute的缩写,意思是标签属性. css是,样式,意思是元素的style样式的 我的理解是: attr是操作元素的   属性 css是操作元素的  style属性 前者可以修改&l ...

  6. Java8 HashMap详解(转)

    Java8 对 HashMap 进行了一些修改,最大的不同就是利用了红黑树,所以其由 数组+链表+红黑树 组成. 根据 Java7 HashMap 的介绍,我们知道,查找的时候,根据 hash 值我们 ...

  7. vue观察模式浅析

    以下是我对vue观察者模式的理解:github L6zt 加入tip 2018-10-14 最近又看到<js设计模式设计>书 推荐去搂搂不要对框架的偏见, 你真的了解jquery.angu ...

  8. 关于 Python 程序的运行方面,有什么手段能提升性能?

    1.使用多进程,充分利用机器的多核性能2.对于性能影响较大的部分代码,可以使用 C 或 C++编写3.对于 IO 阻塞造成的性能影响,可以使用 IO 多路复用来解决4.尽量使用 Python 的内建函 ...

  9. Spark2.0基于广播变量broadcast实现实时数据按天统计

    package com.gm.hive.SparkHive; import java.text.SimpleDateFormat; import java.util.Arrays; import ja ...

  10. ASE Backend Alpha Sprint Review

    [Backend] Alpha Review展示博客 团队成员介绍:仅限于Alpha阶段有贡献的成员. 典型场景描述:描述并说明你们认为的产品面向的典型场景. 团队管理与协作:包括但不限于团队内部如何 ...