123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- require_once dirname(dirname(__FILE__)).'/DB_PDO.class.php';
- //require_once 'TBK.class.php';
- require_once dirname(dirname(__FILE__)).'/conf.class.php';
- /**
- * 更新优选商品剩余数量数据
- */
- #设置时区
- date_default_timezone_set('PRC');
- ini_set('memory_limit','1024M');
- $plat_arr = array(
- 0 => 998, //抖音
- 1 => 993, //快手
- 2 => 58, //bilibili
- 3 => 94, //美拍
- 4 => 95, //秒拍
- 5 => 997, //西瓜视频
- 6 => 996, //火山小视频
- );
- $tag_arr = array(
- 0 => 0, //全部
- 1 => 30, //搞笑
- 2 => 29, //段子
- 3 => 73, //时尚
- 4 => 34, //美食
- 5 => 77, //旅游
- 6 => 40, //游戏
- 7 => 45, //娱乐
- 8 => 33, //音乐
- 9 => 50, //创意
- 10 => 41, //健身
- 11 => 308, //手工手绘
- 12 => 49, //萌娃
- 13 => 38, //宠物
- 14 => 35, //美妆
- 15 => 32, //舞蹈
- 16 => 44, //鬼畜
- 17 => 46, //影视
- 18 => 63, //科技
- 19 => 47, //小姐姐
- 20 => 48, //小哥哥
- 21 => 53, //民间艺人
- 22 => 78, //母婴
- 23 => 74, //汽车
- 24 => 31, //高颜值
- );
- $timer_arr = array(0,1,2,3);// 最多同步最近4期榜单
- /**
- * 同步
- *
- */
- $total = 0;
- $success = 0;
- foreach($plat_arr as $platform){
- foreach($tag_arr as $tag){
- foreach($timer_arr as $timer){
- $total++;
- $res = sync_data($platform, $tag, $timer, $total);
- if($res){
- $success++;
- }
- }
- }
- }
- echo "\n总共同步{$total}次,成功同步{$success}次";
- function sync_data($platform, $tag, $timer, $total){
- echo "\n\n/************* Start-{$total} **************/";
- echo "\nplatform:".$platform." tag:".$tag." timer:".$timer;
- $url = "https://api.data.caasdata.com/rank/lists?time=1556439520000&dataType=kol&tag={$tag}&timer={$timer}&page=1&timeType=week&platform={$platform}&count=50&data_counter=5";
- $result = curl_get($url);
- if(empty($result['data']['data'])){
- echo "\n没有数据!";
- echo "\n".'/*** End ***/';
- return false;
- }
- #第二页
- $url = "https://api.data.caasdata.com/rank/lists?time=1556439520000&dataType=kol&tag={$tag}&timer={$timer}&page=2&timeType=week&platform={$platform}&count=50&data_counter=5";
- $result2 = curl_get($url);
- if(empty($result2['data']['data'])){
- $_data = $result['data']['data'];
- }else{
- $_data = array_merge($result['data']['data'], $result2['data']['data']);
- }
-
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $total_num = 0;
- $ins_num = 0;
- $already = 0;
- foreach($_data as $k=>$v){
- $total_num++;
- if(!if_exists($v['id'])){
- $sql = format_sql($v, 'ks_users');
- }else{
- $already++;
- continue;
- }
- $_PDO->beginTransaction();
- try {
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- $res = $_PDO->lastinsertid();
- if($res){
- #插入详情
- $ins_info = ins_info($v['id']);
- $ins_num++;
- $_PDO->commit();
- }else{
- $_PDO->rollback();
- return false;
- }
- } catch(PDOException $e) {
- $_PDO->rollback();
- echo "\n异常:".$e->getMessage();
- return false;
- }
- }
- echo "\n总共".$total_num."条数据";
- echo "\n成功插入".$ins_num."条数据";
- echo "\n已存在".$already."条数据";
- echo "\n".'/*** End ***/';
- return true;
- }
- function curl_get($url){
-
- $header = array(
- 'Origin: https://www.caasdata.com',
- 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
- 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2FwaS5kYXRhLmNhYXNkYXRhLmNvbS91c2VycyIsImlhdCI6MTU1NjQzNjU2NywiZXhwIjoxNTU5MDI4NTY3LCJuYmYiOjE1NTY0MzY1NjcsImp0aSI6Ik45ekZrRFBadEVwT2pKSFgiLCJzdWIiOiI2OTY2NyIsIkxvZ2luVG9rZW4iOiJJakZlejFUV3lCRkJ3In0.DkA2GnIZKm-dSoi0mHGOnZ-F2-0d1XuS3E6Gr2HGXNw',
- );
- $ch = curl_init();//初始化curl
- curl_setopt($ch, CURLOPT_URL,$url);
- curl_setopt($ch, CURLOPT_HEADER, 0);//
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- //curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
- //curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
- $data = curl_exec($ch);//运行curl
- curl_close($ch);
- return json_decode($data,true);
- }
- function curl_post($url, $param){
- $auth_token = isset($param['auth_token'])? $param['auth_token'] : '';
- unset($param['auth_token']);
- $header = array('Content-Type:application/x-www-form-urlencoded;charset=utf-8','auth_token:'.$auth_token);
- $ch = curl_init();//初始化curl
- curl_setopt($ch, CURLOPT_URL,$url);
- curl_setopt($ch, CURLOPT_HEADER, 0);//
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
- curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
- $data = curl_exec($ch);//运行curl
- curl_close($ch);
- return json_decode($data,true);
- }
- function if_exists($ks_id){
- $sql = "select id from ks_users where ks_id = {$ks_id} limit 1";
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- $res = $stmt->fetch(PDO::FETCH_ASSOC);
- if($res){
- return true;
- }
- return false;
- }
- function format_sql($data, $table){
- if(empty($data)) return false;
- $columns = '';
- $values = '';
- foreach($data as $key=>$val){
- if($key == 'id'){
- $key = 'ks_id';
- }
- $columns .= '`'.$key.'`,';
- if(is_array($val)){
- $val = json_encode($val);
- }
- $values .= "'".$val."',";
- }
- $columns = trim($columns, ',');
- $values = trim($values,',');
- $sql = 'insert into '.$table.' ('.$columns.') values('.$values.')';
- return $sql;
- }
- function ins_info($ks_id){
- $url = "https://api.data.caasdata.com/channels/{$ks_id}/ta";
- $result = curl_get($url);
- //echo '<pre>';
- //print_r($result);exit;
- if(empty($result['data'])) return false;
- $_PDO=DB_PDO::getInstance( conf::$DB_CONF );
- $sql = format_sql($result['data'], 'ks_user_info');
- $stmt = $_PDO->prepare($sql);
- $stmt->execute();
- $res = $_PDO->lastinsertid();
- return $res;
- }
|