优惠券订单及其他脚本

show_update.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. require_once 'PDO.class.php';
  3. require_once 'conf.class.php';
  4. require_once 'RedisClass.php';
  5. require_once 'KuaishouScrapyIpProxyService.php';
  6. /**
  7. * 更新优选商品剩余数量数据
  8. */
  9. #设置时区
  10. date_default_timezone_set('PRC');
  11. set_time_limit(0);
  12. ini_set('memory_limit', '1024M');
  13. $time = time();
  14. while (1) {
  15. echo "\n".'内存0:'.round(memory_get_usage()/1024/1024, 2).'MB';
  16. if(time() - $time > 3600){
  17. die();
  18. }
  19. try{
  20. getKwaiShowUpdateInfo();
  21. sleep(rand(1, 2));
  22. } catch(\Exception $e){
  23. echo "\n".'showup_error:'.$e->getMessage();
  24. continue;
  25. }
  26. }
  27. function getKwaiShowUpdateInfo() {
  28. $DB = new PDO_DB(conf::$DB_CONF);
  29. $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';
  30. $kwaiUsers = $DB->select($sql);
  31. if (count($kwaiUsers) == 0){
  32. return;
  33. }
  34. echo "\n".'内存1:'.round(memory_get_usage()/1024/1024, 2).'MB';
  35. foreach ($kwaiUsers as $item) {
  36. $res = KuaishouScrapyIpProxyService::getStartPlayInfo($item['user_id']);
  37. echo "\n".'内存1-1:'.round(memory_get_usage()/1024/1024, 2).'MB';
  38. if ( isset($res['result']) && $res['result'] == 601) {
  39. //stopPlay($item['user_id'],$item['live_stream_id']);
  40. continue;
  41. }
  42. upShowData($item['user_id'], $res);
  43. echo "\n".'内存1-2:'.round(memory_get_usage()/1024/1024, 2).'MB';
  44. sleep(rand(0.8, 1.5));
  45. unset($item);
  46. unset($res);
  47. }
  48. echo "\n".'内存2:'.round(memory_get_usage()/1024/1024, 2).'MB';
  49. }
  50. function upShowData($uid, $res) {
  51. $DB = new PDO_DB(conf::$DB_CONF);
  52. $arr = array();
  53. $arr['live_stream_id'] = (int) isset($res['liveStreamId']) ? $res['liveStreamId'] : 0;
  54. $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();
  55. //$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();
  56. if (!$show_info) {
  57. return TRUE;
  58. }
  59. // 将3.5W改成35000
  60. if (isset($res['displayLikeCount']) && strpos($res['displayLikeCount'], 'w') !== FALSE) {
  61. $res['displayLikeCount'] = (substr($res['displayLikeCount'], 0, strlen($res['displayLikeCount']) - 1)) * 10000;
  62. }
  63. if (isset($res['displayWatchingCount']) && strpos($res['displayWatchingCount'], 'w') !== FALSE) {
  64. $res['displayWatchingCount'] = (substr($res['displayWatchingCount'], 0, strlen($res['displayWatchingCount']) - 1)) * 10000;
  65. }
  66. // 点赞数量
  67. $arr['display_like_count'] = $res['displayLikeCount'];
  68. // 最高在线人数
  69. if ($res['displayWatchingCount'] > $show_info[0]['bast_display_watching_count']) {
  70. $arr['bast_display_watching_count'] = $res['displayWatchingCount'];
  71. }
  72. // 平均在线人数=((库中平均在线人数*访问次数)+抓取到的在线人数) / 历史次数
  73. $arr['display_watching_count'] = ceil(( ($show_info[0]['display_watching_count'] * $show_info[0]['history'] ) + $res['displayWatchingCount']) / ($show_info[0]['history']+ 1));
  74. $arr['history'] = $show_info[0]['history'] + 1;
  75. $arr['updated_at'] = date("Y-m-d H:i:s");
  76. $arr['start_time'] = $show_info[0]['start_time'];
  77. $arr['reward_count'] = getTop($arr['live_stream_id']);
  78. $ret = $DB->table('sc_kwai_show')->where(['live_stream_id'=>$arr['live_stream_id']])->update($arr);
  79. echo "\n更新:";
  80. var_dump($ret,$arr['live_stream_id']);
  81. unset($arr);
  82. unset($res);
  83. unset($show_info);
  84. return $ret;
  85. }
  86. // 打赏榜
  87. function getTop($showId) {
  88. $DB = new PDO_DB(conf::$DB_CONF);
  89. $res = KuaishouScrapyIpProxyService::topUsers($showId);
  90. if (!isset($res['topUsers'])) {
  91. return false;
  92. }
  93. $top = 0;
  94. try {
  95. foreach ($res['topUsers'] as $k => $v) {
  96. if ($k > 30) {
  97. break;
  98. }
  99. $params = array();
  100. $params['reward'] = (int) (isset($v['displayKsCoin']) ? $v['displayKsCoin'] : 0);
  101. $params['fan'] = (int) (isset($v['fansCount']) ? $v['fansCount'] : 0);
  102. $params['show_id'] = $showId;
  103. $params['kwai_user_id'] = isset($v['userInfo']['user_id']) ? $v['userInfo']['user_id'] : '';
  104. $params['user_name'] = isset($v['userInfo']['user_name']) ? $v['userInfo']['user_name'] : '';
  105. $params['head_url'] = isset($v['userInfo']['headurl']) ? $v['userInfo']['headurl'] : '';
  106. $params['enable'] = 1;
  107. $ret = $DB->table('sc_kwai_show_top')->insert($params);
  108. $top += $v['displayKsCoin'];
  109. unset($v);
  110. }
  111. } catch (\Exception $exc) {
  112. echo "\n".'userinfo showid:' . $showId, $exc->getMessage();
  113. }
  114. unset($res);
  115. return $top;
  116. }