123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- require_once 'PDO.class.php';
- require_once 'conf.class.php';
- require_once 'RedisClass.php';
- require_once 'KuaishouScrapyIpProxyService.php';
- /**
- * 更新优选商品剩余数量数据
- */
- #设置时区
- date_default_timezone_set('PRC');
- set_time_limit(0);
- ini_set('memory_limit', '1024M');
- $time = time();
- while (1) {
- echo "\n".'内存0:'.round(memory_get_usage()/1024/1024, 2).'MB';
- if(time() - $time > 3600){
- die();
- }
- try{
- getKwaiShowUpdateInfo();
- sleep(rand(1, 2));
- } catch(\Exception $e){
- echo "\n".'showup_error:'.$e->getMessage();
- continue;
- }
- }
- function getKwaiShowUpdateInfo() {
- $DB = new PDO_DB(conf::$DB_CONF);
- $sql='select user_id,id,live_stream_id from sc_kwai_show where enable = 1 and id % 10 = 0 order by updated_at asc limit 20';
- $kwaiUsers = $DB->select($sql);
- if (count($kwaiUsers) == 0){
- return;
- }
- echo "\n".'内存1:'.round(memory_get_usage()/1024/1024, 2).'MB';
- foreach ($kwaiUsers as $item) {
- $res = KuaishouScrapyIpProxyService::getStartPlayInfo($item['user_id']);
- echo "\n".'内存1-1:'.round(memory_get_usage()/1024/1024, 2).'MB';
- if ( isset($res['result']) && $res['result'] == 601) {
- //stopPlay($item['user_id'],$item['live_stream_id']);
- continue;
- }
- upShowData($item['user_id'], $res);
- echo "\n".'内存1-2:'.round(memory_get_usage()/1024/1024, 2).'MB';
- sleep(rand(0.8, 1.5));
- unset($item);
- unset($res);
- }
- echo "\n".'内存2:'.round(memory_get_usage()/1024/1024, 2).'MB';
-
- }
- function upShowData($uid, $res) {
- $DB = new PDO_DB(conf::$DB_CONF);
- $arr = array();
- $arr['live_stream_id'] = (int) isset($res['liveStreamId']) ? $res['liveStreamId'] : 0;
- $show_info = $DB->table('sc_kwai_show')->select('id, history, bast_display_watching_count, display_watching_count, start_time, live_stream_id')->where(['enable'=>1, 'live_stream_id'=>$res['liveStreamId']])->orderBy('id')->limit(1)->get();
- //$show_info = DB::table('sc_kwai_show')->select('id', 'history', 'bast_display_watching_count', 'display_watching_count', 'start_time', 'live_stream_id')->where('enable', '1')->where('live_stream_id', $res['liveStreamId'])->orderBy('id', 'asc')->limit(1)->get();
- if (!$show_info) {
- return TRUE;
- }
- // 将3.5W改成35000
- if (isset($res['displayLikeCount']) && strpos($res['displayLikeCount'], 'w') !== FALSE) {
- $res['displayLikeCount'] = (substr($res['displayLikeCount'], 0, strlen($res['displayLikeCount']) - 1)) * 10000;
- }
- if (isset($res['displayWatchingCount']) && strpos($res['displayWatchingCount'], 'w') !== FALSE) {
- $res['displayWatchingCount'] = (substr($res['displayWatchingCount'], 0, strlen($res['displayWatchingCount']) - 1)) * 10000;
- }
- // 点赞数量
- $arr['display_like_count'] = $res['displayLikeCount'];
- // 最高在线人数
- if ($res['displayWatchingCount'] > $show_info[0]['bast_display_watching_count']) {
- $arr['bast_display_watching_count'] = $res['displayWatchingCount'];
- }
- // 平均在线人数=((库中平均在线人数*访问次数)+抓取到的在线人数) / 历史次数
- $arr['display_watching_count'] = ceil(( ($show_info[0]['display_watching_count'] * $show_info[0]['history'] ) + $res['displayWatchingCount']) / ($show_info[0]['history']+ 1));
- $arr['history'] = $show_info[0]['history'] + 1;
- $arr['updated_at'] = date("Y-m-d H:i:s");
- $arr['start_time'] = $show_info[0]['start_time'];
- $arr['reward_count'] = getTop($arr['live_stream_id']);
- $ret = $DB->table('sc_kwai_show')->where(['live_stream_id'=>$arr['live_stream_id']])->update($arr);
- echo "\n更新:";
- var_dump($ret,$arr['live_stream_id']);
- unset($arr);
- unset($res);
- unset($show_info);
- return $ret;
- }
- // 打赏榜
- function getTop($showId) {
- $DB = new PDO_DB(conf::$DB_CONF);
- $res = KuaishouScrapyIpProxyService::topUsers($showId);
- if (!isset($res['topUsers'])) {
- return false;
- }
- $top = 0;
- try {
- foreach ($res['topUsers'] as $k => $v) {
- if ($k > 30) {
- break;
- }
- $params = array();
- $params['reward'] = (int) (isset($v['displayKsCoin']) ? $v['displayKsCoin'] : 0);
- $params['fan'] = (int) (isset($v['fansCount']) ? $v['fansCount'] : 0);
- $params['show_id'] = $showId;
- $params['kwai_user_id'] = isset($v['userInfo']['user_id']) ? $v['userInfo']['user_id'] : '';
- $params['user_name'] = isset($v['userInfo']['user_name']) ? $v['userInfo']['user_name'] : '';
- $params['head_url'] = isset($v['userInfo']['headurl']) ? $v['userInfo']['headurl'] : '';
- $params['enable'] = 1;
- $ret = $DB->table('sc_kwai_show_top')->insert($params);
- $top += $v['displayKsCoin'];
- unset($v);
- }
- } catch (\Exception $exc) {
- echo "\n".'userinfo showid:' . $showId, $exc->getMessage();
- }
- unset($res);
- return $top;
- }
|