14days laravel
<?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的更多相关文章
- TODO:Laravel增加验证码
TODO:Laravel增加验证码1. 先聊聊验证码是什么,有什么作用?验证码(CAPTCHA)是"Completely Automated Public Turing test to te ...
- TODO:Laravel 内置简单登录
TODO:Laravel 内置简单登录 1. 激活Laravel的Auth系统Laravel 利用 PHP 的新特性 trait 内置了非常完善好用的简单用户登录注册功能,适合一些不需要复杂用户权限管 ...
- TODO:Laravel 使用blade标签布局页面
TODO:Laravel 使用blade标签布局页面 本文主要介绍Laravel的标签使用,统一布局页面.主要用到到标签有@yield,@ stack,@extends,@section,@stop, ...
- TODO:搭建Laravel VueJS SemanticUI
TODO:搭建Laravel VueJS SemanticUI Laravel是一套简洁.优雅的PHP开发框架(PHP Web Framework).可以让你从面条一样杂乱的代码中解脱出来:它可以帮你 ...
- Bringing Whoops Back to Laravel 5
You might be missing the "prettier" Whoops error handler from Laravel 4. If so, here's how ...
- 在 Laravel 中使用图片处理库 Integration/Image
系统需求 PHP >= 5.3 Fileinfo Extension GD Library (>=2.0) … or … Imagick PHP extension (>=6.5.7 ...
- Laravel Composer and ServiceProvider
Composer and: 创建自定义类库时,按命名空间把文件夹结构组织好 composer.json>autoload>classmap>psr-4 composer dump-a ...
- Laravel 5.x 请求的生命周期(附源码)
Laravel最早接触是刚开始实习的时候,那时通过网上的学习资料很快便上手,开发模块接口.后来没有什么深入和总结,但是当我刚开始学Laravel的时候,我对Laravel最大的认识就是,框架除了路由. ...
- Laravel 5.3 登录注册底层实现详解
每个控制器都使用 trait 来引入它们需要的方法 */ 用于处理用户登录认证 用于处理新用户注册 包含重置密码逻辑 用于处理重置密码邮件链接 认证需要的视图 包含了应用的基础布局文件 ...
随机推荐
- Angular5 错误: ngModel cannot be used to register form controls with a parent formGroup directive
在创建一个表单时,出现了这样的错误: 原因是,在最外层的form中使用了 formGroup 指令,但在下面的某个input 元素中,使用了ngModel 指令,但没有加入formControl 指令 ...
- [Python3] 007 列表的遍历,你是 for 联盟还是 while 部落
目录 少废话,直接上例子 for 联盟 for 与 list 的简单合作 for 的老搭档 range() for 与嵌套列表(双层列表) for 从 if 那儿认识的 else 循环"三杰 ...
- 15年6月8号 jsp内置对象总结
jsp的内置对象:主要有三个request.session.application:而且三者之间有各自不同的特点,在不同的情况下,使用不同的对象会有不同的效果, 其中: 1.request(特点):一 ...
- JavaBean简介和要求
JavaBean是一种Java语言写成的可重用组件. 所谓javaBean,是指符合如下标准的Java类: 类是公共的 有一个无参的公共的构造器 有属性,且有对应的get.set方法 用户可以使用Ja ...
- PC端实现浏览器点击分享到QQ好友,空间,微信,微博等
网上现在比较流行的是JIaThis,但是测试的时候,不能分享给QQ好友,一直卡在输入验证码,以下代码亲测有效,可直接使用 <%@ page language="java" c ...
- k3 cloud中获取年月日
日期类型字段元素.Date.Year(获取年) 日期类型字段元素.Date.Month(获取月)日期类型字段元素.Date.Day(获取天)
- Composer简介与下载安装
简介: 初次接触Composer的PHP程序员可能是需要下载ThinkPHP框架(5.1),那么什么是Composer,怎么下载安装呢? Composer是一个依赖管理工具,下载管理第三方包是其主要功 ...
- Spark2.0 Java实现将Hive运算结果保存到数据库
package com.gm.hive.SparkHive; import org.apache.spark.sql.Dataset; import org.apache.spark.sql.Row; ...
- 关于使用itext转Html为pdf添加css样式的问题
使用的jar文件 xmlworker-5.5.11.jar itextpdf-5.5.11.jar 下载地址:https://pan.baidu.com/s/1i5AIBvZ 以下为测试代码 pack ...
- 我是如何用python给Thunar写GUI插件的 (pygtk+glade)
更新:zip乱码的问题可以通过安装patch之后的p7zip-natspec和unzip-natspec解决(archlinuxcn源),而仍使用Engrampa做前端.此文重点在pygtk... 问 ...