liqian %!s(int64=3) %!d(string=hai) anos
pai
achega
87751c1570
Modificáronse 4 ficheiros con 111 adicións e 1 borrados
  1. 29 1
      config.py
  2. 61 0
      pool_predict.py
  3. 6 0
      pool_train.py
  4. 15 0
      utils.py

+ 29 - 1
config.py

@@ -1,4 +1,8 @@
 class BaseConfig(object):
+    # 产品标识
+    APP_TYPE = {
+        'VLOG': 0,
+    }
     # 数据存放路径
     DATA_DIR_PATH = './data'
 
@@ -26,5 +30,29 @@ class BaseConfig(object):
     MODEL_FILENAME = 'model.pickle'
 
 
+class TestConfig(BaseConfig):
+    """测试环境配置"""
+    # 从流量池获取视频接口地址
+    GET_VIDEOS_FROM_POOL_URL = 'http://testapi-internal.piaoquantv.com/flowpool/video/getAllVideo'
+    # 获取视频在流量池中的剩余可分发数接口地址
+    GET_REMAIN_VIEW_COUNT_URL = 'http://testapi-internal.piaoquantv.com/flowpool/video/remainViewCount'
+
+
+class PreProductionConfig(BaseConfig):
+    """预发布环境配置"""
+    # 从流量池获取视频接口地址
+    GET_VIDEOS_FROM_POOL_URL = 'http://preapi-internal.piaoquantv.com/flowpool/video/getAllVideo'
+    # 获取视频在流量池中的剩余可分发数接口地址
+    GET_REMAIN_VIEW_COUNT_URL = 'http://preapi-internal.piaoquantv.com/flowpool/video/remainViewCount'
+
+
+class ProductionConfig(BaseConfig):
+    """预发布环境配置"""
+    # 从流量池获取视频接口地址
+    GET_VIDEOS_FROM_POOL_URL = 'http://api-internal.piaoquantv.com/flowpool/video/getAllVideo'
+    # 获取视频在流量池中的剩余可分发数接口地址
+    GET_REMAIN_VIEW_COUNT_URL = 'http://api-internal.piaoquantv.com/flowpool/video/remainViewCount'
+
+
 def set_config():
-    return BaseConfig()
+    return TestConfig()

+ 61 - 0
pool_predict.py

@@ -0,0 +1,61 @@
+import time
+
+from config import set_config
+from utils import request_post
+from log import Log
+
+config_ = set_config()
+log_ = Log()
+
+
+def get_videos_from_pool(app_type, size=1000):
+    """
+    从流量池获取视频,循环获取,直到返回数据为None结束
+    :param app_type: 产品标识 type-int
+    :param size: 每次获取视频数量,type-int,默认1000
+    :return: videos
+    """
+    # 获取批次标识,利用首次获取数据时间戳为标记
+    batch_flag = time.time()
+    request_data = {'appType': app_type, 'batchFlag': batch_flag, 'size': size}
+    videos = []
+    while True:
+        result = request_post(request_url=config_.GET_VIDEOS_FROM_POOL_URL, request_data=request_data)
+        if result is None:
+            break
+        if result['code'] != 0:
+            log_.info('batch_flag: {}, 获取流量池视频失败'.format(batch_flag))
+            break
+        videos.append(result['data'])
+    return videos
+
+
+def get_videos_remain_view_count(app_type, videos_info):
+    """
+    获取视频在流量池中的剩余可分发数
+    :param app_type: 产品标识 type-int
+    :param videos_info: 视频信息 (视频id, 流量池标记) type-list,[(video_id, flow_pool), ...]
+    :return: data type-list,[(video_id, flow_pool, view_count), ...]
+    """
+    if not videos_info:
+        return []
+
+    videos = [{'videoId': info[0], 'flowPool': info[1]} for info in videos_info]
+    request_data = {'appType': app_type, 'videos': videos}
+    result = request_post(request_url=config_.GET_REMAIN_VIEW_COUNT_URL, request_data=request_data)
+
+    if result is None:
+        return []
+
+    if result['code'] != 0:
+        log_.info('获取视频在流量池中的剩余可分发数失败')
+        return []
+
+    data = [(item['videoId'], item['flowPool'], item['viewCount']) for item in result['data']]
+    return data
+
+
+if __name__ == '__main__':
+    # res = get_videos_from_pool(app_type=0)
+    res = get_videos_remain_view_count(app_type=0, videos_info=[('12345', '#2#1#111')])
+    print(res)

+ 6 - 0
pool_train.py

@@ -0,0 +1,6 @@
+def train():
+    pass
+
+
+def predict():
+    return None

+ 15 - 0
utils.py

@@ -1,5 +1,7 @@
 import pickle
 import os
+import requests
+import json
 
 from odps import ODPS
 from config import set_config
@@ -62,3 +64,16 @@ def read_from_pickle(filename, filepath=config_.DATA_DIR_PATH):
     with open(file, 'rb') as rf:
         data = pickle.load(rf)
     return data
+
+
+def request_post(request_url, request_data):
+    """
+    post 请求 HTTP接口
+    :param request_url: 接口URL
+    :param request_data: 请求参数
+    :return: res_data json格式
+    """
+    response = requests.post(url=request_url, json=request_data)
+    if response.status_code == 200:
+        res_data = json.loads(response.text)
+        return res_data